Search code examples
iphonexcodeuiimagepickercontroller

Compressing / down size / resize image while storing image in iPhone application


To understand my question please go through following:

  • In my application user first taps on a button.
  • Image picker controller is displayed
  • user selects images / an image from it.
  • all that images must be saved to my iPhone application.

I have already implemented this and for doing this I have implemented following code.

 -(IBAction)setPhoto:(id)sender {
    facPhotoPicker=[[UIImagePickerController alloc]init];
    facPhotoPicker.delegate=self;
    facPhotoPicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
    facPhotoPicker.allowsImageEditing=YES;
    facPhotoPicker.navigationBar.barStyle=UIBarStyleBlackOpaque;
    [self presentModalViewController:facPhotoPicker animated:YES];
}

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {
    NSData *imgData=UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],1);
    UIImage *img=[[UIImage alloc] initWithData:imgData];
    facImgView.image=img;
    [img release];
    NSString *str=[NSString stringWithFormat:@"%i.jpg",[currentFaculty facultyNo]];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0], str];
    [imgData writeToFile:path atomically:YES];
    [picker dismissModalViewControllerAnimated:YES];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissModalViewControllerAnimated:YES];
}

But the problem is the user's iPhone may have larger images.

I don't want to store that large images within application. for example

  • user selects an image having size of 1200 x 800
  • But I want only 80 x 80 size image

    1. selected images should be down sized to my requirement / 8 mb image to less then 500 kb
    2. how to store image within resource directory instead of storing in documents directory?

Solution

  • This is solved.

    The main key for downsizing. I downsized an image up to 116 kb.


    NSData *imgData=UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],compressionRatio);
    while ([imgData length]>50000) { 
        compressionRatio=compressionRatio*0.5;
        imgData=UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],compressionRatio);
    }
    

    -(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
    {
    double compressionRatio=1;
    NSData *imgData=UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],compressionRatio);
    while ([imgData length]>50000) { 
        compressionRatio=compressionRatio*0.5;
        imgData=UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],compressionRatio);
    }
    UIImage *img=[[UIImage alloc] initWithData:imgData];
    facImgView.image=img;
    NSLog(@"%@",[info objectForKey:@"UIImagePickerControllerOriginalImage"],3);
    [img release];
    
    
    NSString *str=[NSString stringWithFormat:@"%i.jpg",[currentFaculty facultyNo]];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [NSString stringWithFormat:@"%@", [paths objectAtIndex:0]];
    path=[path stringByDeletingLastPathComponent];
    path=[NSString stringWithFormat:@"%@/%@",path,str];
    [imgData writeToFile:path atomically:YES];
    [picker dismissModalViewControllerAnimated:YES];
    }