【问题标题】:Cropping captured image correctly when using AV Foundation使用 AV Foundation 时正确裁剪捕获的图像
【发布时间】:2014-02-20 18:21:51
【问题描述】:

我正在使用 AV Foundation 作为照片应用程序。我已经使用以下代码设置了一个占据屏幕上半部分的预览层:

[_previewLayer setFrame:CGRectMake(0, 0, rootLayer.bounds.size.width, rootLayer.bounds.size.height/2)];

一旦用户捕获图像,我将其旋转以使其处于正确的纵向(因为 iOS 喜欢将默认图像设置为横向),然后我创建一个新的子图层并将其内容属性设置为捕获的图像。这会使图像显示为屏幕上半部分的新图层。

唯一的问题是图像看起来被拉伸了,经过研究,我了解到您需要裁剪捕获的图像,但在尝试了几个小时后,我无法让我的裁剪正常工作。

希望有人可以推荐我需要的正确裁剪代码。我只想裁剪捕获的图像,使其与用户在拍照时在预览层中看到的完全一致。

这是我的图像捕获代码:

-(IBAction)stillImageCapture {

AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in _stillImageOutput.connections){
    for (AVCaptureInputPort *port in [connection inputPorts]){

        if ([[port mediaType] isEqual:AVMediaTypeVideo]){

            videoConnection = connection;
            break;
        }
    }
    if (videoConnection) {
        break;
    }
}

NSLog(@"about to request a capture from: %@", _stillImageOutput);


[_stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {

    if(imageDataSampleBuffer) {



        NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];


        UIImage *image = [[UIImage alloc]initWithData:imageData];


        image = [self rotate:image andOrientation:image.imageOrientation];


        CALayer *subLayer = [CALayer layer];


        CGImageRef imageRef = image.CGImage;


        subLayer.contents = (id)[UIImage imageWithCGImage:imageRef].CGImage;


        subLayer.frame = _previewLayer.frame;


        [_previewLayer addSublayer:subLayer];


    }

}];


}

【问题讨论】:

    标签: ios iphone objective-c avfoundation crop


    【解决方案1】:

    我的建议是不要谈论您是如何获得图像的。首先获取表示为 UIImages 的东西,无需操作,然后有一个对 UIImages 进行操作的转换库。您可以从this one for cropping 上接受的答案开始。

    【讨论】:

      猜你喜欢
      • 2014-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-01
      • 1970-01-01
      相关资源
      最近更新 更多