iOS 使用Image I/O 实现超大图片降采样

一个图片解码成未压缩的位图的时候,占用的内存和图片的文件大小没有关系,和图片的尺寸大小有关系,尺寸越大,所需要的像素点越多,所以超大图片,我们需要进行优化处理

这里直接上降采样代码

给UIImage 添加一个分类,在分类中添加一个方法 ,该size就是

UIImageView的大小

复制代码
- (UIImage *)resizeCG:(CGSize)size
{
    CGFloat maxPixelSize = MAX(size.width, size.height);
    CGImageSourceRef sourceRef = CGImageSourceCreateWithData((__bridge CFDataRef)UIImageJPEGRepresentation(self, 0.9), nil);
    NSDictionary *options = @{(__bridge id)kCGImageSourceCreateThumbnailFromImageAlways:(__bridge id)kCFBooleanTrue,
                              (__bridge id)kCGImageSourceThumbnailMaxPixelSize:[NSNumber numberWithFloat:maxPixelSize]
                              };
    CGImageRef imageRef = CGImageSourceCreateImageAtIndex(sourceRef, 0, (__bridge CFDictionaryRef)options);
    UIImage *newImage = [UIImage imageWithCGImage:imageRef scale:2 orientation:self.imageOrientation];
    CGImageRelease(imageRef);
    CFRelease(sourceRef);
    return newImage;
}
相关推荐
茶底世界之下1 天前
为什么预览、录制、离线导出不能共享同一个背压策略?
ios·swift
2501_916007471 天前
苹果商店iOS应用上架费用全面解析:开发者计划与审核费用计算
android·ios·小程序·https·uni-app·iphone·webview
2501_916008891 天前
如何实现iOS App代码混淆:SwiftShield使用指南
android·ios·小程序·https·uni-app·iphone·webview
2501_915909061 天前
移动端开发工具链怎么组合比较好,iOS、Android、跨端三套配置方案
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
2501_916008891 天前
怎么用 Godot 导出 iOS 应用并上架 App Store?签名字段该填什么?
android·ios·小程序·https·uni-app·iphone·webview
2501_915918412 天前
iOS编程用什么软件好?主流工具Xcode、AppCode、CodeRunner与新兴快蝎对比
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
终端安全笔记2 天前
iOS 27 之后「策略空转」:设备升级不报错,但旧策略不再管它
android·网络·安全·ios·智能手机
9765033352 天前
iOS 上架 4.3a 被拒【uniapp专讲】
flutter·ios·objective-c·uniapp·swift
黑化旺仔3 天前
【OC】KVO
macos·ios·objective-c·cocoa
HouWan3 天前
SwiftUI 小技巧:如何读取 SwiftUI Font 的详细信息
ios·swiftui·apple