Home News Forum Social Networking Support Us Advertise

Spanish Lesson 1 ($1.99)

aWake!Gently ($1.99)

The Bird & The Snail - Knock Knock - Deluxe ($4.99)

Match-It Trains ($0.99)

Tangled ($0.99)

iFlatter ($0.99)

The 15 puzzle ($0.99)

Tap Forms Database ($8.99)

Higher or Lower Card Game (Hi Lo) ($0.99)

Red Pixel ($0.99)

Time-Shift Radio ($0.99)

Want your application advertised here? Only $10/week!

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

Reply
 
LinkBack Thread Tools Display Modes
Old 12-02-2008, 06:10 PM   #1 (permalink)
Senior Member
 
cmezak's Avatar
 
Join Date: Jul 2008
Posts: 175
Rep Power: 1
cmezak is on a distinguished road
Default Resizing a photo to a new UIImage

This is crazy. I know there are threads that touch on this already, but none of them have led me to the answer. I can't believe that it is really this difficult!

I want to get an image from the camera via UIImagePicker and resize the image so I can use it in my app. Fullsize images from the camera are far too large to hold in memory in large numbers. I can handle the ImgePicker fine. But once I get the fullsize image from it, I need to resize the image.

For the life of me, I can't figure out how to accomplish this simple task.

From what I've read so far, I have to do some quartz2d programming using bitmatcontexts and transforms. The results so far are not good. even when I can get an image out of the other end of my code, it is not oriented correctly or it is stretched the wrong way. If I attempt to apply more transforms to fix the problem, I'm thwarted again.

I know this is a rant and a cop out, but could someone help me get to a simple function that look like this:

Code:
- (UIImage *)resizeImage:(UIImage *)original (CGSize)newSize {

* * * MAGIC * * * 

return resizedImage (not resized and stretched or resized and rotated!)

}
I'd post some of my own code, but it is mostly hacked up versions of code I've found elsewhere on the net. I'm a fine iPhone programmer, but I've never dealt with low-level image stuff. I hoped that such a simple task as resizing an image would not be so difficult!

- Charlie
cmezak is offline   Reply With Quote
Old 12-02-2008, 08:14 PM   #2 (permalink)
Member
iPhone Dev SDK Supporter
 
Join Date: Oct 2008
Location: Columbus, OH
Posts: 35
Rep Power: 0
pinpoint is on a distinguished road
Send a message via AIM to pinpoint
Smile This works for me

Here's a solution that has worked for me so far.
I got most if not all of the code from another post here. I did have to hard code the alpha setting or I got a run-time error.

I won't pretend to know how it works at this point, but it does the job.
I'm having memory leaks at the moment, but I suspect they're related to other parts of my code not this.

Code:
-(UIImage *)resizeImage:(UIImage *)image width:(int)width height:(int)height {
	
	CGImageRef imageRef = [image CGImage];
	CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
	
	//if (alphaInfo == kCGImageAlphaNone)
		alphaInfo = kCGImageAlphaNoneSkipLast;
	
	CGContextRef bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), 4 * width, CGImageGetColorSpace(imageRef), alphaInfo);
	CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
	CGImageRef ref = CGBitmapContextCreateImage(bitmap);
	UIImage *result = [UIImage imageWithCGImage:ref];
	
	CGContextRelease(bitmap);
	CGImageRelease(ref);
	
	return result;	
}
__________________
Dave Peat
Pinpoint Solutions, Inc.
pinpoint is offline   Reply With Quote
Old 12-03-2008, 12:28 PM   #3 (permalink)
Senior Member
 
cmezak's Avatar
 
Join Date: Jul 2008
Posts: 175
Rep Power: 1
cmezak is on a distinguished road
Default

I get this error when I run that code:

Wed Dec 3 13:27:41 CMEZAK.WIFI.WPI.EDU cameraTest[41687] <Error>: CGBitmapContextCreate: unsupported parameter combination: 5 integer bits/component; 16 bits/pixel; 3-component colorspace; kCGImageAlphaNoneSkipLast; 200 bytes/row.
Wed Dec 3 13:27:41 CMEZAK.WIFI.WPI.EDU cameraTest[41687] <Error>: CGContextDrawImage: invalid context
Wed Dec 3 13:27:41 CMEZAK.WIFI.WPI.EDU cameraTest[41687] <Error>: CGBitmapContextCreateImage: invalid context
cmezak is offline   Reply With Quote
Old 12-03-2008, 12:41 PM   #4 (permalink)
Senior Member
 
cmezak's Avatar
 
Join Date: Jul 2008
Posts: 175
Rep Power: 1
cmezak is on a distinguished road
Default

Using this code gets me an image, but the color is wrong:

Code:
-(UIImage *)resizeImage:(UIImage *)image width:(int)width height:(int)height {
	
	CGImageRef imageRef = [image CGImage];
	CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
	CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);
	
	if (alphaInfo == kCGImageAlphaNone)
	alphaInfo = kCGImageAlphaNoneSkipLast;
	
	CGContextRef bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, alphaInfo);
	CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
	CGImageRef ref = CGBitmapContextCreateImage(bitmap);
	UIImage *result = [UIImage imageWithCGImage:ref];
	
	CGContextRelease(bitmap);
	CGImageRelease(ref);
	
	return result;	
}
The resulting image looks like this:



- Charlie
cmezak is offline   Reply With Quote
Old 12-03-2008, 12:43 PM   #5 (permalink)
Senior Member
 
cmezak's Avatar
 
Join Date: Jul 2008
Posts: 175
Rep Power: 1
cmezak is on a distinguished road
Default

Does the problem have to do with bytes per row? Since I am resizing, do I have to do a conversion?

- Charlie
cmezak is offline   Reply With Quote
Old 12-03-2008, 12:57 PM   #6 (permalink)
Member
iPhone Dev SDK Supporter
 
Join Date: Oct 2008
Location: Columbus, OH
Posts: 35
Rep Power: 0
pinpoint is on a distinguished road
Send a message via AIM to pinpoint
Default Alpha Settings

From the prior post, I think the issue is with the alpha setting.
I received a similar error, and that's why i hard coded the alpha setting the way I did.

Not knowing exactly what I was doing of course leaves me wide open to an image with parms I don't expect blowing up or looking differently than i'd like.

If anyone can shed some light on this i'd be very interested! I just don't have time to look at the docs right now
__________________
Dave Peat
Pinpoint Solutions, Inc.
pinpoint is offline   Reply With Quote
Old 12-03-2008, 01:53 PM   #7 (permalink)
Senior Member
 
cmezak's Avatar
 
Join Date: Jul 2008
Posts: 175
Rep Power: 1
cmezak is on a distinguished road
Default

I've been looking at the docs, and they're not too helpful. I'm surprised, because this seems like an essential iphone task.

- Charlie
cmezak is offline   Reply With Quote
Old 12-08-2008, 04:48 PM   #8 (permalink)
Lost in a sea of code
iPhone Dev SDK Supporter
 
BostonMerlin's Avatar
 
Join Date: Apr 2008
Location: Boston
Posts: 368
Rep Power: 0
BostonMerlin is an unknown quantity at this point
Default

I am looking to do exactly the same thing. Take a picture, resize the image to just the size of the iphones screen.. then save it. That image is being sent out to a server and back out to other phones.. need to get the filesize down. I'm already using UIImageJPEGRepresentation with a compression of 0.0 but they're still way too big for what's needed.

If anyone can help move this along I would be forever grateful!

Thanks
john
__________________
----------------------------------------------------------------------
I love being a dad, flying airplanes and writing code.
----------------------------------------------------------------------
Follow me on Twitter: @BostonMerlin
----------------------------------------------------------------------
iPhone Apps: The Pilots Library
BostonMerlin is offline   Reply With Quote
Old 12-08-2008, 04:50 PM   #9 (permalink)
Senior Member
 
cmezak's Avatar
 
Join Date: Jul 2008
Posts: 175
Rep Power: 1
cmezak is on a distinguished road
Default

Here is my code to shrink the camera's output to 640x480.

I can't claim mastery of what happens in it -- it's the result of a good deal of struggle -- but at least it works!

If you tweak it or make it more flexible/robust, please post the result here!

- Charlie

Code:
-(UIImage *)resizeImage:(UIImage *)image {
	
	CGImageRef imageRef = [image CGImage];
	CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
	CGColorSpaceRef colorSpaceInfo = CGColorSpaceCreateDeviceRGB();
	
	if (alphaInfo == kCGImageAlphaNone)
		alphaInfo = kCGImageAlphaNoneSkipLast;
	
	int width, height;
	
	width = 640;
	height = 480;
	
	CGContextRef bitmap;
	
	if (image.imageOrientation == UIImageOrientationUp | image.imageOrientation == UIImageOrientationDown) {
		bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, alphaInfo);
		
	} else {
		bitmap = CGBitmapContextCreate(NULL, height, width, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, alphaInfo);
		
	}
	
	if (image.imageOrientation == UIImageOrientationLeft) {
		NSLog(@"image orientation left");
		CGContextRotateCTM (bitmap, radians(90));
		CGContextTranslateCTM (bitmap, 0, -height);
		
	} else if (image.imageOrientation == UIImageOrientationRight) {
		NSLog(@"image orientation right");
		CGContextRotateCTM (bitmap, radians(-90));
		CGContextTranslateCTM (bitmap, -width, 0);
		
	} else if (image.imageOrientation == UIImageOrientationUp) {
		NSLog(@"image orientation up");	
		
	} else if (image.imageOrientation == UIImageOrientationDown) {
		NSLog(@"image orientation down");	
		CGContextTranslateCTM (bitmap, width,height);
		CGContextRotateCTM (bitmap, radians(-180.));
		
	}
	
	CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
	CGImageRef ref = CGBitmapContextCreateImage(bitmap);
	UIImage *result = [UIImage imageWithCGImage:ref];
	
	CGContextRelease(bitmap);
	CGImageRelease(ref);
	
	return result;	
}
cmezak is offline   Reply With Quote
Old 12-08-2008, 04:57 PM   #10 (permalink)
Lost in a sea of code
iPhone Dev SDK Supporter
 
BostonMerlin's Avatar
 
Join Date: Apr 2008
Location: Boston
Posts: 368
Rep Power: 0
BostonMerlin is an unknown quantity at this point
Default

Thanks Charlie. I'll give this a whirl. I started messing around with code from another thread and it's causing all kinds of weirdness.

John
__________________
----------------------------------------------------------------------
I love being a dad, flying airplanes and writing code.
----------------------------------------------------------------------
Follow me on Twitter: @BostonMerlin
----------------------------------------------------------------------
iPhone Apps: The Pilots Library
BostonMerlin is offline   Reply With Quote
Old 12-08-2008, 04:59 PM   #11 (permalink)
Senior Member
 
cmezak's Avatar
 
Join Date: Jul 2008
Posts: 175
Rep Power: 1
cmezak is on a distinguished road
Default

Yeah. I found that handling the transforms to correctly orient the images was the most tricky part. The documentation for this is not clear. Also, I found that images from the photo library come out pink. I couldn't resolve this, but since my app is using images from the camera (which come out just fine) it's not a big deal for me.

Good luck!

- Charlie
cmezak is offline   Reply With Quote
Old 12-08-2008, 05:01 PM   #12 (permalink)
Lost in a sea of code
iPhone Dev SDK Supporter
 
BostonMerlin's Avatar
 
Join Date: Apr 2008
Location: Boston
Posts: 368
Rep Power: 0
BostonMerlin is an unknown quantity at this point
Default

Hey Charlie.. I'm getting an error on the following line of code.. looks like you have another method out there I need or need to reference another library?

Error: Implicit declaration of function radians..

CGContextRotateCTM (bitmap, radians(90));

Thoughts?

Thanks again
John
__________________
----------------------------------------------------------------------
I love being a dad, flying airplanes and writing code.
----------------------------------------------------------------------
Follow me on Twitter: @BostonMerlin
----------------------------------------------------------------------
iPhone Apps: The Pilots Library
BostonMerlin is offline   Reply With Quote
Old 12-08-2008, 05:03 PM   #13 (permalink)
Senior Member
 
cmezak's Avatar
 
Join Date: Jul 2008
Posts: 175
Rep Power: 1
cmezak is on a distinguished road
Default

Oh right. That's just something Apple recommends in the documentation on CTM transforms in the quartz2d programming guide. I suggest you check it out, though it didn't clear up all of my questions.

Anyway, the missing function just translates degrees into radians, which are what those methods need. Here it is:

Code:
#include <math.h>
static inline double radians (double degrees) {return degrees * M_PI/180;}
cmezak is offline   Reply With Quote
Old 01-06-2009, 10:36 AM   #14 (permalink)
Member
 
Join Date: Nov 2008
Posts: 34
Rep Power: 0
narut is on a distinguished road
Default

Quote:
Originally Posted by cmezak View Post
Yeah. I found that handling the transforms to correctly orient the images was the most tricky part. The documentation for this is not clear. Also, I found that images from the photo library come out pink. I couldn't resolve this, but since my app is using images from the camera (which come out just fine) it's not a big deal for me.

Good luck!

- Charlie
First of all, thanks for the code cmezak. You really did save my life.

I'm totally newbie about Quartz, but from trial and error, I've managed to get the code working for both camera and library.
Looks like the photo from library turn pink because CGImageGetBitsPerComponent(imageRef) will always return 5 bits per component (got 8 bits from camera images).
I'm not sure if the Generic RGB Color Space returned from CGColorSpaceCreateDeviceRGB() will assume that you use 8 bits/component picture. So that's why only the images from library was not processed properly.

Here's what I do
Code:
bitmap = CGBitmapContextCreate(NULL, longSide, shortSide, 8, image.size.width*3, 
CGColorSpaceCreateDeviceRGB(), alphaInfo);
Not even sure if there would be any bad side effects for harding code like this. But I think I'm gonna keep it this way until I learn more about Quartz things.
narut is offline   Reply With Quote
Old 02-11-2009, 04:37 PM   #15 (permalink)
Junior Member
 
Join Date: Feb 2009
Posts: 4
Rep Power: 0
MikeRiegger is on a distinguished road
Default Great sample

Hey thanks for the excellent example cmezek!

Two things I noticed though, I think you might need a:

CGColorSpaceRelease(colorSpaceInfo);

In the above example. Also, should:


if (image.imageOrientation == UIImageOrientationUp | image.imageOrientation == UIImageOrientationDown) {


that be a || instead of a |?

Anyway, the code is working great though on my iphone, thanks for posting it.

-Michael Riegger
MikeRiegger is offline   Reply With Quote
Old 02-12-2009, 03:17 PM   #16 (permalink)
Junior Member
 
Join Date: Dec 2008
Posts: 8
Rep Power: 0
rocaz is on a distinguished road
Default

Good stuff--I have slightly different need. I am wondering if someone looking at this thread has already figured this "easier" version out. I don't want to transform the data--I just want to set the same rotation flag that Apple uses in its stored photos. If you process the nominal data it appears that the data is rotated landscape vs. portrait (in the nominal position). I believe imageOrientation is the key (UIImage) but that particular data member is read only according to the docs. I haven't figured out how to set the same orientation in the output images. Anybody have an idea?
rocaz is offline   Reply With Quote
Old 03-22-2009, 12:25 AM   #17 (permalink)
Member
 
Join Date: Mar 2009
Posts: 47
Rep Power: 0
XenoSage is on a distinguished road
Default

here's what i think to be an easier way to resize and image.

Code:
UIGraphicsBeginImageContext( newSize );// a CGSize that has the size you want
   [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
//image is the original UIImage
   UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();
newImage is your newly created image with the size you want
XenoSage is offline   Reply With Quote
Old 03-22-2009, 12:57 AM   #18 (permalink)
Senior Member
 
Join Date: Aug 2008
Posts: 123
Rep Power: 1
bhearn is on a distinguished road
Default

Quote:
Originally Posted by XenoSage View Post
here's what i think to be an easier way to resize and image.

Code:
UIGraphicsBeginImageContext( newSize );// a CGSize that has the size you want
   [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
//image is the original UIImage
   UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();
newImage is your newly created image with the size you want
Yes, this is the easy way to do it -- unless you want to do it in a background thread:

background threads and graphics

Then it looks like you have to use something like the code here.
bhearn 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


» Advertisements


» Stats
Members: 8,231
Threads: 20,199
Posts: 90,216
Top Poster: RickMaddy (2,121)
Welcome to our newest member, OneGlobe
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:44 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0