 |
 |
|
 |
07-06-2009, 10:14 PM
|
#1 (permalink)
|
|
Registered Member
Join Date: Jul 2009
Posts: 3
|
UIImagePickerController does not appear to report correct orientation of the image re
I am using the UIImagePickerController to capture a picture from the camera. I then scale and rotate the image using code that I has been posted repeatedly on various SDK forums. The problem appears that the UIImage returned to the UIImagePickerController's delegate is reporting the wrong orientation. Has anyone seen this issue before and if so, do you have any suggestions or solutions?
Here is the delegate's method to get the returned image. The UIImage passed into this method does not have the correct orientation.
Code:
- (void) imagePickerController:(UIImagePickerController *) picker
didFinishPickingImage:(UIImage *) image
editingInfo:(NSDictionary *) editingInfo
{
selectedImage = [self scaleAndRotateImage:image];
selectedImageView.image = selectedImage;
}
Here is the scale and rotate code that I mentioned from numerous other posts.
Code:
- (UIImage *)scaleAndRotateImage:(UIImage *)image
{
int kMaxResolution = 480;
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 = bounds.size.width / ratio;
}
else {
bounds.size.height = kMaxResolution;
bounds.size.width = 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 :NSInternalInconsistencyExceptionformat:@"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;
}
|
|
|
07-07-2009, 03:32 PM
|
#2 (permalink)
|
|
Registered Member
Join Date: Apr 2009
Location: Northern CA
Posts: 144
|
What do you mean "does not have the correct orientation"? How are you determining what the correct orientation is?
Note that when you display a UIImage in a UIImageView, it will take the orientation into account, but when you draw the same image using core graphics, you have to do the orientation transformation yourself (which is what scaleAndRotateImage does for you).
Pictures returned from the camera always have a left(?) orientation. Put them in a UIIMageView and you would never know that, but it is "correct".
|
|
|
07-07-2009, 06:49 PM
|
#3 (permalink)
|
|
Registered Member
Join Date: Jul 2009
Posts: 3
|
After taking a photo and choosing "use" from the UIImagePickerController, the application returns control to this method:
- (void) imagePickerController  UIImagePickerController *) picker
didFinishPickingImage  UIImage *) image
editingInfo  NSDictionary *) editingInfo
The orientation I am referring to is in the UIImage sent by the UIImagePickerController. It has a imageOrientation property and when I interrogate the value, it does not match the orientation that I took the photo in.
For example, if I have the device up (UIImageOrientationUp), the orientation returned by the this image is UIImageOrientationRight. Similar problems occur when taking pictures in left or right landscape.
|
|
|
07-08-2009, 02:57 PM
|
#4 (permalink)
|
|
Registered Member
Join Date: Apr 2009
Location: Northern CA
Posts: 144
|
You are assuming that the orientation of the CCD matches the orientation of the phone. I'm betting that they had to turn it 90 degrees to wedge it in.
Regardless, the return value may not be what you expect, but it isn't wrong. When the phone is held vertically, the bits from the camera image are all rotated to the right, and orientation is set as right. Any rotation of the phone will return the proper bits and orientation in the image.
UIImageVIews take this orientation into account when drawing, so again, everything is consistent.
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
» Online Users: 363 |
| 33 members and 330 guests |
| 63wagon, Ajameel, arcturus, Batman, cornishGeeza, Craig2, damienbern, DarrinXYZ, Desdichado, Dexter, dhouse, Duncan C, giraffe81, hermonir, iPhoneAppStudio, iSkythe, javaconvert, joshsroka, kalx, labouhassan, Leopard, listingboat, mrburns05, myshed, neo_com_3, Nungster, obecnie, pengcognito, starwarsdevwookie59, TapTouchClick, TheBearFlagRepublic, TripleA2k8, ZunePod |
| Most users ever online was 779, 05-11-2009 at 10:55 AM. |
» Stats |
Members: 23,754
Threads: 38,524
Posts: 169,208
Top Poster: smasher (2,543)
|
| Welcome to our newest member, labouhassan |
|