[英]How to display Live-photos Videos from library and display in Collection-View in iOS Swift?
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. 但是在Collectionview中都不会显示。
try to override the method from UICollectionViewDataSource protocol: 尝试覆盖UICollectionViewDataSource协议中的方法:
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if let count = livePhotoAssets?.count {
return count
}
return 0
}
also: 也:
don't forget to set the ViewController as table view datasource. 不要忘记将ViewController设置为表视图数据源。 don't forget to attach collectionView property in storyboard, if you are create it from storyboard. 如果您是从情节提要中创建的,请不要忘记在情节提要中附加collectionView属性。
i have build it and works perfectly. 我已经建立并完美地工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.