【发布时间】:2017-03-24 07:24:15
【问题描述】:
我用UIImage *image = [UIImage imageWithData: data],但是图片大小大了四倍。原来data.length=100 KB,但是这个image现在多了400 KB。你可能会说用压缩完成。但我比压缩后通过以下方法data.length=200 KB。
以下是我的方式
压缩方式
// spend is size (KB)
- (NSData *)compressionImageData:(NSData *)data spend:(CGFloat)spend {
if (data.length < spend) {
return data; //no compression
}
UIImage *sourceImg = [UIImage imageWithData:data];
CGFloat compression = 0.9f;
CGFloat maxCompression = 0.1f;
NSData *imageData = UIImageJPEGRepresentation(sourceImg, compression);
while ([imageData length] > spend && compression > maxCompression) {
compression -= 0.1;
imageData = UIImageJPEGRepresentation(sourceImg, compression);
}
return imageData;
}
图片下载方法:
- (void)loadPosterImage:(NSString *)imgURL {
weakSelf();
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:imgURL]
options:SDWebImageDownloaderLowPriority
progress:^(NSInteger receivedSize, NSInteger expectedSize) {}
completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
strongSelf();
// image is through [UIImage imageWithData: data]
self.posterImage = image;
self.posterData = data;
}];
}
图片大小超过 500 KB
data.lenght = 100KB
这是图片的data.length
这是图片的图片
【问题讨论】:
-
有没有办法把数据直接放入图片大小和data.length大小
-
你想达到什么目的?
-
你想做什么?
-
我要的是“data.lenght=100KB”。而不是457KB大小的图片图片
标签: ios objective-c uiimage nsdata