HurkNburkS
HurkNburkS

Reputation: 5510

Resize UIImage from UIImagePickerController

I am currently taking a photo using this code here

- (void) cameraButtonSelected
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;

    [self presentViewController:picker animated:YES completion:NULL];
}

I am allowing the user to edit the the photo but when I use this delegate method for some reason the UIImagePickerController will not remove from view after the user has pressed "Use Photo"

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

I would like to know

  1. How do I remove UIImagePickerController from view after "Use Photo" button pressed
  2. How can I resize the photo I have just taken as I need a smaller variant to send to my server

Upvotes: 2

Views: 9909

Answers (3)

Pawan Rai
Pawan Rai

Reputation: 3444

simple , you can search it here on stackoverflow

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
 UIImage *tempImage=[info objectForKey:UIImagePickerControllerEditedImage];

 [self.dealImageView setImage:[self imageWithImage:tempImage convertToSize:CGSizeMake(200, 200)]];

 [self dismissViewControllerAnimated:YES completion:nil];

}

- (UIImage *)imageWithImage:(UIImage *)image convertToSize:(CGSize)size {

  UIGraphicsBeginImageContext(size);
  [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
  UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  return destImage;
}

whats-the-easiest-way-to-resize-optimize-an-image-size-with-the-iphone-sdk

resizing-and-cropping-a-uiimage

Upvotes: 5

Shubhank
Shubhank

Reputation: 21805

You can get the image by querying for UIImagePickerControllerEditedImage in the info dict. And to remove the ImagePicker from View just dismiss the picker. Here is the code to resize. Just call it with your image instance

You can use this function to scale the image to particular size

- (UIImage *) scaleImage:(UIImage*)image toSize:(CGSize)newSize {
    //UIGraphicsBeginImageContext(newSize);
    // In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
    // Pass 1.0 to force exact pixel size.
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

So your final code should be something like this

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {    
    [picker dismissViewControllerAnimated:YES completion:Nil]; 
    UIImage *image = info[UIImagePickerControllerEditedImage];
    image = [self scaleImage:image toSize:CGSizeMake(200,200)]; // or some other size
}

Upvotes: 5

bvogelzang
bvogelzang

Reputation: 1415

  1. Call [self dismissModalViewControllerAnimated:YES completion:nil] somewhere in your - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info method. This is common practice for any modal view.

  2. This question is probably more complicated than you might think. There are a number of ways to resize a photo in iOS and the code you use really will depend on your needs. However you can take a look at this blog post to get an understanding of photo resizing. It's very comprehensive and I would recommend reading it before you write any code.

    http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

    That being said this is a very simple way to achieve resizing:

    The simplest way to resize an UIImage?

Upvotes: 1

Related Questions