When I try to crop the bottom of an image the result is zoomed in from the top left. Without the crop the image shows just fine. I really can't see what I'm doing wrong here with the crop function. Why this doesn't return the complete image with the crop? See code and results below:
- (UIImage *)croppedImageFromScreenShot
{
// Prepare view for screen capture
// Screen Capture
CGRect fullScreenRect = self.view.bounds;
UIGraphicsBeginImageContextWithOptions(fullScreenRect.size, NO, [UIScreen mainScreen].scale);
[self.view drawViewHierarchyInRect:fullScreenRect afterScreenUpdates:YES];
UIImage *screenShotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Crop Image
CGFloat cropDistance = 20.0;
CGSize imageSize = screenShotImage.size;
CGRect imageRect = CGRectMake(0.0, 0.0, imageSize.width, imageSize.height);
CGRect cropRect = CGRectInset(imageRect, 0.0, cropDistance);
CGImageRef imageRef = CGImageCreateWithImageInRect([screenShotImage CGImage], cropRect);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
// Restore view after screen capture
return croppedImage;
}
Crop is going good but the image is zoomed in and smaller:
Without the cropping the size is just fine from code below:
- (UIImage *)croppedImageFromScreenShot
{
// Prepare view for screen capture
// Screen Capture
CGRect fullScreenRect = self.view.bounds;
UIGraphicsBeginImageContextWithOptions(fullScreenRect.size, NO, [UIScreen mainScreen].scale);
[self.view drawViewHierarchyInRect:fullScreenRect afterScreenUpdates:YES];
UIImage *screenShotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return screenShotImage;
}
Returns to original image as it also should do with the cropped function:
Change this:
CGRect cropRect = CGRectInset(imageRect, 0.0, cropDistance);
Into:
CGRect cropRect = CGRectMake(0,0,imageSize.width, imageSize.height - cropDistance);
You just want to define the specific rect you want as the subset of the image, so if you want to crop the bottom, then you just subtract from the height.