Capturing zoomed preview view in AVFoundation

前端 未结 4 886
暗喜
暗喜 2020-12-23 23:38

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 t

4条回答
  •  悲&欢浪女
    2020-12-24 00:22

    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.

    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];
    

提交回复
热议问题