Reputation: 3721
I am trying to use this code to allow users to zoom and crop their images in my app: https://github.com/iosdeveloper/ImageCropper/tree/master/Classes/ImageCropper
code snippet
float zoomScale = 1.0 / [scrollView zoomScale];
CGRect rect;
rect.origin.x = [scrollView contentOffset].x * zoomScale;
rect.origin.y = [scrollView contentOffset].y * zoomScale;
rect.size.width = [scrollView bounds].size.width * zoomScale;
rect.size.height = [scrollView bounds].size.height * zoomScale;
CGImageRef cr = CGImageCreateWithImageInRect([[imageView image] CGImage], rect);
UIImage *cropped = [UIImage imageWithCGImage:cr];
CGImageRelease(cr);
[delegate imageCropper:self didFinishCroppingWithImage:cropped];
But it does not take into account images being rotated (I guess it's some sort of metadata on the iPhone images). I do not want to have users draw a rectangle to crop as I much prefer this interface.
Can someone tell me how to make it not rotate my images?
I am building in iOS 5 & 6 if that helps.
[EDIT] Also, this is done nicely in the Game Center application for uploading your profile picture. Anyone know where I can find the code for that?
Upvotes: 4
Views: 4609
Reputation: 3721
I found an answer here that helped me do what I wanted iOS: Cropping a still image grabbed from a UIImagePickerController camera with overlay
Code Snippet for those interested
float zoomScale = 1.0 / [scrollView zoomScale];
CGRect rect;
NSLog(@"contentOffset is :%f,%f",[scrollView contentOffset].x,[scrollView contentOffset].y);
rect.origin.x = fabsf([scrollView contentOffset].x * zoomScale);
rect.origin.y = fabsf([scrollView contentOffset].y * zoomScale);
rect.size.width = fabsf([scrollView bounds].size.width * zoomScale);
rect.size.height = fabsf([scrollView bounds].size.height * zoomScale);
UIGraphicsBeginImageContextWithOptions( CGSizeMake( rect.size.width,
rect.size.height),
NO,
0.);
[[imageView image] drawAtPoint:CGPointMake( -rect.origin.x, -rect.origin.y)
blendMode:kCGBlendModeCopy
alpha:1.];
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Upvotes: 3