iOS 图片压缩

2017-09-06  本文已影响26人  LiwaySun

//压缩图片质量
+(UIImage *)reduceImage:(UIImage *)image percent:(float)percent
{
NSData *imageData = UIImageJPEGRepresentation(image, percent);
UIImage *newImage = [UIImage imageWithData:imageData];
return newImage;
}
//压缩图片尺寸

上面的方法比较常见,可是需要加载到内存中来处理图片,当图片数量多了的时候就会收到内存警告,程序崩溃。研究半天终于在一篇博客中找到了解决方法:

static size_t getAssetBytesCallback(voidvoid *info, voidvoid *buffer, off_t position, size_t count) {
ALAssetRepresentation *rep = (__bridge id)info;

NSError *error = nil;  
size_t countRead = [rep getBytes:(uint8_t *)buffer fromOffset:position length:count error:&error];  
  
if (countRead == 0 && error) {  
    // We have no way of passing this info back to the caller, so we log it, at least.  
    NDDebug(@"thumbnailForAsset:maxPixelSize: got an error reading an asset: %@", error);  
}  
  
return countRead;  

}

static void releaseAssetCallback(voidvoid *info) {
// The info here is an ALAssetRepresentation which we CFRetain in thumbnailForAsset:maxPixelSize:.
// This release balances that retain.
CFRelease(info);
}

// Returns a UIImage for the given asset, with size length at most the passed size.
// The resulting UIImage will be already rotated to UIImageOrientationUp, so its CGImageRef
// can be used directly without additional rotation handling.
// This is done synchronously, so you should call this method on a background queue/thread.

上一篇下一篇

猜你喜欢

热点阅读