压缩从设备中获取的图像或从库,iOS中选择的图像

时间:2012-04-02 07:33:20

标签: ios image compression

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

- (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倍。

上述方法有什么问题?我不能将图像压缩到25KB?

任何帮助,建议都将不胜感激。

3 个答案:

答案 0 :(得分:4)

这是一个替代答案,我认为它更简洁。它来自堆栈溢出的另一个答案,但已经在我的代码中存在了一段时间。我正在获得~15kB大小的图像,这是通过4s(2.5k×3.5k像素)相机拍摄的标准图像调整大小到更有用的分辨率:(400乘600)。然后压缩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

谢谢!我希望这有助于其他人!当您尝试执行从服务器加载图像的QUICK集合视图加载时,此问题很重要。

答案 1 :(得分:3)

夫特:

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)!
}

答案 2 :(得分:1)

我认为以下代码可以帮助您做您想做的事情,请在您的应用中使用之前删除我的一些代码。

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

这肯定会解决您的图像问题,使这种常用方法可以在多个视图中使用...

相关问题