压缩图像以减小文件大小 [英] Compress images to reduce file size

查看:187
本文介绍了压缩图像以减小文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个应用程式,让使用者在iPhone上从相片库中选取相片或上传到「Parse」后端。

I'm building an app that lets the user take a photo or select one from the library on the iPhone and upload it to the Parse backend.

我面对的是文件的大小。

The problem I'm facing is regarding to the size of the file.

我已经阅读了关于解决方案和文件的Facebook,Twitter,Instagram和Google做的大玩家

I've read about what big players like Facebook, Twitter, Instagram, and Google do regarding resolution and file size but I can't get close to that.

我确定他们有最好的代码和工具,但我会很乐意实现它

I'm sure they have the best code and tools to do that but I'll be happy to implement it as good as possible with iOS regular processes.

这是我现在正在做的:

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



我尝试让1024像素的最大允许大小)在那里开始一些限制,然后我应用最大压缩,以减小大小。

I'm trying to make 1024px the maximun allowed size (both with ot height) to start some restrictions there and then I'm applying maximun compression to reduce size.

这可以工作和大约50%的图像大小,而不会真正损害JPEG,但它还是很多。特别是如果照片是用手机的相机拍摄并上传的。处理的图片仍然可以容易地有1MB的大小,这是太多了。

This works and cuts aproximately 50% of the image size without really damaging JPEGs but it's still a lot. Specially if photos are taken with the phone's camera and uploaded. The processed image can still easly have 1MB size which is way too much.

我猜我可能会缺少一些有用的步骤或使用错误的技术。

I'm guessing that I could be missing some useful step or using the wrong technique.

任何反馈都将非常感谢。

Any feedback would be greatly appreciated.

推荐答案

类似的问题,我也认为压缩不工作。它在我的代码中的其他地方,我正在使用不同的压缩写入文件到磁盘。你可能对这个函数返回的数据做同样的事情。检查压缩确实有效的一个好方法是做这样的事情:

I had a similar problem and I also thought that the compression wasn't working. It turned out elsewhere in my code I was writing the file out to disk using different compression. You might be doing the same thing with the data this function returns. A good way to check that indeed the compression is effective is to do something like this:

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 )降至18 kB(0.0)

I'm using an image that is 640x480 and I get file sizes ranging from 325 kB (for 1.0) down to 18 kB (for 0.0)

这篇关于压缩图像以减小文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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