压缩从设备或从库,iOS中选择的图像 [英] Compressing image taken from device or chosen from library, iOS

查看:112
本文介绍了压缩从设备或从库,iOS中选择的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程式中,使用者可以从装置上拍摄相片,或是从图片库上传相片,将其设为个人资料相片。现在当用户完成后,我需要将该图像上传到服务器。通常从设备获取的图像大小为5-6MB。我需要它压缩到25KB,然后上传到服务器。所以我使用以下方法

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]);
}

这个方法的问题是,无论我设置缩放参数多么小,压缩文件大小不会降到〜230KB以下,这是我需要的高出将近10倍。

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.

上面的方法有什么问题?不能压缩图像到25KB?

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

任何帮助,建议将非常感激。

Any help, suggestion will be greatly appreciated.

推荐答案

我认为下面的代码可能会帮助你做你想要的,请在使用你的应用程序之前删除一些代码。

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];
}

这将确保解决您的问题与图像使这种常见的方法在多个views ...

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

这篇关于压缩从设备或从库,iOS中选择的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆