Search code examples
swiftios9phasset

How to fetch newly created object from PHObjectPlaceholder after change request completes


I'm using the new Photos framework and I'm saving a video to my photo roll:

   let assetRequest = PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(url)
    assetPlaceHolder = assetRequest?.placeholderForCreatedAsset

A short time later in my success handler after the video has been saved to the photo library I can reference the assetPlaceHolder object and it's looking like a valid PHObjectPlaceholder object. I'm just unsure from looking at Apple's documentation how to get at the actual video or url to the video that's been saved.


Solution

  • This code is untested, but it pretty much works like this:

    let assets:PHFetchResult = PHAsset.fetchAssetsWithLocalIdentifiers([assetPlaceHolder!.localIdentifier], options: nil)
    
    if let asset = assets.firstObject as? PHAsset {
         PHImageManager.defaultManager().requestPlayerItemForVideo(asset, options: PHVideoRequestOptions(), resultHandler: {
            (result, info) in
            //result: AVplayerItem?
    
    }