我正在尝试裁剪从 AVCaptureStillImageOutput 拍摄的图像,但无法正确裁剪正确的矩形。
我的相机视频预览是 320x458 帧,裁剪矩形出现在此预览帧内,其坐标和大小为 CGRectMake(60, 20, 200, 420)。
拍完照片后,我收到了来自
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *image = [[UIImage alloc] initWithData:imageData];
UIImage *finalImage = [self cropImage:image];
之后,我尝试使用以下函数裁剪大小为 1080x1920 的实际图像。我得到了一个任性的剪辑,结果图像超出了实际的矩形!;(
- (UIImage *)cropImage:(UIImage *)oldImage {
CGRect rect = CGRectMake(60, 20, 200, 420);
CGSize boundSize = CGSizeMake(320, 458);
float widthFactor = rect.size.width * (oldImage.size.width/boundSize.width);
float heightFactor = rect.size.height * (oldImage.size.height/boundSize.height);
float factorX = rect.origin.x * (oldImage.size.width/boundSize.width);
float factorY = rect.origin.y * (oldImage.size.height/boundSize.height);
CGRect factoredRect = CGRectMake(factorX,factorY,widthFactor,heightFactor);
UIImage *croppedImage = [UIImage imageWithCGImage:CGImageCreateWithImageInRect([oldImage CGImage], factoredRect) scale:oldImage.scale orientation:oldImage.imageOrientation];
return croppedImage;
}
在附图中,我正在尝试裁剪咖啡杯,但我得到的不是正确的裁剪图像!