压缩图像以减小文件大小

时间:2013-09-08 08:50:19

标签: ios compression jpeg image-size

我正在构建一个应用程序,让用户拍照或从iPhone上的库中选择一个,并将其上传到Parse后端。

我面临的问题是文件的大小。

我已经读过像Facebook,Twitter,Instagram和Google这样的大型玩家在分辨率和文件大小方面所做的事情,但我无法接近这一点。

我确信他们拥有最好的代码和工具,但我很乐意用iOS常规流程尽可能好地实现它。

这就是我现在正在做的事情:

- (UIImage *)normalResImageForAsset:(ALAsset*)asset
{
    // Convert ALAsset to UIImage
    UIImage *image = [self highResImageForAsset:asset];

    // Determine output size
    CGFloat maxSize = 1024.0f;
    CGFloat width = image.size.width;
    CGFloat height = image.size.height;
    CGFloat newWidth = width;
    CGFloat newHeight = height;

    // If any side exceeds the maximun size, reduce the greater side to 1200px and proportionately the other one
    if (width > maxSize || height > maxSize) {
        if (width > height) {
            newWidth = maxSize;
            newHeight = (height*maxSize)/width;
        } else {
            newHeight = maxSize;
            newWidth = (width*maxSize)/height;
        }
    }

    // Resize the image
    CGSize newSize = CGSizeMake(newWidth, newHeight);
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    // Set maximun compression in order to decrease file size and enable faster uploads & downloads
    NSData *imageData = UIImageJPEGRepresentation(newImage, 0.0f);
    UIImage *processedImage = [UIImage imageWithData:imageData];

    return processedImage;
}

我正在努力使1024px达到最大允许大小(两者都有ot height)以在那里开始一些限制,然后我应用最大压缩来减小大小。

这可以工作并削减大约50%的图像尺寸,而不会真正损坏JPEG,但它仍然很多。特别是如果使用手机的相机拍摄照片并上传。处理后的图像仍然可以轻松拥有1MB大小,这太过分了。

我猜我可能会错过一些有用的步骤或使用错误的技巧。

非常感谢任何反馈。

3 个答案:

答案 0 :(得分:9)

我有类似的问题,我也认为压缩不起作用。事实证明,在我的代码中,我正在使用不同的压缩将文件写入磁盘。您可能对此函数返回的数据执行相同的操作。检查压缩是否有效的一个好方法是做这样的事情:

NSData *imgData1 = UIImageJPEGRepresentation(newImage, 1.0f);
NSLog(@"1.0 size: %d", imgData1.length);

NSData *imgData2 = UIImageJPEGRepresentation(newImage, 0.7f);
NSLog(@"0.7 size: %d", imgData2.length);

NSData *imgData3 = UIImageJPEGRepresentation(newImage, 0.4f);
NSLog(@"0.4 size: %d", imgData3.length);

NSData *imgData4 = UIImageJPEGRepresentation(newImage, 0.0f);
NSLog(@"0.0 size: %d", imgData4.length);

// Don't convert NSData back to UIImage before writing to disk
[imgData4 writeToFile:imagePath atomically:YES];

我正在使用640x480的图像,我的文件大小从325 kB(对于1.0)到18 kB(对于0.0)

答案 1 :(得分:0)

@codeMonkey的Swift-3版本: -

在图像压缩方面有效完美

func compressImage() -> UIImage {

        let oldImage = UIImage(named: "background.jpg")
        var imageData =  Data(UIImagePNGRepresentation(oldImage!)! )
        print("***** Uncompressed Size \(imageData.description) **** ")

        imageData = UIImageJPEGRepresentation(oldImage!, 0.025)!
        print("***** Compressed Size \(imageData.description) **** ")

        let image = UIImage(data: imageData)
        return image!

    }

enter image description here

答案 2 :(得分:0)

Please Try below answer.

+(UIImage *)compressImage:(UIImage *)image{
float actualHeight = image.size.height;
float actualWidth = image.size.width;
float maxHeight = 1136.0f;
float maxWidth = 640.0f;
float imgRatio = actualWidth/actualHeight;
float maxRatio = maxWidth/maxHeight;
float compressionQuality = 1;//50 percent compression

if (actualHeight > maxHeight || actualWidth > maxWidth){
    if(imgRatio < maxRatio){
        //adjust width according to maxHeight
        imgRatio = maxHeight / actualHeight;
        actualWidth = imgRatio * actualWidth;
        actualHeight = maxHeight;
    }
    else if(imgRatio > maxRatio){
        //adjust height according to maxWidth
        imgRatio = maxWidth / actualWidth;
        actualHeight = imgRatio * actualHeight;
        actualWidth = maxWidth;
    }
    else{
        actualHeight = maxHeight;
        actualWidth = maxWidth;
    }
}else{
    actualHeight = maxHeight;
    actualWidth = maxWidth;
    compressionQuality = 1;
}

CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
UIGraphicsBeginImageContext(rect.size);
[image drawInRect:rect];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
NSData *imageData = UIImageJPEGRepresentation(img, compressionQuality);
UIGraphicsEndImageContext();

return [UIImage imageWithData:imageData];
}