Reputation: 246
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 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)))
Upvotes: 1
Views: 628
Reputation: 246
I resolved this issue by changing the downloading image method as following,
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
if let medias = history?.medias
{
if medias.count > 0
{
slides = [];
for item in medias{
if let url = URL(string: item.media_url ?? " "){
if item.media_url?.contains(".mp4") ?? false
{
self.imageView_Video.backgroundColor = UIColor.black.withAlphaComponent(0.8)
DispatchQueue.global().async {
if let cachedImage = AppDelegate.shared.imageCache.object(forKey: "\(url)" as NSString) {
DispatchQueue.main.async {
self.imageView_Video.image = cachedImage
}
}else{
UIImage.getThumbnailFromUrl("\(url)") { [weak self] (image) in
DispatchQueue.main.async {
if let img = image{
AppDelegate.shared.imageCache.setObject(img, forKey: "\(url)" as NSString? ?? NSString())
self?.imageView_Video.image = img
}
}
}
}
}
}
else{
let slide1:Slide = Bundle.main.loadNibNamed("Slide", owner: self, options: nil)?.first as! Slide
let processor = DownsamplingImageProcessor(size: CGSize(width: 500, height: 500))
|> RoundCornerImageProcessor(cornerRadius: 0)
slide1.imageView?.kf.setImage(
with: url,
placeholder: UIImage(named: "i_placeholder"),
options: [
.processor(processor),
])
{ [weak self]
result in
switch result {
case .success(let value):
self?.slides.append(slide1)
case .failure(let error):
slide1.imageView?.image = UIImage(named: "i_placeholder")
self?.slides.append(slide1)
}
self?.setupSlideScrollView(slides: self?.slides ?? [])
}
}//end else
}//end if let url
}
}
}
}
I removed the Slideshow library and added kingfisher to download high-resolution images.
Upvotes: 0