简体   繁体   English

AVFoundation 根据预览纵横比裁剪捕获的静止图像

[英]AVFoundation crop captured still image according to the preview aspect ratio

My question is mostly similar to this one:我的问题与这个问题大体相似:

Cropping image captured by AVCaptureSession 裁剪由 AVCaptureSession 捕获的图像

I have an application which uses AVFoundation for capturing still images.我有一个使用 AVFoundation 来捕获静止图像的应用程序。 My AVCaptureVideoPreviewLayer has AVLayerVideoGravityResizeAspectFill video gravity thus making preview picture which is shown to the user to be cropped from the top and from the bottom parts.我的 AVCaptureVideoPreviewLayer 具有 AVLayerVideoGravityResizeAspectFill 视频重力,因此可以从顶部和底部裁剪向用户显示的预览图片。

When user is pressing "Capture" button, the image actually captured is differs from the preview picture shown to user.当用户按下“捕获”按钮时,实际捕获的图像与显示给用户的预览图片不同。 My question is how to crop captured image accordingly?我的问题是如何相应地裁剪捕获的图像?

Thanks in advance.提前致谢。

I used UIImage+Resize category provided in here with some new methods I wrote to do the job.我使用了此处提供的UIImage+Resize类别和我编写的一些新方法来完成这项工作。 I reformatted some code to look better and not tested, but it should work.我重新格式化了一些代码以使其看起来更好并且未经测试,但它应该可以工作。 :)) :))

- (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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用avfoundation时 - 裁剪在显示层中表示的捕获图像的可见部分 - When using avfoundation - crop visible portion of captured image represented in display layer 使用AVFoundation静止图像 - Use AVFoundation Still Image 尝试将我的UIImage裁切为1:1的纵横比(正方形),但它会不断放大图像,从而导致图像模糊。 为什么? - Trying to crop my UIImage to a 1:1 aspect ratio (square) but it keeps enlarging the image causing it to be blurry. Why? 调整图像保持宽高比的大小 - Resize image maintaining aspect ratio 如何使用宽高比调整图像大小? - How to resize image with aspect ratio? UIViewContentModeScaleAspectFill不保持图像的宽高比 - UIViewContentModeScaleAspectFill not maintaining aspect ratio of image 裁剪UIImage的部分以获得新的宽高比,并缩小到大小 - Crop off parts of UIImage for new aspect ratio and scale down to size 使用iOS中的PEPhotoCropEditor库,恒定的作物纵横比 - Constant crop aspect ratio using PEPhotoCropEditor library in iOS AVFoundation捕获静止图像的速度太慢 - AVFoundation capture a still Image too slowly 使用AVFoundation时,对于哪个对象实际包含捕获的图像感到困惑 - Confused on what object actually contains the captured image when using AVFoundation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM