【发布时间】:2019-08-07 03:12:40
【问题描述】:
我需要从 URL 加载图像并将它们存储在本地,这样就不必一遍又一遍地重新加载它们。我正在开发这个扩展:
扩展 UIImage { 函数加载(图像图像名称:字符串)-> UIImage { // 声明图片位置 让 imagePath: String = "\(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])/\(imageName).png" 让 imageUrl: URL = URL(fileURLWithPath: imagePath) // 检查图像是否已经存储 如果 FileManager.default.fileExists(atPath: imagePath), 让 imageData: 数据 = 试试?数据(内容:imageUrl), 让图像:UIImage = UIImage(数据:imageData,比例:UIScreen.main.scale){ 返回图片 } // 图像尚未创建:创建、存储、返回 做 { 让 url = URL(string: eventInfo!.bannerImage)! 让数据=尝试数据(contentsOf:url) 让loadedImage:UIImage = UIImage(数据:数据)! } 抓住{ 打印(错误) } 让 newImage: UIImage = 尝试? UIImagePNGRepresentation(loadedImage)?.write(to: imageUrl) 返回新图像 } }我遇到了一个问题,UIImagePNGRepresentation 中的“loadedImage”返回错误“使用未解析的标识符加载图像”。我的目标是在本地存储图像的 PNG 表示。对此错误的任何建议将不胜感激。
【问题讨论】: