在AVFoundation中捕获缩放的预览视图 [英] Capturing zoomed preview view in AVFoundation

查看:186
本文介绍了在AVFoundation中捕获缩放的预览视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在AVFoundation相机中使用缩放功能,我通过缩放具有AVCaptureVideoPreviewLayer的视图实现了缩放。
现在我想拍摄缩放的图像。

I am working with zoom functionality in AVFoundation camera, i have implemented zoom by scaling the view that has AVCaptureVideoPreviewLayer. Now i want to capture the zoomed image.

这是我添加AVFoundationVideoPreviewLayer查看的代码:

here is my code for adding AVFoundationVideoPreviewLayer to view:

 // create a uiview subclass for showing the camera feed
 UIView *previewView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 430)];
 [[self view] addSubview:previewView];

 CGRect layerRect = CGRectMake(0, 0, 320, 430);
 [[self avCaptureVideoPreviewLayer] setBounds:layerRect];

 [[self  previewLayer] setPosition:CGPointMake(CGRectGetMidX(layerRect), CGRectGetMidY(layerRect))];

 // add the video previewview layer to the preview view
 [[previewView layer] addSublayer:[self avCaptureVideoPreviewLayer]];

缩放预览视图的代码

  // zoom the preview view using core graphics
 [previewView setTransform:CGAffineTransformMakeScale(2.0, 2.0 )];

现在我想从previewView中捕获这个缩放的图像

Now i want to capture this zoomed image from previewView

提前致谢。

推荐答案

最后,我设法在我的应用中添加捕获缩放的照片。它有点难看,但它的工作原理。我使用UIImage +来自网络的调整大小代码,然后将图像缩放到缩放比例,并计算缩放区域的偏移位置[在摄像机视图中可见]然后我裁剪它。

Finally i have managed to add capturing zoomed photo in my app. Its a bit ugly but it works. I used UIImage+resize code from the net and then scaled the image to zoom scale, and the calculated the offset position for the zoomed region [that is visible in the camera view] and then i cropped it.

现在,当缩放比例达到最大值时,我的应用程序有时会崩溃,即6倍。我收到内存问题,因为缩放的图像太大,我会问这是另一个问题。

Now my app crashes sometimes when the zoom scale is at max i.e. at 6x. I am receiving memory problem as the zoomed image is too big, i will ask this as a another question.

希望这段代码可能对某人有用。

Hoping this code might be usefull for someone.

 if (cameraZoom > 1.0) 
 {
        // save the images original size
        CGSize orgSize = [image size];  

        // resize the image to the zoom scale  
        image = [image resizedImage:CGSizeMake(image.size.width * cameraZoom, image.size.height *cameraZoom) interpolationQuality:kCGInterpolationNone];           

        // now calculate the offset x and offset y
        CGFloat offsetX = ( image.size.width / 2 ) - (orgSize.width /2) ;
        CGFloat offsetY =  ( image.size.height / 2 ) - (orgSize.height /2);

         // crop the image from the offset position to the original width and height
         image = [image croppedImage:CGRectMake(offsetX, offsetY, orgSize.width, orgSize.height)];
    }


    UIButton *imgBtn = (UIButton *)[self.view viewWithTag:500];
    [imgBtn setImage:image forState:UIControlStateNormal];

这篇关于在AVFoundation中捕获缩放的预览视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆