1

I'm relatively new to Swift.

I'm currently trying to grab videos stored in the photo library and display them in a collection view. After selecting a video in the collection view, I want to be able to play the video.

Right now I've written part of the function grabVideos and I have 2 questions:

  1. How should I store these videos? Can they be stored as UIImages? A lot of the other sources I found grabbed videos from online sources and they just stored the video url
  2. What should I do in the resultHandler? I would assume thats were I store my videos into a global array

Note: code below is in a function called getVideos()

    let imgManager = PHImageManager.default()
    let requestOption = PHVideoRequestOptions()
    requestOption.isSynchronous = true
    requestOption.deliveryMode = .highQualityFormat

    let fetchOption = PHFetchOptions()
    fetchOption.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]

    if let fetchResult:PHFetchResult = PHAsset.fetchAssets(with: .video, options: fetchOption) {

        if fetchResult.count > 0 {
            for i in 0...fetchResult.count {
                imgManager.requestAVAsset(forVideo: fetchResult.object(at: i) as! PHAsset, options: requestOption, resultHandler: {{ (<#AVAsset?#>, <#AVAudioMix?#>, <#[AnyHashable : Any]?#>) in
                    <#code#>
                    }})
            }
        } else {
            print("Error: No Videos Found")
        }
    }
Harjot Singh
  • 535
  • 7
  • 24
Sam Shuzawa
  • 11
  • 1
  • 2

1 Answers1

0

First you add a variable to your ViewController var fetchResults: PHFetchResult<PHAsset>?

Then you execute the fetch in viewDidLoad for instance

let fetchOption = PHFetchOptions()
    fetchOption.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]

let fetchResults = PHAsset.fetchAssets(with: .video, options: fetchOption);
self.fetchResults = fetchResults

if fetchResults.count == 0 {
    print("Error: No Videos Found")
    return
}

In your collectionViewCell you have to add a UIImageView so that we can show thumbnail in each cell, then you just do the following in collection view data source methods

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return fetchResults?.count ?? 0
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "reuse", for: indexPath) as! TestCollectionViewCell
    let videoAsset = fetchResults!.object(at: indexPath.item)
    PHImageManager.default().requestImage(for: videoAsset, targetSize: cell.bounds.size, contentMode: .aspectFill, options: nil) { (image: UIImage?, info: [AnyHashable : Any]?) in
        cell.imageView.image = image
    }

    return cell
}

This can be approved upon, but will be OK for the first try, specifically look at using PHCachingImageManager instead of PHImageManager, more on this in the links below:

PHCachingImageManager

How to use PHCachingImageManager

How to play a video from PHAsset you can find answered:

Swift - Playing Videos from iOS PHAsset

Ladislav
  • 7,223
  • 5
  • 27
  • 31