我需要从大小为(W,H)的ciImage中裁剪大小为(w,h)的缩略图。缩略图被裁剪并缩放为完全适合的大小(w,h)。使用Core Image裁剪和缩放转换生成此缩略图的正确方法是什么?本质上,我需要使用核心映像来实现scaledAspectFill。
这是我的代码:
var ciImage = CIImage(cvPixelBuffer: pixelBuffer)
let width = CVPixelBufferGetWidth(pixelBuffer)
let height = CVPixelBufferGetHeight(pixelBuffer)
let imageSize = CGSize(width: width, height: height)
var scaledImageRect = CGRect.zero
let widthFactor = size.width / imageSize.width
let heightFactor = size.height / imageSize.height
let aspectRatio = max(widthFactor, heightFactor)
scaledImageRect.size.width = imageSize.width * aspectRatio;
scaledImageRect.size.height = imageSize.height * aspectRatio;
scaledImageRect.origin.x = (size.width - scaledImageRect.size.width) / 2.0;
scaledImageRect.origin.y = (size.height - scaledImageRect.size.height) / 2.0;
ciImage = ciImage.cropped(to: scaledImageRect)
let rect = CGRect(x: 0, y: 0, width: size.width,
height: size.height)
//Do I need to scale again to fit 'size'??? If so, what should be scaling factor and what's the correct way to apply scaling transform that scales the image from center
let colorSpace = CGColorSpaceCreateDeviceRGB()
if let jpegData = context.jpegRepresentation(of: ciImage, colorSpace: colorSpace, options: nil) {
//Write jpegData
}
此代码是否过大?还是有一种干净,更好的方法来实现它?
答案 0 :(得分:1)
是的,您需要在调用jpegRepresentation
之前缩小图像,否则您将只从原始图像中裁剪出一个小矩形。
此代码对我有用:
let targetSize = CGSize(width: 50, height: 50)
let imageSize = ciImage.extent.size
let widthFactor = targetSize.width / imageSize.width
let heightFactor = targetSize.height / imageSize.height
let scaleFactor = max(widthFactor, heightFactor)
// scale down, retaining the original's aspect ratio
let scaledImage = ciImage.transformed(by: CGAffineTransform(scaleX: scaleFactor, y: scaleFactor))
let xInset = (scaledImage.extent.width - targetSize.width) / 2.0
let yInset = (scaledImage.extent.height - targetSize.height) / 2.0
// crop the center to match the target size
let croppedImage = scaledImage.cropped(to: scaledImage.extent.insetBy(dx: xInset, dy: yInset))