Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Roate UIImage help

Hi,

Any image taken with the camera and saved as a UIImage doesn't seem to come out right, orientation wise that is. Does anyway have a solution on how to rotate the image to it's proper state?

Many Thanks
Dave

Macbook Pro, Mac OS X (10.4.6)

Posted on May 27, 2008 8:44 PM

Reply
Question marked as Best reply

Posted on May 27, 2008 9:59 PM

i struggled quite a bit with this... the trick is the UIImage.imageOrientation flag. i also figured out that the size returned by the camera doesn't seem to be right, to get the right size, i had to do:


CGImageRef imgRef = theImage.CGImage;
orient = theImage.imageOrientation;

CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);


from here, i created a context and put that through a series of Affine transformations. if you just want to show the image on a UIImageView, you can use the following:


UIImage *imageCopy = [[UIImage alloc]initWithCGImage:theImage.CGImage];
switch (orient) {
case UIImageOrientationLeft:
imageBackground.transform = CGAffineTransformMakeRotation(3.0 * M_PI / 2.0);
break;
case UIImageOrientationRight:
imageBackground.transform = CGAffineTransformMakeRotation(M_PI / 2.0);
break;
case UIImageOrientationDown: //EXIF = 3
imageBackground.transform = CGAffineTransformMakeRotation(M_PI);
default:
break;
}
imageBackground.image = imageCopy;


where image background is a UIImageView object. as you can see, i set the image to a copy of the UIImage passed in... so i'm guessing either i'm doing something wrong, or there is some trouble with the camera in the image picker.

this all seems to work... if you want to save the image, it gets more complicated, but i think you can use the general principles above to work that out.
22 replies

Jul 28, 2008 5:01 PM in response to Daniel Morais

A couple of notes here:

1) Posting your code bracketed by matching { code } (without the spaces) tags will format your code so it is much more readable.

2) You are hijacking someone else's thread by changing the subject. It is bad etiquette and also will not get you the same fresh eyes that creating your own new thread with an appropriate title will.

I suggest you start your own thread and repost your question there,

=Tod

Jul 29, 2008 2:04 AM in response to Tod Kuykendall

Hi Todd!

Sorry for bad formating of code, I'm still not very confortable with this, as you can see 🙂

About the 'thread hijack', I'm not sure I'm really off-topic: I may not be very clear in my message, but the problem I'm having occur only when using the function posted in the thread.

I'm not saying this function is 'bad' 😉, I just mean I may not be using it correctly and wanted to know if other people having used it could see which mistake I made.

Aug 23, 2008 6:57 AM in response to BadPirate

This worked for me too although I changed the invocation to:

-(UIImage *) scaleAndRotateImage:(UIImage *)image
{
int kMaxResolution = 60; // Or whatever

etc.

just for ease of use as a Cocoa Touch method rather than a function. No other changes were made. Thanks for this code snip. It was a lifesaver for me (after about five hours of otherwise fruitless research into how to scale the image I got back from the camera roll to fit on a 60x60 button.) This is probably a trivial change for you old cocoa hands but to cocoa noobs migrating from the Java world like me it may prove helpful.

-dB

Aug 25, 2008 8:04 AM in response to Daniel Morais

We are also getting an EXC BADACCESS error when using the code from this thread. It does not occur every time the image is scaled, but rather around 1 in every 20 times. The error occurs at the line


UIImage *imageCopy = [UIGraphicsGetImageFromCurrentImageContext() retain];


When trying to track it down, we did manage to get the following error:


Tue Aug 12 17:24:46 Benoit HelloWorld[3858] <Error>: CGContextConcatCTM: invalid context


Could the crash be related to that error?

The images we are scaling are the UIImages passed to the delegate of our UIImagePickerController. As far as we can tell we're not doing anything differently the few times it crashes.

Here is a stack trace:


#0 0x30fd96d8 in CGGStateGetRenderingIntent ()
#1 0x33ae17f8 in ripc_RenderImage ()
#2 0x33ae8888 in ripc_DrawImage ()
#3 0x30fe0adc in CGContextDelegateDrawImage ()
#4 0x30fe0a48 in CGContextDrawImage ()
#5 0x000033b4 in -[ImagePickerViewDelegate scaleAndRotateImage:] (self=0x14b9a0, _cmd=0xa880, image=0x15c9b0) at /Users/martine/workspace/app/Classes/ImagePickerViewDelegate.m:248
#6 0x0000232c in -[ImagePickerViewDelegate useImage:] (self=0x14b9a0, _cmd=0xa874, image=0x15c9b0) at /Users/martine/workspace/app/Classes/ImagePickerViewDelegate.m:50
#7 0x306761f0 in -[NSThread main] ()
#8 0x306760be in _NSThread__main_ ()
#9 0x3143cc50 in pthreadbody ()
#10 0x00000000 in ?? ()


Any ideas would be greatly appreciated, thanks!

Roate UIImage help

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.