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:
- 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
- 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")
}
}