Overall memory growth
Whenever i get memory warning i'm clearing caches
func applicationDidReceiveMemoryWarning(_ application: UIApplication) {
print("Memory warning...")
imageCache.countLimit = 0
imageCache.totalCostLimit = 0
imageCache.removeAllObjects()
SDImageCache.shared.clearMemory()
URLCache.shared.removeAllCachedResponses()
}
This is the code for downloading images and video thumbnails
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)))
}
}
- To download the image I'm using image slideshow 3rd party library
To download thumbnail I'm using the following code,
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()) } } } }
If i comment following code, the app will not crash
image_url.append(AlamofireSource(url: url , placeholder: UIImage(named: AppConstants.image_placeholder)))