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!
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.
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
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
Feed your brain on Twitter: @iPhoneDev101
----------------------------------------------------------------------
iPhone Apps:
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
Feed your brain on Twitter: @iPhoneDev101
----------------------------------------------------------------------
iPhone Apps:
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.
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
Feed your brain on Twitter: @iPhoneDev101
----------------------------------------------------------------------
iPhone Apps:
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:
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.
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.
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?
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
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:
I just used the the function refined by itayneeman ... it works fine ... however when I try to load the image from Camera ... and when the application is in Release Mode ... it crashes on the device with the following warning ... [that usually comes when I use the function even in Simulator Debug mode]
Code:
-[UIWindow endDisablingInterfaceAutorotation] called on <UIWindow:
0xf19f70; frame = (0 0; 320 480); opaque = NO; autoresize = RM+BM;
layer = <CALayer: 0xf1acb0>> without matching
-beginDisablingInterfaceAutorotation. Ignoring.
and I assume ... this is the main problem, that is causing me issues ... can any one shed some light on this ...
need help urgently ... appreciate a quick response ...
OK ... that was dumb of me to ask a stupid question ... I just fixed it and error was not related to this topic at all ... it was just that I was dismissing a modal controller with animation ... after pushing a new controller to the view ... how insane ...
Hi,
thanks a lot for the info.
i've used the code and it does what it's expected but i do encounter some memory problems after using it.
in my app, the user will select a photo form the photo lib, after selecting it, the application will display the photo in a UIImageView and later will be taken form the imageView for some processing.
I'm a bit new to iphoen programming and most chances i'm doing something wrong.
Since i want to fix the rotation problem, i use the method inorder ot resize the photo to the original size (by that i just fix the rotatin problem).
I call the routine with the selected image size from the DidFinishPickingMediaWithInfo method (is it ok to call it from there ?)
and after one or two selections i start to get memory warnings.
what i do in DidFinishPickingMediaWithInfo is basicly :
OK ... that was dumb of me to ask a stupid question ... I just fixed it and error was not related to this topic at all ... it was just that I was dismissing a modal controller with animation ... after pushing a new controller to the view ... how insane ...
THANK YOU - that error was driving me nuts and I had no idea what I was doing... (I was also changing the view under a modal view before dismissing the modal view).
Now that I think of it... is there anything wrong with that? I feel like not, but I'm still relatively new at iPhone app stuff.