0

我正在尝试裁剪从 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;
}

在附图中,我正在尝试裁剪咖啡杯,但我得到的不是正确的裁剪图像!

要裁剪的图像 裁剪的图像 - 错误

4

2 回答 2

1

我认为您应该使用 PEPhotoCropEditor 示例代码进行裁剪。它很容易使用。您可以从https://www.cocoacontrols.com/controls/pephotocropeditor下载源代码

于 2013-09-18T06:55:10.763 回答
0

i am also facing hard time in this i think the what you have to do is write image of file then crop that image or try out orientation issue to solve.

于 2013-09-18T06:08:22.287 回答