Jordan Smith
Jordan Smith

Reputation: 10378

iPhone: problems resizing image from UIImagePickerController

What I'm trying to achieve:

At the moment, I have a UIImagePickerController set up to take a picture, and return it as a small-sized photo (about 300 x 500ish pixels).

By default, the image obtained from the image picker is full resolution - i.e. quite large. So before doing anything with this image, I resize it:

UIGraphicsBeginImageContext(targetSize);

CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width  = scaledWidth;
thumbnailRect.size.height = scaledHeight;

[sourceImage drawInRect:thumbnailRect];

newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

-

What doesn't work so well:

This step is quite resource intensive, in fact it freezes the app for several seconds during the operation. I haven't tried it on an iPhone 4, but I can imagine it will only be worse with HD images.

Aside from being resource intensive, getting this photo also seems to use up a very large amount of memory for a small period of time, often resulting in the app crashing. I am assuming that this memory is from the large sized images that are being captured at first.

-

So:

What it the best/standard method of getting small images from UIImagePicker? I don't want my app to freeze or crash due to high CPU or RAM usage. Surely there must be a better, more stable and efficient way?

Any help is much appreciated :)

Upvotes: 1

Views: 3589

Answers (1)

scalbatty
scalbatty

Reputation: 788

You may find the Trevor's UIImage categories useful to resize images the right way and not reinventing the wheel. That's what I use and it works great.

Resize a UIImage the right way

As for the performance issue, you may call your resize method in the background, or put it in an operation to free your main thread of this work.

Upvotes: 3

Related Questions