Compressing image taken from device or chosen from

2019-04-10 08:21发布

问题:

In my app, user is allowed to take a photo from a device or upload it from the library to set it as his profile pic. Now when user is done, I need to upload that image to server. Usually the image taken from device is of size 5-6MB. I need it to compress to 25KB before uploading to server. So I am using following method to that

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissModalViewControllerAnimated:YES];
    [picker release];
    profilePicImageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    NSData* pngData = UIImagePNGRepresentation(profilePicImageView.image);
    NSLog(@"image size of png is %d", [pngData length]);
    NSData *imageData = UIImageJPEGRepresentation(profilePicImageView.image, 0.00001);
    NSLog(@"image size is %d", [imageData length]);
}

Problem with this method is, no matter how small I set the scaling parameter, compressed file size does not drop below ~230KB, which is nearly 10 times higher than what I need.

What is wrong with the above method? Can't I compress image to 25KB?

Any help, suggestion will be greatly appreciated.

回答1:

I think Below code may help you to do what you want, please remove some of my code before using in your app.

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
  [appDelegate.objList setHidden:FALSE];    
  [self dismissModalViewControllerAnimated:YES];

UIImage *capturedImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[self performSelector:@selector(waitUntillImageCaptured:) withObject:capturedImage afterDelay:0.2];
}

-(void)waitUntillImageCaptured:(UIImage *)originalImage
{
UIImage *tempimg = originalImage;
//UIImageWriteToSavedPhotosAlbum(tempimg,nil,nil,nil);

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

CGSize newSize = CGSizeMake(320, 480);
UIGraphicsBeginImageContext(newSize);

[tempimg drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Get the new image from the context
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
// End the context
UIGraphicsEndImageContext();

NSString *strUser = [NSString stringWithFormat:@"%@",[imgDictName objectForKey:@"UsersName"]];
strUser = [strUser stringByReplacingOccurrencesOfString:@" " withString:@""];
[strUser retain];
NSString * strUserLocal = [strUser stringByAppendingString:@"temp.png"];

NSString *imagePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:strUserLocal];
UIImageView *tempView = [[UIImageView alloc] initWithImage:newImage];
NSData *data = [NSData dataWithData:UIImagePNGRepresentation(tempView.image)];
[tempView release];

if(data != nil) {
    [data writeToFile:imagePath atomically:YES];
}
else{
    [[NSFileManager defaultManager] removeItemAtPath:imagePath error:NULL];
}

[pool release];

[self performSelector:@selector(thumbWithSideOfLength:) withObject:strUser afterDelay:0.3];
}

- (void)thumbWithSideOfLength:(NSString*)strImgName 
{    
[self onEditing];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *fullPathToThumbImage = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@temp.png", strImgName]];

UIImage *thumbnail;

UIImage *mainImage = [UIImage imageWithContentsOfFile:fullPathToThumbImage];

UIImageView *mainImageView = [[UIImageView alloc] initWithImage:mainImage];

CGRect clippedRect = CGRectMake(0, 0, mainImage.size.width,  mainImage.size.height);

CGFloat scaleFactor = 0.8;

UIGraphicsBeginImageContext(CGSizeMake(mainImage.size.width * scaleFactor, mainImage.size.height * scaleFactor));
CGContextRef currentContext = UIGraphicsGetCurrentContext();

CGContextClipToRect(currentContext, clippedRect);   

CGContextScaleCTM(currentContext, scaleFactor, scaleFactor);

[mainImageView.layer renderInContext:currentContext];

thumbnail = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(thumbnail);

[imageData writeToFile:fullPathToThumbImage atomically:YES];
userImageView.backgroundColor = [UIColor clearColor];
[userImageView imageFromImagePath:fullPathToThumbImage];

[mainImageView release];
}

This will surely solve your issues with image make this common method to use in multiple views...



回答2:

Here is an alternative answer, and I think it is a lot more concise. It is from another answer on stack overflow but has been in my code for a while. I am getting ~15kB size images and this is with a resize from standard image taken with camera on 4s (2.5k by 3.5k pixels) to a more useful resolution: (400 by 600). Then compression UIImageJPEGRepresentation(pic,0.1).

CGRect rect = CGRectMake(0,0,capturedImage.size.width/6,capturedImage.size.height/6);
UIGraphicsBeginImageContext( rect.size );
[capturedImage drawInRect:rect];
UIImage *picture1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSData *imageDataForResize = UIImageJPEGRepresentation(picture1,0.1);
UIImage *img=[UIImage imageWithData:imageDataForResize];
capturedImage = img; // reassigning to original image as to prevent changing things later in the code

Thanks! I hope this helps other people! This issue is important when you are trying to do QUICK collection view loads where images are loaded from a server.



回答3:

Swift:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
    var capturedImage = info[UIImagePickerControllerEditedImage] as! UIImage
    let rect = CGRectMake(0, 0, capturedImage.size.width/6, capturedImage.size.height/6)
    UIGraphicsBeginImageContext(rect.size)
    capturedImage.drawInRect(rect)
    let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    let compressedImageData = UIImageJPEGRepresentation(resizedImage, 0.1)
    capturedImage = UIImage(data: compressedImageData)!
}