Couldn't figure out how to translate after renderInContext
is applied to a layer... so rather than sticking the scaled UIImageView's layer into renderInContext
I created a UIView the same size at origin (0,0)
then I added the imageView as a subview to the UIView I just created and translated it within the view THEN I applied renderInContext to that view!
UIGraphicsBeginImageContextWithOptions(CGSizeMake(640, 1136), YES, 0.0);
UIView *theImageHolder = [[UIView alloc] initWithFrame:CGRectMake(0,0,640,1136)];
UIImageView *theImageToCrop = [[UIImageView alloc] initWithFrame:CGRectMake(0-(theScrollView.contentOffset.x*(640/theScrollView.frame.size.width)), 0-(theScrollView.contentOffset.y*(1136/theScrollView.frame.size.height)), theScrollView.contentSize.width*(640/theScrollView.frame.size.width), theScrollView.contentSize.height*(1136/theScrollView.frame.size.height))];
theImageToCrop.contentMode = UIViewContentModeScaleAspectFill;
theImageToCrop.image = originalImageView.image;
CGContextRef context = UIGraphicsGetCurrentContext();
[theImageHolder addSubview:theImageToCrop];
[theImageHolder.layer renderInContext:context];
croppedImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
edit: It should be noted that if the user has NOT made any adjustments to the image rather than leaving the image as is when preforming the crop it will return an image of width:height = 0:0 ... this is because contentSize
is 0:0 ... To fix this instead of using contentSize in the UIImageView CGRectMake code use a float variable and define the float variables in a way that if they are equal to zero they just get set to the size of your UIScrollView frame (the same size they would be had the user not pinched them at all to scale!
float scaleWidth = imageAdjustView.contentSize.width;
float scaleHeight = imageAdjustView.contentSize.height;
if (scaleWidth == 0) {
scaleWidth = imageAdjustView.frame.size.width;
}
if (scaleHeight == 0) {
scaleHeight = imageAdjustView.frame.size.height;
}