【发布时间】:2018-02-19 21:09:01
【问题描述】:
我正在尝试使用从用户的相机胶卷中提取的视频和照片的缩略图填充 UICollectionView。我在数组images 中以PHAsset 的形式从相机胶卷中加载了所有媒体,如下所述:
var images = [PHAsset]()
if-then 循环检查PHAsset 是图像还是视频。如果它是一个图像,它只是将它添加到集合视图中。但是,我不确定如何将视频的缩略图添加到集合视图中。任何帮助表示赞赏。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoLibraryCollectionViewCell", for: indexPath) as! PhotoLibraryCollectionViewCell
let asset = images[indexPath.row]
if asset.mediaType == PHAssetMediaType.image {
let manager = PHImageManager.default()
let option = PHImageRequestOptions()
var thumbnail = UIImage()
option.isSynchronous = true
manager.requestImage(for: asset, targetSize: CGSize(width: 138, height: 138), contentMode: .aspectFit, options: option, resultHandler: {(result, info)->Void in
thumbnail = result!
})
cell.imageView.image = thumbnail
} else {
// code to add the thumbnail of the video to the collection view
}
return cell
}
编辑: 我尝试删除 if-then 方法,但集合视图根本没有显示视频的缩略图。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoLibraryCollectionViewCell", for: indexPath) as! PhotoLibraryCollectionViewCell
let asset = images[indexPath.row]
let manager = PHImageManager.default()
let option = PHImageRequestOptions()
var thumbnail = UIImage()
option.isSynchronous = true
option.isNetworkAccessAllowed = true
manager.requestImage(for: asset, targetSize: CGSize(width: 138, height: 138), contentMode: .aspectFit, options: option, resultHandler: {(result, info)->Void in
thumbnail = result!
})
cell.imageView.image = thumbnail
return cell
}
如果您查看下面的链接,我的相机胶卷中显然有一个视频,但它没有显示在收藏视图中: https://imgur.com/a/5GfDm
【问题讨论】:
-
您是否尝试过像处理照片一样处理视频?文档表明视频和照片资产都具有可从 PHImageManager 获得的缩略图属性。
-
@JoshHeald 我试过了,但由于某种奇怪的原因它显示为空白图像。