For Displaying the Live-Photos:
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()
}
}
}
And For Displaying in Grid Code is below:
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
}
And For Fetching Video, I am using this predicate:
option.predicate = NSPredicate(format: "mediaType == %i",PHAssetMediaType.video.rawValue)
But in Collectionview both are not display.
try to override the method from UICollectionViewDataSource protocol:
also:
don't forget to set the ViewController as table view datasource. don't forget to attach collectionView property in storyboard, if you are create it from storyboard.
i have build it and works perfectly.