iphone - 裁剪 AVCaptureSession 捕获的图像

标签 iphone avfoundation

我正在编写一个 iPhone 应用程序,它使用 AVFoundation 来拍照并裁剪它。 该应用程序类似于 QR 码阅读器:它使用带有覆盖层的 AVCaptureVideoPreviewLayer。 覆盖层有一个正方形。我想裁剪图像,以便裁剪后的图像正是用户在正方形内放置的图像。

预览图层有重力AVLayerVideoGravityResizeAspectFill。

看起来相机实际捕捉到的内容并不完全是用户在预览图层中看到的内容。这意味着我需要从预览坐标系移动到捕获的图像坐标系,以便我可以裁剪图像。为此,我认为我需要以下参数: 1. View 尺寸与捕获图像尺寸之间的比例。 2. 告诉捕获图像的哪一部分与预览层中显示的部分相匹配的信息。

有人知道我如何获取此信息,或者是否有不同的方法来裁剪图像。

(另外,捕获预览的屏幕截图不是一个选项,据我所知,这可能会导致应用程序被拒绝)。

提前谢谢

最佳答案

希望这能满足您的要求

- (UIImage *)cropImage:(UIImage *)image to:(CGRect)cropRect andScaleTo:(CGSize)size {
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGImageRef subImage = CGImageCreateWithImageInRect([image CGImage], cropRect);
    NSLog(@"---------");     
    NSLog(@"*cropRect.origin.y=%f",cropRect.origin.y);
    NSLog(@"*cropRect.origin.x=%f",cropRect.origin.x);

    NSLog(@"*cropRect.size.width=%f",cropRect.size.width);     
    NSLog(@"*cropRect.size.height=%f",cropRect.size.height);     

    NSLog(@"---------");     

    NSLog(@"*size.width=%f",size.width);     
    NSLog(@"*size.height=%f",size.height);     

    CGRect myRect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    CGContextScaleCTM(context, 1.0f, -1.0f);
    CGContextTranslateCTM(context, 0.0f, -size.height);
    CGContextDrawImage(context, myRect, subImage);
    UIImage* croppedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CGImageRelease(subImage);     

    return croppedImage;
}

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

相关文章:

ios - AVCaptureVideoPreviewLayer 方向 - 需要横向

ios - 如何在 Swift 中将图像导出为视频?

objective-c - AVCaptureSessionPresetiPad 上的高分辨率?

iphone - 更改了 Xcode 中的目标名称现在它运行黑屏

iphone - CoreAudio 音频单元只播放一个声道的立体声音频

ios - 如何从 iOS 应用程序使用 RTSP 将视频流发送到 WOWZA 服务器?

ios - 如何确定CoreFoundation _CFXNotificationPost崩溃

ios - 我们可以在 Xcode 中删除许多列作为 UltraEdit 吗?

iphone - 为什么我的 UIPageViewController 在 curl 过渡期间会自行裁剪?

iphone - 禁用除特定 View 之外的所有 super View