views:

363

answers:

1

I have an app that lets the user take a picture with his/her iPhone and use it as a background image for the app. I use UIImagePickerController to let the user take a picture and set the background UIImageView image to the returned UIImage object.

IBOutlet UIImageView *backgroundView;

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
backgroundView.image = image;
[self dismissModalViewControllerAnimated:YES];
}

This all works fine. How can I reduce the size of the UIImage to 480x320 so my app can be memory efficient? I don't care if I loose any image quality.

Thanks in advance.

+1  A: 

You can create a graphics context, draw the image into that at the desired scale, and use the returned image. For example:

UIGraphicsBeginImageContext(CGSizeMake(480,320));

CGContextRef            context = UIGraphicsGetCurrentContext();

[image drawInRect: CGRectMake(0, 0, 480, 320)];

UIImage        *smallImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();    
Ben Gottlieb
I replaced line 3 with this code for aspect fit:<code>CGRect rect = CGRectMake(0, 0, 320, 480);if(image.size.width/image.size.height > 320.0/480) { rect.size.width = 480/image.size.height*image.size.width; rect.origin.x = -(rect.size.width-320)/2;} else if(image.size.width/image.size.height < 320.0/480) { rect.size.height = 320/image.size.width*image.size.height; rect.origin.y = -(rect.size.height-480)/2;}[image drawInRect: rect];
cduck
Thanks for the code, it works perfectly.
cduck
@cduck, accept the answer if it solves your problem.
KennyTM