简体   繁体   中英

Resize UIImage and imagePickerController Memory pressure?

App crashes After taking a picture using imagePickerController Here I am trying to rotate the image from imagepickercontroller

- (void)imagePickerController:(UIImagePickerController *)picker1 didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
if (chosenImage.imageOrientation!= UIImageOrientationUp) {
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size,self.view.opaque,0.0);
        [chosenImage drawInRect:(CGRect){0, 0,chosenImage.size}];
    chosenImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

}
UIImage *imageToDisplay =chosenImage;
}

It gives 10-MB image.How to compress it in very low size image in Kbs?

Just use something like this.

NSURL *url = info[UIImagePickerControllerReferenceURL];

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset) {
     ALAssetRepresentation *rep = [myasset defaultRepresentation];
     CGImageRef imageRef = [rep fullResolutionImage];
     if (imageRef) {
// here is your image
        chosenImage = [UIImage imageWithCGImage:iref];
     }
};

ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *error)
{
        NSLog(@"Error -> %@",[error localizedDescription]);
};

ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
        [assetslibrary assetForURL:url 
                       resultBlock:resultblock
                      failureBlock:failureblock];

Or you can use such code

CGSize newSize = CGSizeMake(1000.0f, 1000.0f);
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

That's will give you image with 1000*1000 px.

I've found the solution to compress the image

//Put this code in didfinish  UIimagepickercontroller
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
NSData *imgData= UIImageJPEGRepresentation(chosenImage,0.1);//0.1 is loosy compression
UIImage *compressedImage=[UIImage imageWithData:imgData];
UIImage *resizedImage=[self imageWithImage:compressedImage scaledToSize:CGSizeMake(290, 390)];

below code is to Resize the image

-(UIImage *)imageWithImage:(UIImage *)image scaledToSize:(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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM