gpt4 book ai didi

objective-c - AVFoundation 根据预览宽高比裁剪捕获的静止图像

转载 作者:行者123 更新时间:2023-12-01 15:52:12 33 4
gpt4 key购买 nike

我的问题大多与这个类似:

Cropping image captured by AVCaptureSession

我有一个使用 AVFoundation 捕捉静止图像的应用程序。我的 AVCaptureVideoPreviewLayer 具有 AVLayerVideoGravityResizeAspectFill 视频重力,从而使显示给用户的预览图片从顶部和底部裁剪。

当用户按下“拍摄”按钮时,实际拍摄的图像与显示给用户的预览图片不同。我的问题是如何相应地裁剪捕获的图像?

提前致谢。

最佳答案

我使用了 here 中提供的 UIImage+Resize 类别用我写的一些新方法来完成这项工作。我重新格式化了一些代码以使其看起来更好并且没有经过测试,但它应该可以工作。 :))

- (UIImage*)cropAndResizeAspectFillWithSize:(CGSize)targetSize
interpolationQuality:(CGInterpolationQuality)quality {

UIImage *outputImage = nil;
UIImage *imageForProcessing = self;

// crop center square (AspectFill)
if (self.size.width != self.size.height) {
CGFloat shorterLength = 0;
CGPoint origin = CGPointZero;
if (self.size.width > self.size.height) {
origin.x = (self.size.width - self.size.height)/2;
shorterLength = self.size.height;
}
else {
origin.y = (self.size.height - self.size.width)/2;
shorterLength = self.size.width;
}
imageForProcessing = [imageForProcessing normalizedImage];
imageForProcessing = [imageForProcessing croppedImage:CGRectMake(origin.x, origin.y, shorterLength, shorterLength)];
}
outputImage = [imageForProcessing resizedImage:targetSize interpolationQuality:quality];
return outputImage;
}

// fix image orientation, which may wrongly rotate the output.
- (UIImage *)normalizedImage {
if (self.imageOrientation == UIImageOrientationUp) return self;

UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
[self drawInRect:(CGRect){0, 0, self.size}];
UIImage *normalizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return normalizedImage;
}

关于objective-c - AVFoundation 根据预览宽高比裁剪捕获的静止图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7028221/

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