簡體   English   中英

使用資產目錄和Xcode在UICollectionView中管理800張圖像

[英]Manage 800 images using asset catalogs with Xcode and in an UICollectionView

我正在創建帶有自定義圖像的表情符號鍵盤,它具有845個不同的圖像(如果包含@ 1x,@ 2x和@ 3x,則為2535)。 將它們作為資產導入到Xcode后,現在編譯我的程序的速度變得異常慢,並且需要3秒鍾才能在iOS Simulator中顯示鍵盤。

我用來顯示表情符號的UICollectionView也有問題:滾動時會崩潰。 我試圖將我的UICollectionViewCell限制為一個UIView,其中背景是表情符號,因此我沒有創建UIImageView,但是性能仍然很差。

這是我的cellForItemAtIndexPath()函數:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as UICollectionViewCell
    let emoji = UIImage(named: emojiCollections[collectionUsed][positionInCollection]) // Creating the UIImage with the asset
    let emojiBackgroundColor = UIColor(patternImage: emoji!) // Set the background of the cell.
    positionInCollection++
    return cell
}

遇到這些問題,我想知道管理這種數量的圖像的最佳方法是什么,以及如何在UICollectionView中正確顯示它們(即具有良好的滾動性能)。

謝謝!

我假設您正在加載所有圖像並將其存儲在內存中,這就是問題的根源。 您應該僅在需要時動態加載表情符號圖像。 當您使用UIImage(named:...)方法加載圖像時,UIKit會為您進行緩存。

至於UICollectionView ,使用backgroundColor屬性設置圖像不是一個好習慣。 使用setBackgroundImage(image: UIImage)方法創建UICollectionViewCell子類, setBackgroundImage(image: UIImage)方法將圖像設置在UIImageView 在子類中,您還可以執行以下操作以確保在重用單元之前卸載舊映像。

override func prepareForReuse() {
    super.prepareForReuse()
    imageView.image = nil
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM