Skip to content

Instantly share code, notes, and snippets.

@iamcam
Created July 27, 2012 16:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamcam/3189094 to your computer and use it in GitHub Desktop.
Save iamcam/3189094 to your computer and use it in GitHub Desktop.
Image Picker Rotation
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(@"Info: %@", info);
UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage];
UIImage* originalImg = [self scaleAndRotateImage:[info objectForKey:UIImagePickerControllerOriginalImage]];
/*** OPTION 1 - JUST USE THE "FULL SIZE" IMAGE FROM THE PICKER CONTROLLER ***/
// [self usePhoto:originalImg];
NSLog(@"Original Size w %.1f x h %.1f\tScale: %.1f", originalImg.size.width, originalImg.size.height, originalImg.scale);
NSLog(@"Edited Size w %.1f x h %.1f\tScale: %.1f", img.size.width, img.size.height, img.scale );
NSLog(@"Orientation %d",originalImg.imageOrientation);
/*** OPTION 2 - USE THE ORIGINAL FULL RESOLUTION IMAGE FROM THE PHOTO LIBRARY ***/
NSURL *referenceURL = [info objectForKey:UIImagePickerControllerReferenceURL];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:referenceURL resultBlock:^(ALAsset *asset) {
ALAssetRepresentation *rep = [asset defaultRepresentation];
/** I'm not sure this is even the best weay, but since we are using orientation in scaleAndRotateImage: might as well use it **/
// Keeps our orientation proper, from http://stackoverflow.com/a/9447468/694080
// Retrieve the image orientation from the ALAsset
UIImageOrientation orientation = UIImageOrientationUp;
NSNumber* orientationValue = [asset valueForProperty:@"ALAssetPropertyOrientation"];
if (orientationValue != nil) {
orientation = [orientationValue intValue];
}
UIImage *asstImg = [self scaleAndRotateImage:[UIImage imageWithCGImage:[rep fullResolutionImage] scale:[rep scale] orientation:orientation]];
NSLog(@"AssImg Size w %.1f x h %.1f\tScale: %.1f", asstImg.size.width, asstImg.size.height, asstImg.scale );
[self usePhoto:asstImg];
} failureBlock:^(NSError *error) {
// NSLog(@"Sorry, error");
}];
[picker dismissModalViewControllerAnimated:YES];
}
/*
Code from: http://discussions.apple.com/thread.jspa?messageID=7949889
via http://stackoverflow.com/a/538064/694080
This method rotates an image based off of its orientation - which is required
when using images from the camera roll, as they are opened in their native orientation.
This takes care of the transformation so we can get the correct output for GPUImage
*/
- (UIImage *)scaleAndRotateImage:(UIImage *)image {
int kMaxResolution = MAX(image.size.width, image.size.height); // Or whatever
CGImageRef imgRef = image.CGImage;
CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);
CGAffineTransform transform = CGAffineTransformIdentity;
CGRect bounds = CGRectMake(0, 0, width, height);
if (width > kMaxResolution || height > kMaxResolution) {
CGFloat ratio = width/height;
if (ratio > 1) {
bounds.size.width = kMaxResolution;
bounds.size.height = roundf(bounds.size.width / ratio);
}
else {
bounds.size.height = kMaxResolution;
bounds.size.width = roundf(bounds.size.height * ratio);
}
}
CGFloat scaleRatio = bounds.size.width / width;
CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
CGFloat boundHeight;
UIImageOrientation orient = image.imageOrientation;
switch(orient) {
case UIImageOrientationUp: //EXIF = 1
transform = CGAffineTransformIdentity;
break;
case UIImageOrientationUpMirrored: //EXIF = 2
transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
transform = CGAffineTransformScale(transform, -1.0, 1.0);
break;
case UIImageOrientationDown: //EXIF = 3
transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;
case UIImageOrientationDownMirrored: //EXIF = 4
transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
transform = CGAffineTransformScale(transform, 1.0, -1.0);
break;
case UIImageOrientationLeftMirrored: //EXIF = 5
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
transform = CGAffineTransformScale(transform, -1.0, 1.0);
transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
break;
case UIImageOrientationLeft: //EXIF = 6
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
break;
case UIImageOrientationRightMirrored: //EXIF = 7
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeScale(-1.0, 1.0);
transform = CGAffineTransformRotate(transform, M_PI / 2.0);
break;
case UIImageOrientationRight: //EXIF = 8
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
transform = CGAffineTransformRotate(transform, M_PI / 2.0);
break;
default:
[NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];
}
UIGraphicsBeginImageContext(bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {
CGContextScaleCTM(context, -scaleRatio, scaleRatio);
CGContextTranslateCTM(context, -height, 0);
}
else {
CGContextScaleCTM(context, scaleRatio, -scaleRatio);
CGContextTranslateCTM(context, 0, -height);
}
CGContextConcatCTM(context, transform);
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return imageCopy;
}
@aelam
Copy link

aelam commented Sep 16, 2012

CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

does this spend too much memory? if it runs on iPhone 4S the image would be very large

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment