English 中文(简体)
压缩机率,但保持体积
原标题:compress UIImage but keep size

I try to use UIImageView to show the photo. But the Photo sometimes is a little large, and I want to compress it.But I d like to keep its size. For example,a photo is 4M and has a size of 320X480. And I want to compress it and it may have 1M but still has a size of 320X480.

感谢!

最佳回答

利用JPEG压缩压缩压缩电。

lowResImage = [UIImage imageWithData:UIImageJPEGRepresentation(highResImage, quality)];

质量在0.0至1.0之间

http://developer.apple.com/library/ios/#documentation/uikit/参比/UIImage_Class/Reference/Reference.html UIImage documentation, 一切都在上作了解释。

问题回答

如果你的目标是在具体数据长度以下获取图像,那么就很难猜测你需要什么样的压缩比率,除非你知道来源形象总是一定的数据长度。 这里采取简单易变的办法,用jpeg compression实现指标长度......请说1MB,来回答以下问题:

// sourceImage is whatever image you re starting with

NSData *imageData = [[NSData alloc] init];
for (float compression = 1.0; compression >= 0.0; compression -= .1) {
    imageData = UIImageJPEGRepresentation(sourceImage, compression);
    NSInteger imageLength = imageData.length;
    if (imageLength < 1000000) {
        break;
    }
}
UIImage *finalImage = [UIImage imageWithData:imageData];

我看到了一些办法,使用<代码>while,在达到目标规模之前,将图像压缩到9或以任何方式压缩,但我认为,你通过连续压缩/重造图像,正在失去图像质量和过程周期。 此外,<<>>>>>代号”此处的路段比较安全,因为它在试图尽量压缩后自动停止。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签