Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-26-2008, 10:55 PM   #1 (permalink)
New Member
 
Join Date: Apr 2008
Posts: 10
Default rotate a 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
ddavtian is offline   Reply With Quote
Old 05-27-2008, 01:24 AM   #2 (permalink)
New Member
 
Join Date: Apr 2008
Location: Lexington, KY
Posts: 108
Default Re: rotate a UIImage help

Either add it to a UIImageView and set a rotation transform or, if you need it to stay a UIImage, you will need to drop down to core graphics to rotate it (or flip it)
Fastrak is offline   Reply With Quote
Old 05-27-2008, 11:42 PM   #3 (permalink)
New Member
 
Join Date: Apr 2008
Posts: 10
Default Re: rotate a UIImage help

I think working with a UIImageView would be easier, do you my any chance have an example you can share?

Basically what I want to do is this

UIIMage --> UIImageView --> rotate --> UIImage --> send

Currently I have the UIImage and it's sending as is.

Thanks in Advance
Dave
ddavtian is offline   Reply With Quote
Old 05-28-2008, 02:32 AM   #4 (permalink)
New Member
 
Join Date: Apr 2008
Posts: 802
Default Re: rotate a UIImage help

Look up UIImageOrientation in the docs. There are settings that can be applied to UIImage.
I think there's info in the editinfo from the camera picker.

If you want to rotate it directly use CGAffineTransformRotate.
scottiphone is offline   Reply With Quote
Old 05-28-2008, 09:47 PM   #5 (permalink)
New Member
 
Join Date: Apr 2008
Posts: 10
Default Re: rotate a UIImage help

Hmm would this work?

+(UIImage *)rotateImageUIImage *)image {

int orient = image.imageOrientation;

UIImageView *imageView = [[UIImageView alloc] init];

UIImage *imageCopy = [[UIImage alloc] initWithCGImage:image.CGImage];


switch (orient) {
case UIImageOrientationLeft:
imageView.transform = CGAffineTransformMakeRotation(3.0 * M_PI / 2.0);
break;
case UIImageOrientationRight:
imageView.transform = CGAffineTransformMakeRotation(M_PI / 2.0);
break;
case UIImageOrientationDown: //EXIF = 3
imageView.transform = CGAffineTransformMakeRotation(M_PI);
default:
break;
}

imageView.image = imageCopy;
return (imageView.image);
}
ddavtian is offline   Reply With Quote
Old 05-29-2008, 05:13 PM   #6 (permalink)
New Member
 
Join Date: Apr 2008
Posts: 802
Default Re: rotate a UIImage help

I haven't studied your code in too much detail but place a draw function after the transform to see if it's working.

Note that if you use orientation I don't think you have to rotate it manually. Since drawing ir a different orientation for the UI compared to some other image libraries they try to make it reasonable to do. Do a search of the example code for orientation or transform.
Once again try drawing before and after so you can see what happens.

You may have to draw and capture it.

Note that you'll have leaks since you've allocated a couple of objects but haven't released them.
scottiphone is offline   Reply With Quote
Old 02-02-2009, 01:19 PM   #7 (permalink)
Registered Member
 
Join Date: Oct 2008
Location: Columbus, OH
Posts: 56
Send a message via AIM to pinpoint
Default Resolution?

did anyone get this to work?

I'm examining the UIImage.orientation of both pics selected from the library and pics taken with the camera, and they're both "UP"... yet my camera pics show on the screen sideways. Oddly enough i'm using the same technique to generate thumbnails, and they're coming out the right orientation.

Thanks!
__________________
Dave Peat
Pinpoint Solutions, Inc.
pinpoint is offline   Reply With Quote
Old 02-02-2009, 01:27 PM   #8 (permalink)
Registered Member
 
Join Date: Oct 2008
Location: Columbus, OH
Posts: 56
Send a message via AIM to pinpoint
Default Resolution...DUH

OK, I was looking at the image at the wrong point- the UIImage returned by the camera is "RIGHT", not UP.

Once i've saved the image out, and re-read it back in again it's UP.

That still leaves me with the task of rotating the image 90 degrees before I save it... anyone have a little code that works?

Thanks!
__________________
Dave Peat
Pinpoint Solutions, Inc.
pinpoint is offline   Reply With Quote
Old 02-03-2009, 05:50 PM   #9 (permalink)
Registered Member
 
Join Date: Oct 2008
Location: Columbus, OH
Posts: 56
Send a message via AIM to pinpoint
Default

Quote:
Originally Posted by pinpoint View Post
OK, I was looking at the image at the wrong point- the UIImage returned by the camera is "RIGHT", not UP.

Once i've saved the image out, and re-read it back in again it's UP.

That still leaves me with the task of rotating the image 90 degrees before I save it... anyone have a little code that works?

Thanks!
Found code that does this here. Works like a charm.
__________________
Dave Peat
Pinpoint Solutions, Inc.
pinpoint is offline   Reply With Quote
Old 10-07-2009, 10:40 PM   #10 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Australia
Posts: 180
Default

Hi,

Ive used his code but my images are still the same.

Im not sure how his code works, can someone guide me how to rotate back the image with his code?

Logic High Software UIImage fix

Here is my code:
Code:
UIImage *imageimage2 = [[UIImage alloc] initWithContentsOfFile:pathToImage];
[self scaleAndRotateImage:imageimage2];
this is his code, i have modified a bit at the end
Code:
- (void)scaleAndRotateImage:(UIImage *)image
{
	int kMaxResolution = 320; // 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 = 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: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();
	
	[self setRotatedImage:imageCopy];
	//return imageCopy;
}
svveet is offline   Reply With Quote
Old 10-07-2009, 11:09 PM   #11 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Australia
Posts: 180
Default

fixed, i need to rotate it before i save the camera image to file

Quote:
Originally Posted by svveet View Post
Hi,

Ive used his code but my images are still the same.

Im not sure how his code works, can someone guide me how to rotate back the image with his code?

Logic High Software UIImage fix

Here is my code:
Code:
UIImage *imageimage2 = [[UIImage alloc] initWithContentsOfFile:pathToImage];
[self scaleAndRotateImage:imageimage2];
this is his code, i have modified a bit at the end
Code:
- (void)scaleAndRotateImage:(UIImage *)image
{
	int kMaxResolution = 320; // 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 = 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: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();
	
	[self setRotatedImage:imageCopy];
	//return imageCopy;
}
svveet is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting data from an UIImage realberen iPhone SDK Development 11 03-16-2009 02:43 AM
Scale & Rotate Gestures - UIImageView col iPhone SDK Development 1 07-14-2008 08:33 AM
UIImage pinch to shrink tkilmer iPhone SDK Development 0 06-25-2008 12:48 AM
Trouble with UIImage Touch tutorial drake iPhone SDK Development 6 05-20-2008 03:35 PM
How do I rotate a image pointed to by a CGImageRef Fastrak iPhone SDK Development 3 04-19-2008 10:08 PM


» Advertisements
» Online Users: 421
9 members and 412 guests
AppleSteve, banatary70, chiataytuday, DaveDee, Domele, ilmman, Jameswhitfield, johnRambo, Objective Zero
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 157,854
Threads: 88,915
Posts: 379,298
Top Poster: BrianSlick (7,072)
Welcome to our newest member, Jameswhitfield
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:38 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0