iOS 图片压缩
2016-01-18 11:24
489 查看
//压缩图片质量 +(UIImage *)reduceImage:(UIImage *)image percent:(float)percent { NSData *imageData = UIImageJPEGRepresentation(image, percent); UIImage *newImage = [UIImage imageWithData:imageData]; return newImage; } //压缩图片尺寸 + (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize { // Create a graphics image context UIGraphicsBeginImageContext(newSize); // new size [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; // Get the new image from the context UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); // End the context UIGraphicsEndImageContext(); // Return the new image. return newImage; } //上面的方法比较常见,可是需要加载到内存中来处理图片,当图片数量多了的时候就会收到内存警告,程序崩溃。研究半天终于在一篇博客中找到了解决方法: [objc] view plaincopy 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. - (UIImage *)thumbnailForAsset:(ALAsset *)asset maxPixelSize:(NSUInteger)size { NSParameterAssert(asset != nil); NSParameterAssert(size > 0); ALAssetRepresentation *rep = [asset defaultRepresentation]; CGDataProviderDirectCallbacks callbacks = { .version = 0, .getBytePointer = NULL, .releaseBytePointer = NULL, .getBytesAtPosition = getAssetBytesCallback, .releaseInfo = releaseAssetCallback, }; CGDataProviderRef provider = CGDataProviderCreateDirect((voidvoid *)CFBridgingRetain(rep), [rep size], &callbacks); CGImageSourceRef source = CGImageSourceCreateWithDataProvider(provider, NULL); CGImageRef imageRef = CGImageSourceCreateThumbnailAtIndex(source, 0, (__bridge CFDictionaryRef) @{ (NSString *)kCGImageSourceCreateThumbnailFromImageAlways : @YES, (NSString *)kCGImageSourceThumbnailMaxPixelSize : [NSNumber numberWithInt:size], (NSString *)kCGImageSourceCreateThumbnailWithTransform : @YES, }); CFRelease(source); CFRelease(provider); if (!imageRef) { return nil; } UIImage *toReturn = [UIImage imageWithCGImage:imageRef]; CFRelease(imageRef); return toReturn; }
相关文章推荐
- H5页面打开自动播放,H5加载,H5和iOS 交互
- iOS 中 NSParameterAssert
- 2016苹果开发者企业号申请流程详解
- iOS 十大控件
- iOS label button等设置不同颜色的标题
- ios动画效果
- iOS 收回键盘的3个方法
- IOS支付宝支付出现6002问题的解决办法
- IOS将字符串去掉斜杠,并转化成NSData数据后,再解析成字典
- iOS The data couldn't be read because it isn't in the correct format
- iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)
- iOS之旅--隐藏(去除)导航栏底部横线
- IOS开发中(null)与<null>的处理
- 盗墓笔记(iOS新特性) 2016-01-18
- iOS9新特性Bitcode
- iOS开发之Masonry(二)
- iOS左是master,右边是detail
- 不让苹果开发者账号折磨我
- iOS 遍历字符串得到每一个字
- iOS runtime 消息分发过程