Sma*_*ree 0 core-image scale cifilter ciimage swift
我正在寻找一种方法将 a 调整CIImage为精确大小,因为我正在使用混合CIFilter并且需要两个 blendedCIImage的大小相同。我需要使用 aCIFilter来调整图像大小,因为它在内存方面对我来说会更便宜,而且我不能使用 Core Graphics 等。
我知道有一个CILanczosScaleTransform过滤器可用,但它不允许调整精确大小,只能缩小或放大。
有没有办法使 aCIImage成为一个精确的大小,只使用Core Image?
在CILanczosScaleTransform有两个参数:
scale:用于图像的缩放因子aspectRatio: 要在图像上使用的附加水平缩放因子使用这两个参数,您可以实现目标图像大小。计算垂直维度的缩放因子以获得所需的高度,然后计算应用于水平维度的缩放结果。这可能与目标宽度不匹配,因此计算要应用于缩放宽度的纵横比以将其校正为所需的目标宽度。
import CoreImage
let context = CIContext()
let imageURL = URL(fileURLWithPath: "sample.jpg")
let sourceImage = CIImage(contentsOf: imageURL, options: nil)
let resizeFilter = CIFilter(name:"CILanczosScaleTransform")!
// Desired output size
let targetSize = NSSize(width:190, height:230)
// Compute scale and corrective aspect ratio
let scale = targetSize.height / (sourceImage?.extent.height)!
let aspectRatio = targetSize.width/((sourceImage?.extent.width)! * scale)
// Apply resizing
resizeFilter.setValue(sourceImage, forKey: kCIInputImageKey)
resizeFilter.setValue(scale, forKey: kCIInputScaleKey)
resizeFilter.setValue(aspectRatio, forKey: kCIInputAspectRatioKey)
let outputImage = resizeFilter.outputImage
Run Code Online (Sandbox Code Playgroud)
我的示例图像有尺寸(w 2,048 h 1,536)。计算出的缩放因子为 0.1497395833333333,纵横比为 0.6195652173913043,给出的目标输出尺寸为(w 190 h 230)。
| 归档时间: |
|
| 查看次数: |
1883 次 |
| 最近记录: |