按大小压缩图像 - iPhone SDK [英] image compression by size - iPhone SDK

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

问题描述

我想压缩图像(相机/照片库),然后将其发送到服务器.我知道我可以按高度和宽度压缩,但我只想按大小将图像压缩到固定大小(200 KB)并保持原始高度和宽度.JPEGRepresentation 中的比例因子不代表大小,只代表压缩质量.如何在不使用任何第三方库的情况下实现这一点(压缩到固定大小)?感谢您的帮助.

I would like to compress images (camera/photo library) and then send it to the server. I know I can compress by height and width, but I would like to compress the images by size to a fixed size (200 KB) only and keep the original height and width. The scale factor in JPEGRepresentation does not represent the size and only compression quality. How can I achieve this (compress to a fixed size) without using any third party library? Thanks for any help.

推荐答案

这里有一些示例代码,它们将尝试为您压缩图像,使其不超过最大压缩率或最大文件大小

Heres some example code that will attempt to compress an image for you so that it doesn't exceed either a max compression or maximum file size

CGFloat compression = 0.9f;
CGFloat maxCompression = 0.1f;
int maxFileSize = 250*1024;

NSData *imageData = UIImageJPEGRepresentation(yourImage, compression);

while ([imageData length] > maxFileSize && compression > maxCompression)
{
    compression -= 0.1;
    imageData = UIImageJPEGRepresentation(yourImage, compression);
}

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

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