Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 433
HurkNburkS Avatar asked Feb 19 '26 14:02

HurkNburkS


2 Answers

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
}
like image 180
Shubhank Avatar answered Feb 24 '26 14:02

Shubhank


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

like image 20
Pawan Rai Avatar answered Feb 24 '26 13:02

Pawan Rai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!