我有自定义相机视图,最重要的是我有指南我想将拍摄的照片裁剪到该指南,当我得到不同部分的裁剪图像时,我想获得指南内的区域。
func imageByCropToRect(rect:CGRect, scale:Bool) -> UIImage {
var rect = rect
var scaleFactor: CGFloat = 1.0
if scale {
scaleFactor = self.scale
rect.origin.x *= scaleFactor
rect.origin.y *= scaleFactor
rect.size.width *= scaleFactor
rect.size.height *= scaleFactor
}
var image: UIImage? = nil;
if rect.size.width > 0 && rect.size.height > 0 {
let imageRef = self.cgImage!.cropping(to: rect)
image = UIImage(cgImage: imageRef!, scale: scaleFactor, orientation: self.imageOrientation)
}
return image!
}
if let image = self.getImageFromSampleBuffer(buffer: sampleBuffer, orientation: orientation) {
let newImage = image.imageByCropToRect(rect: self.guideImageView.frame, scale: true)
selectedPropertyImage = newImage
stopCaptureSession()}