Search code examples
ioscompressionios-simulatoruiimagepickercontrollerjpeg

UIImagePicker image size is more than original


WorkGround

I have an image (.JPG format) of size of 53kb in simulator.

Now I am selecting this image using UIImagePickerController and save it to local.

Code for selecting image and storing it local is below:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[[UIApplication sharedApplication] setStatusBarHidden:YES];

NSString *mediaType = info[UIImagePickerControllerMediaType];

if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
    // Media is an image
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    if (image) {
        NSData *jpegData = UIImageJPEGRepresentation(image, 1);

    // Store image.
        BOOL isImageSave = [jpegData writeToFile:filePath atomically:NO];

        if (isImageSave) {
            DLog(@"Image saved");
        } else {
            DLog(@"Error");
        }
    }
}
[self.view dismissViewControllerAnimated:YES completion:nil];

}

Problem:

Here original image is of 53kb but when i select this image the image size will increase and store image is of size 142kb. Here i used compression to reduce size. But i check twice that in UIImagePickerController delegate method it returns bigger size image.

Does any one have idea, Why it select bigger image. Is there any way to get original size image in terms of size?


Solution

  • You can resize to image view in particular frame. using below code

    - (UIImage*)imageWithImage:(UIImage*)image 
                   scaledToSize:(CGSize)newSize;
    {
       UIGraphicsBeginImageContext( newSize );
       [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
       UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
       UIGraphicsEndImageContext();
    
       return newImage;
    }
    

    Then call this method as :

    UIImage *myIcon = [self imageWithImage:myUIImageInstance scaledToSize:CGSizeMake(20, 20)];
    

    As far as storage of the image, the fastest image format to use with the iPhone is PNG, because it has optimizations for that format. However, if you want to store these images as JPEGs, you can take your UIImage and do the following:

    NSData *dataForJPEGFile = UIImageJPEGRepresentation(theImage, 0.6);