用于显示实时照片
func fetchLivePhotos() {
let options = PHFetchOptions()
options.sortDescriptors = [ NSSortDescriptor(key: "creationDate", ascending: false) ]
options.predicate = NSPredicate(format: "(mediaSubtype & %d) != 0",PHAssetMediaSubtype.photoLive.rawValue)
DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async {
self.livePhotoAssets = PHAsset.fetchAssets(with: options)
DispatchQueue.main.async {
self.collectionView?.reloadData()
}
}
}
网格代码显示在
之下
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MediaCell", for: indexPath) as! MediaCell
if let asset = livePhotoAssets?[indexPath.row]{
let options = PHImageRequestOptions()
options.isNetworkAccessAllowed = true
let targetSize = CGSize(width: 200, height: 200)
PHImageManager.default().requestImage(for: asset, targetSize: targetSize, contentMode: .aspectFill, options: options, resultHandler: { (image: UIImage?, info: [AnyHashable : Any]?) in
cell.backgroundImageView.image = image
})
}
cell.selectedImage.isHidden = true
return cell
}
为了获取视频我正在使用这个谓词
option.predicate = NSPredicate(format: "mediaType == %i",PHAssetMediaType.video.rawValue)
但在Collectionview中都不显示
任何人都知道
感谢高级。!!!!
答案 0 :(得分:0)
尝试从UICollectionViewDataSource协议覆盖该方法:
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if let count = livePhotoAssets?.count {
return count
}
return 0
}
也:
不要忘记将ViewController设置为表视图数据源。 如果您是从storyboard创建的,请不要忘记在storyboard中附加collectionView属性。我已经建立并完美运作。