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.
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]];
code for zooming preview view
// zoom the preview view using core graphics
[previewView setTransform:CGAffineTransformMakeScale(2.0, 2.0 )];
Now i want to capture this zoomed image from previewView
Thanks in advance.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With