我正在尝试使用从用户的相机胶卷中提取的视频和照片的缩略图来填充UICollectionView。我在数组PHAsset
中以images
的形式加载了相机胶卷中的所有媒体,如下所示:
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
答案 0 :(得分:1)
使用PHAsset requestImage(for:targetSize:contentMode:options:resultHandler:)
适用于照片和视频。
您可以将此方法用于视频的照片和视频资源 资产,图像请求提供缩略图或海报框架。
所以只需删除if语句并调用请求两种类型的缩略图。
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
编辑: 确保允许网络访问
options.isNetworkAccessAllowed = true
答案 1 :(得分:0)
您可以尝试使用Apple sample project
中的PHCachingImageManager
let cachingImageManager = PHCachingImageManager()
在加载项目调用之前的某个地方尝试加载缓存
func updateCache() {
//Update the size here
let size: CGSize = CGSize(width: 138, height: 138)
cachingImageManager.startCachingImages(for: images, targetSize: size, contentMode: .aspectFit, options: nil)
}
然后加载图片和视频的缩略图就像这样
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoLibraryCollectionViewCell", for: indexPath) as! PhotoLibraryCollectionViewCell
let asset = images[indexPath.row]
let size = CGSize(width: 138, height: 138)
let option = PHImageRequestOptions()
option.isSynchronous = true
cachingImageManager.requestImage(for: asset, targetSize: size, contentMode: .aspectFit, options: option) { image, info in
cell.imageView.image = image
}
return cell
}
注意:从PHAsset
加载图像而不进行缓存可能会导致内存问题
如果图像数组中的项目发生更改,您可能需要创建另一个PHCachingImageManager