[英]Upload image in imageview inside collectionView using sdwebimage
[英]Crazy Memory Issues using SDWebImage in a CollectionView (inside a tableviewCell)
我一開始使用SDWebImage填充的圖像視圖滾動集合視圖,就無法弄清楚為什么我的應用程序的內存會迅速上升。 如果尚未下載,則可以暫時使用。 但是一旦下載完每個圖像,如果我什至不觸摸滾動,應用程序ui都將變得無響應,並且內存飛速上升(超過1GB),不僅導致應用程序崩潰,而且整個操作系統崩潰並重新啟動。
我有一個包含幾個單元格的tableview,其中一個具有可滾動的collectionview。 我有一個數據對象,可用於填充collectionViewCell中的所有標簽和圖像。 cellForItem非常復雜(許多if語句和switch語句),因此我嘗試將一些代碼從那里移出,然后將其移到我在collectionViewCell中設置的用於容納數據對象的屬性的didSet方法中。 然后,我只是將數據對象設置為itemForRow中的屬性(在后台線程中),該屬性觸發了單個collectionViewCell的didSet方法來填充標簽並使用sd_set_image方法設置圖像。 這是tableViewCell上的相關代碼:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
print(indexPath.row)
if itemStyle == .Grid {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: self.resourceCellIdentifier, for: indexPath) as! ResourceCollectionViewCell
cell.delegate = self
cell.tag = indexPath.row
if self.filter == Filter.Collections || self.resourceArray?[indexPath.row].actionContent == nil {
let resource: ActionContentTemplate?
if self.filter == Filter.Collections {
resource = self.collectionResourceArray?[indexPath.row].actionContentTemplate
} else {
resource = self.resourceArray?[indexPath.row].actionContentTemplate
}
DispatchQueue.global(qos: .userInitiated).async {
cell.dataObject = resource
}
return cell
} else {.....
這是CollectionViewCell本身的didSet方法:
var dataObject: ActionContentTemplate? {
didSet {
var cellRow: Int = 0
if self.actionContentTemplate != nil {
....
DispatchQueue.main.async {
cellRow = self.tag
.....
switch self.actionContentTemplate?.contentTypeID?.uintValue {
case ContentTypeVideo.rawValue,
ContentTypeVideoRecording.rawValue,
ContentTypeScreenRecording.rawValue:
.......
case ContentTypeAttachment.rawValue:
let fileType = (self.actionContentTemplate?.contentFileName as NSString?)?.pathExtension.lowercased()
var placeholderImage: UIImage
if fileType == "pdf" {
placeholderImage = UIImage(named: "pdf")!
} else if fileType == "doc" {
placeholderImage = UIImage(named: "doc")!
} else if fileType == "docx" {
placeholderImage = UIImage(named: "doc")!
} else if fileType == "xls" {
placeholderImage = UIImage(named: "xls")!
} else if fileType == "xlsx" {
placeholderImage = UIImage(named: "xls")!
} else if fileType == "ppt" {
placeholderImage = UIImage(named: "ppt")!
} else if fileType == "pptx" {
placeholderImage = UIImage(named: "ppt")!
} else {
placeholderImage = UIImage(named: "plain")!
}
if let urlString = self.actionContentTemplate?.thumbnailURL {
let imageURL = URL(string: urlString)
SDWebImageManager.shared().loadImage(with: imageURL, options: [.continueInBackground, .scaleDownLargeImages], progress: nil) { (image, data, err, cacheType, finished, url) in
DispatchQueue.main.async {
if cellRow == self.tag {
self.activityIndicator?.stopAnimating()
self.imageView.image = image
UIAnimation.fadeTransitionImageView(self.imageView)
}
}
}
}
} else {
DispatchQueue.main.async {
self.activityIndicator?.stopAnimating()
self.imageView.image = placeholderImage
}
}
break
.....
實際上我切換到loadImage方法,因為sd_setimage方法也不起作用。
SDWebImageManager.shared().loadImage(with: imageURL, options: [.continueInBackground, .scaleDownLargeImages], progress: nil) { (image, data, err, cacheType, finished, url) in
DispatchQueue.main.async {
if cellRow == self.tag {
//IF I COMMENT OUT THESE TWO LINES BELOW THE ISSUES STOP (I think it is only the self.imageView.image = image line that is causing issues)
// self.activityIndicator?.stopAnimating()
// self.imageView.image = image
UIAnimation.fadeTransitionImageView(self.imageView)
}
}
}
誰能向我解釋為什么會發生這種情況以及我可以采取什么措施來解決?
編輯-忘記在應用程序委托中將這些設置添加到問題:
//Settings for image caching
[[SDWebImageDownloader sharedDownloader] setShouldDecompressImages:NO];
[[[SDImageCache sharedImageCache] config] setShouldDecompressImages:NO];
[[[SDImageCache sharedImageCache] config] setShouldCacheImagesInMemory:NO];
[[[SDImageCache sharedImageCache] config] setMaxCacheSize:1024 * 1024 * 100];
[[[SDImageCache sharedImageCache] config] setMaxCacheAge:3600 * 24 * 7];
[[SDImageCache sharedImageCache] setMaxMemoryCost:1024 * 1024 * 15];
原來縮略圖很大,並且使用此鏈接( iOS Swift:如何正確縮小圖像? )將圖像調整為imageView大小,從而完全解決了內存問題。 但是,下載完圖像后,滾動會有點混亂。...我認為將盡可能多的計算代碼移至后台線程,並移出cellForItem會對此有所幫助,但我顯然遺漏了一些東西。 如果有人能指出正確的方向,那我將不勝感激,因為我一直在努力學習更多有關開發人員的知識。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.