捕获AVFoundation中的缩放预览视图

kad*_*gon 9 iphone avfoundation uiview ios

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

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

 // 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]];
Run Code Online (Sandbox Code Playgroud)

用于缩放预览视图的代码

  // zoom the preview view using core graphics
 [previewView setTransform:CGAffineTransformMakeScale(2.0, 2.0 )];
Run Code Online (Sandbox Code Playgroud)

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

提前致谢.

kad*_*gon 7

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

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

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

 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];
Run Code Online (Sandbox Code Playgroud)