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
I was able to accomplish cropping first and then zooming using core graphics image transforms. Notice that we keep the original image size and scale in the image buffer and do the zoom feature completely with transform operations on the individual pixels. Note that this code does not take into account the device orientation (in my case I only use portrait upright currently), but could easily be accomplished with an additional rotation transform:
UIGraphicsBeginImageContextWithOptions(imageSize,NO,1.0); // this will crop
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect newRect = CGRectMake(0, 0, width, height);
// apply a scaling transform to zoom
// rotate about the center
// First we scale, then rotate, then translate
// actual matrix multiplication steps are applied in reverse when using
// right-handed coordinate system
// M' = M * translatation * rotation * scale
CGContextTranslateCTM(context, (width - scaledWidth)*0.5f,
(height-scaledHeight)*0.5f);
CGContextScaleCTM(context,scaleFactor,scaleFactor);
[sourceImage drawInRect:newRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
if (newImage == nil) {
NSLog(@"could not scale image");
}
// pop the context to get back to the default
UIGraphicsEndImageContext();