swift3でcollectionviewでカスタムセルでフルスクリーンの画像を表示する、スライドショーのようなアプリを作成しています。
xibを使って複数のレイアウトを用意していますが、基本的にはボタンの配置などが違うだけで、フルスクリーンでメイン画像を表示しています。
セルをスクロールして、だいたい50セル目を超えたあたりで、consoleに
2017-04-18 21:33:52.165 アプリ名[4497:60b] Received memory warning.Received memory warning.
と表示されます。
これを無視してスクロールしていると、何回かconsoleに上記が表示されたあと、アプリが落ちます。
落ちたらxcodeで
Message from debugger: Terminated due to Memory Error
とポップアップがでます。
調べていると、UIImageがメモリを圧迫しているのではないかと思いましたが、対処方法がよくわかっていません。
詳しい方いらっしゃいましたら、ご教授願えますでしょうか。
// CollectionViewClass override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.collectionView!.register(UINib(nibName: "CustomCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CustomCell") } // (中略) override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { // アセットカタログに登録したimageを取得する let image = getCollectionViewImage(row: indexPath.row)! let NoBtnCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath as IndexPath) as! CollectionViewCell NoBtnCell.setCell(image: image) }
// CustomCellクラス import UIKit class CustomCell: UICollectionViewCell { @IBOutlet weak var mainImage: UIImageView! override func awakeFromNib() { super.awakeFromNib() // Initialization code setButton() } func setCell(image: UIImage) { self.mainImage.image = image } }
上記のコードは一つのカスタムセルクラスに絞って書いています(本当は複数ありますが、配置以外ほとんど変わらないので)。
追記
swift
1 2// アセットカタログに登録したimageを取得するメソッド 3func getCollectionViewImage(row: Int) -> UIImage? { 4 let imageName: String = NSString(format:"main_%02d", row) as String 5 let image = UIImage(named: imageName)! 6 return image 7}
getCollectionViewImage()のコードを書いて下さい。
カスタムセルが複数とのことですが具体的には何種類でしょうか?
セットしている画像のファイルサイズを大体でいいので教えて下さい。
追記しました。
回答1件
あなたの回答
tips
プレビュー