Realmに保存している写真のlocalIdentifierから写真をCollectionViewに表示できるようにしたのですが
スクロールの速度を上げるとカクついてしまうため、マルチスレッドで読み込みを早くしようとしたら、カクつきはしなくなりましたが
その代わり読み込まれる画像がおかしくなってしまいます
もしかしたら他スレッドでの処理が終わっていないのにcellへの画像設定が呼び出されているから、おかしいのかなと思いましたが、解決策がわかりません
解決方法をご存知の方いましたらご教授願えませんでしょうか
よろしくお願いいたします
CollectionViewDelegate
swift
1func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 2 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "test", for: indexPath) as! collectionViewCell 3 _getPhotoImage(index: indexPath.row) 4 cell.photoView.image = self.image 5 return cell 6}
swift
1 2private var image: UIImage? 3 4private func _getPhotoImage(index: Int){ 5 let realm = try! Realm() 6 //Realmはバラバラに保存されるため、順番を保持するための配列 7 var photoCollection = UserDefaults.standard.array(forKey: "PHOTO_COLLECTION") 8 9 if let photoObject = realm.object(ofType: Photo.self, forPrimaryKey: (photoCollection?[index])){ 10 let _photo = ThreadSafeReference(to: photoObject) 11 DispatchQueue.global(qos: .userInteractive).async { 12 let realm = try! Realm() 13 guard let photo = realm.resolve(_photo) else { 14 return 15 } 16 let localIdentifier: [String] = [photo.photoURL!] 17 let photoAsset = PHAsset.fetchAssets(withLocalIdentifiers: localIdentifier, options: nil) 18 //PHAssetをUIImageに変換 19 self.image = getUIImage(asset:photoAsset.object(at: 0)) 20 } 21 } 22}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/02/11 03:09
2019/02/11 07:41
退会済みユーザー
2019/02/12 00:59