开发中,通常需要用到使用选取多张图片的功能,但是高清大图很吃内存,我想到的处理方案就是拿到高清大图的时候,重新绘制一张小的图片使用.至于清晰度尚可,至少我是分辨不出多大区别.
基本思路就是先固定宽,然后根据宽高比重新绘制一张新图片使用,大致代码如下:
为UIImage写一个extention,方便调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import UIKit extension UIImage { //根据传入的宽度生成一张按照宽高比压缩的新图片 func imageWithScale ( width : CGFloat ) - > UIImage { //1.根据 宽度 计算高度 let height = width * size . height / size . width //2.按照宽高比绘制一张新的图片 let currentSize = CGSize . init ( width : width , height : height ) UIGraphicsBeginImageContext ( currentSize ) //开始绘制 draw ( in : CGRect . init ( origin : CGPoint . zero , size : currentSize )) let newImage = UIGraphicsGetImageFromCurrentImageContext () UIGraphicsEndImageContext () //结束上下文 return newImage ! } } |
控制器代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | import UIKit var identifier = "cell" private var imgAry = [ UIImage ]() class ViewController : UIViewController { @IBOutlet weak var btn : UIButton ! @IBOutlet weak var colectionView : UICollectionView ! override func viewDidLoad () { super . viewDidLoad () colectionView . register ( UICollectionViewCell . self , forCellWithReuseIdentifier : identifier ) colectionView . delegate = self colectionView . dataSource = self // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning () { super . didReceiveMemoryWarning () // Dispose of any resources that can be recreated. } //选择 按钮点击事件 弹出相册 @IBAction func btnAction ( _ sender : UIButton ) { let vc = UIImagePickerController () vc . delegate = self present ( vc , animated : true , completion : nil ) } } extension ViewController : UICollectionViewDelegate , UICollectionViewDataSource , UINavigationControllerDelegate , UIImagePickerControllerDelegate { public func collectionView ( _ collectionView : UICollectionView , numberOfItemsInSection section : Int ) - > Int { return imgAry . count } public func collectionView ( _ collectionView : UICollectionView , cellForItemAt indexPath : IndexPath ) - > UICollectionViewCell { let cell = colectionView . dequeueReusableCell ( withReuseIdentifier : identifier , for : indexPath ) let imgView = UIImageView . init ( frame : CGRect . init ( x : 0 , y : 0 , width : 100 , height : 100 )) if ( imgAry . count > 0 ){ imgView . image = imgAry [ indexPath . item ] } cell . addSubview ( imgView ) return cell } //选择图片 public func imagePickerController ( _ picker : UIImagePickerController , didFinishPickingMediaWithInfo info : [ String : Any ]){ let image = info [ "UIImagePickerControllerOriginalImage" ] as ! UIImage //选择的图片 let newImage = image . imageWithScale ( width : 500 ) //按照宽为500的宽高比给图片重新绘制新的图片 imgAry . append ( newImage ) colectionView . reloadData () picker . dismiss ( animated : true , completion : nil ) } } |
占用内存情况如下:
未使用照片:25.7 MB
使用未压缩的照片: 333.1MB
使用压缩之后的照片:53.9MB
demo源码:https://github.com/pheromone/swift-imagePicker-memory
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探