gpt4 book ai didi

iphone - 裁剪从 AVCaptureStillImageOutput 获取的图像

转载 作者:行者123 更新时间:2023-11-29 03:38:08 29 4
gpt4 key购买 nike

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

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

Image to be cropped Cropped image - wrong

最佳答案

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

关于iphone - 裁剪从 AVCaptureStillImageOutput 获取的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18854722/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com