我在我的應用程序中有一個UICollectionView,每個單元格都是UIImageView和一些文本標籤。問題是,當我有UIImageViews顯示他們的圖像時,滾動性能是可怕的。它遠不如UITableView的滾動體驗,甚至沒有UIImageView的UICollectionView。可憐的UICollectionView使用UIImage滾動性能
我發現this question從幾個月前,似乎找到了答案,但它是用RubyMotion編寫的,我不明白這一點。我試圖看看如何將其轉換爲Xcode,但由於我從未使用過NSCache,所以有點難。那裏的海報也指出here有關除了他們的解決方案之外的其他實現,但我不知道該把代碼放在哪裏。可能是因爲我不明白first question的代碼。
有人能幫助翻譯成Xcode嗎?
def viewDidLoad
...
@images_cache = NSCache.alloc.init
@image_loading_queue = NSOperationQueue.alloc.init
@image_loading_queue.maxConcurrentOperationCount = 3
...
end
def collectionView(collection_view, cellForItemAtIndexPath: index_path)
cell = collection_view.dequeueReusableCellWithReuseIdentifier(CELL_IDENTIFIER, forIndexPath: index_path)
image_path = @image_paths[index_path.row]
if cached_image = @images_cache.objectForKey(image_path)
cell.image = cached_image
else
@operation = NSBlockOperation.blockOperationWithBlock lambda {
@image = UIImage.imageWithContentsOfFile(image_path)
Dispatch::Queue.main.async do
return unless collectionView.indexPathsForVisibleItems.containsObject(index_path)
@images_cache.setObject(@image, forKey: image_path)
cell = collectionView.cellForItemAtIndexPath(index_path)
cell.image = @image
end
}
@image_loading_queue.addOperation(@operation)
end
end
下面是從second question代碼的first question的提問者說解決了這個問題:
UIImage *productImage = [[UIImage alloc] initWithContentsOfFile:path];
CGSize imageSize = productImage.size;
UIGraphicsBeginImageContext(imageSize);
[productImage drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
productImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
同樣,我不知道如何/在哪裏實現這一點。
非常感謝。
發佈您的代碼!你有什麼嘗試?爲什麼你的應用程序的性能變慢?分析它看看發生了什麼的結果是什麼? – 2013-04-03 22:49:55
這不是我的整個應用程序性能低下,它只是滾動UICollectionView中的單元格。該應用程序的其餘部分表現非常好。我目前使用'cell.cardImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:tempCard]];'設置圖像。正在使用的圖像不是應用程序包的一部分,但通常會下載並存儲在Caches文件夾中。 – Nick 2013-04-04 02:11:48