【问题标题】:Swift - How to load videos from photo album and display them in collection viewSwift - 如何从相册加载视频并在收藏视图中显示它们
【发布时间】:2017-01-07 06:24:56
【问题描述】:

我使用PHImageManager 从相册加载图像。 这是我的代码...

var photoLibrary = [UIImage]()
    func grabPhotos(){
        let imgManager = PHImageManager.default()

        let requestOptions = PHImageRequestOptions()
        requestOptions.isSynchronous = true
        requestOptions.deliveryMode = .highQualityFormat

        let fetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
        if let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions) {
            if fetchResult.count > 0 {
                for i in 0..<fetchResult.count{
                    imgManager.requestImage(for: fetchResult.object(at: i) as PHAsset , targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler: {
                        image, error in
                        self.photoLibrary.append(image!)
                    })
                }
            }
            else{
                showAllertToImportImage()//A function to show alert
            }
        }
    }

现在我想从相册加载视频,我需要在UICollectionView 中显示它们。 为了在UICollectionView 中加载图像,我在单元格中使用了UIImageView。我应该在单元格中使用什么来加载视频? 我还需要显示视频时长。

【问题讨论】:

    标签: ios swift video uicollectionview


    【解决方案1】:

    请查看获取视频和视频时长的答案

      func grabPhotos(){
        let imgManager = PHImageManager.default()
    
        let requestOptions = PHImageRequestOptions()
        requestOptions.isSynchronous = true
        requestOptions.deliveryMode = .highQualityFormat
    
        let fetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
        if let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: .video, options: fetchOptions) {
            if fetchResult.count > 0 {
                    for i in 0..<fetchResult.count{
    
     //Used for fetch Image//
    imgManager.requestImage(for: fetchResult.object(at: i) as PHAsset , targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler: {
                        image, error in
                        let imageOfVideo = image! as UIImage
                        self.photoLibrary.append(imageOfVideo)
                    })
      //Used for fetch Video//
     imgManager.requestAVAsset(forVideo: fetchResult.object(at: i) as PHAsset, options: PHVideoRequestOptions(), resultHandler: {(avAsset, audioMix, info) -> Void in
                        if let asset = avAsset as? AVURLAsset {
                            //let videoData = NSData(contentsOf: asset.url)
                            let duration : CMTime = asset.duration
                           let durationInSecond = CMTimeGetSeconds(duration)
                            print(durationInSecond)
                        }
    
    
    
                    })
                }
    
            }
            else{
                //showAllertToImportImage()//A function to show alert
            }
        }
    }
    

    【讨论】:

    • 我在 UICollectionView 的单元格中使用了 UIImageVIew 来显示图像。我应该用什么做视频??任何的想法?? @Amit Verma
    • 这将为您提供图像,您还可以通过视频制作图像数据,例如 Thumbimage、imgManager.requestImage(for: fetchResult.object(at: i) as PHAsset , targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler: { image, error in self.photoLibrary.append(image!) })
    • 您使用 self.photoLibrary.append(imageOfVideo) 来获取图像。像这样我使用 self.videoDurationArray.append(String(durationInSecond)) 并添加了这行 let labelView = cell.viewWithTag(2) as! UILabel labelView.text = videoDurationArray[indexPath.row] in func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {...} ... NSLog 说索引超出范围 :(
    猜你喜欢
    • 1970-01-01
    • 2011-05-07
    • 2018-01-25
    • 2016-05-30
    • 2014-06-13
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多