Swift - 如何从相册加载视频并在集合视图中显示它们

时间:2017-01-07 06:24:57

标签: ios swift video uicollectionview

我使用split = tidx.to_series().diff().diff().abs().idxmax() t1 = tidx[tidx < split] t2 = tidx[tidx >= split] print(split) 2005-03-13 17:00:00 print(t1) DatetimeIndex(['2005-03-11 11:00:00', '2005-03-11 11:30:00', '2005-03-11 12:00:00', '2005-03-11 12:30:00', '2005-03-11 13:00:00', '2005-03-11 13:30:00', '2005-03-11 14:00:00', '2005-03-11 14:30:00', '2005-03-11 15:00:00'], dtype='datetime64[ns]', freq=None) print(t2) DatetimeIndex(['2005-03-13 17:00:00', '2005-03-13 17:30:00', '2005-03-13 18:00:00', '2005-03-13 18:30:00', '2005-03-13 19:00:00', '2005-03-13 19:30:00', '2005-03-13 20:00:00', '2005-03-13 20:30:00', '2005-03-13 21:00:00', '2005-03-13 21:30:00', '2005-03-13 22:00:00', '2005-03-13 22:30:00', '2005-03-13 23:00:00', '2005-03-13 23:30:00', '2005-03-14 00:00:00'], dtype='datetime64[ns]', freq=None) 从相册中加载图片。 这是我的代码......

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。我应该在单元格中使用什么来加载视频? 我还需要显示视频时长。

1 个答案:

答案 0 :(得分:2)

请参阅获取视频和视频持续时间的答案

  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
        }
    }
}