【问题标题】:How to get thumbnail for video from PHAsset?如何从 PHAsset 获取视频的缩略图?
【发布时间】: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 我试过了,但由于某种奇怪的原因它显示为空白图像。

标签: swift phasset


【解决方案1】:

当使用 PHAsset requestImage(for:targetSize:contentMode:options:resultHandler:) 时,照片和视频都适用。

From Apple documentation:

您可以将此方法用于视频的照片和视频资源 资产,图像请求提供缩略图或海报框架。

所以只需删除 if 语句并调用 request 两种类型的缩略图。

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

【讨论】:

  • 试过这样做,请参阅更新帖子中的代码。集合视图在结果中根本不显示缩略图。
  • 我还加了一些图片供参考
  • @itsfaraaz 确保允许网络访问。查看我的编辑
  • 仍然没有运气,请查看我所做的编辑的更新代码。
  • @itsfaraaz 你能分享你用来获取相机胶卷的代码吗?
【解决方案2】:

您可以尝试使用PHCachingImageManager,如Apple sample project

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 以防图像数组中的项目发生变化

【讨论】:

    猜你喜欢
    • 2015-12-17
    • 2013-06-05
    • 2020-05-16
    • 1970-01-01
    • 2011-05-25
    • 2011-05-12
    • 2010-11-19
    • 2012-03-28
    相关资源
    最近更新 更多