整体内存增长
每当我收到内存警告时,我都会清除缓存
func applicationDidReceiveMemoryWarning(_ application: UIApplication) {
print("Memory warning...")
imageCache.countLimit = 0
imageCache.totalCostLimit = 0
imageCache.removeAllObjects()
SDImageCache.shared.clearMemory()
URLCache.shared.removeAllCachedResponses()
}
这是下载图像和视频缩略图的代码
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
image_url = [InputSource]()
if item.media_url?.contains(".mp4") ?? false
{
DispatchQueue.global().async {
UIImage.getThumbnailImage(forUrl: url) { (image) in
DispatchQueue.main.async {
self.imageView_Video.image = image
}
}
}
}
else{
image_url.append(AlamofireSource(url: url , placeholder: UIImage(named: AppConstants.image_placeholder)))
}
}
- 要下载图像,我正在使用图像幻灯片 3rd 方库
要下载缩略图,我使用以下代码,
static func getThumbnailImage(forUrl url: URL, completion: @escaping(_ image: UIImage) -> Void) { DispatchQueue.global().async { do{ let asset = AVURLAsset(url: url, options: nil) let imgGenerator = AVAssetImageGenerator(asset: asset) imgGenerator.appliesPreferredTrackTransform = true let cgImage = try imgGenerator.copyCGImage(at: CMTimeMake(value: 0, timescale: 1), actualTime: nil) DispatchQueue.main.async { let image = UIImage(cgImage: cgImage) completion(image) } } catch let error{ print("*** Error generating thumbnail: \(error.localizedDescription)") DispatchQueue.main.async { completion(UIImage()) } } } }
如果我评论以下代码,应用程序不会崩溃
image_url.append(AlamofireSource(url: url , placeholder: UIImage(named: AppConstants.image_placeholder)))