Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get URL of a newly created asset, iOS 9 style

ALAssetsLibrary is deprecated these days but practically all examples on SO are still making use of it. For my objective, I need to know the URL of the video that was added to the Photo Library so I could share a video to the Instagram app (that's the only way in which Instagram accepts videos). The URL should probably start with "assets-library://..."

This is simple with ALAssetsLibrary:

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeVideoAtPathToSavedPhotosAlbum:videoFilePath completionBlock:^(NSURL *assetURL, NSError *error) {  // ...

Above, the URL is passed as an argument to the completion block. But what about iOS 9 compatible way with PHPhotoLibrary class and performChanges method?

var videoAssetPlaceholder:PHObjectPlaceholder!  // property

// ...

let videoURL = NSBundle.mainBundle().URLForResource("Video_.mp4", withExtension: nil)!

let photoLibrary = PHPhotoLibrary.sharedPhotoLibrary()
photoLibrary.performChanges({
    let request = PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(videoURL)
    self.videoAssetPlaceholder = request?.placeholderForCreatedAsset
},
completionHandler: { success, error in
    if success
    {
        // The URL of the video asset?
    }
})
like image 604
Desmond Hume Avatar asked Jan 13 '16 18:01

Desmond Hume


2 Answers

Here's what I've ended up with:

let videoURL = NSBundle.mainBundle().URLForResource("Video_.mp4", withExtension: nil)!

let photoLibrary = PHPhotoLibrary.sharedPhotoLibrary()
var videoAssetPlaceholder:PHObjectPlaceholder!
photoLibrary.performChanges({
    let request = PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(videoURL)
    videoAssetPlaceholder = request!.placeholderForCreatedAsset
},
completionHandler: { success, error in
    if success {
        let localID = videoAssetPlaceholder.localIdentifier
        let assetID =
            localID.stringByReplacingOccurrencesOfString(
                "/.*", withString: "",
                options: NSStringCompareOptions.RegularExpressionSearch, range: nil)
        let ext = "mp4"
        let assetURLStr =
            "assets-library://asset/asset.\(ext)?id=\(assetID)&ext=\(ext)"

        // Do something with assetURLStr
    }
})
like image 66
Desmond Hume Avatar answered Oct 15 '22 02:10

Desmond Hume


Nice answer and pseudo, thanks Desmond.

Here is an updated answer :

Swift 3

var assetPlaceholder: PHObjectPlaceholder!
    PHPhotoLibrary.shared().performChanges({
        let assetRequest = PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: videoURL as URL!)
        assetPlaceholder = assetRequest?.placeholderForCreatedAsset
    }, completionHandler: { (success, error) in
        if success {
            let localID = assetPlaceholder.localIdentifier
            let assetID = localID.replacingOccurrences(of: "/.*", with: "", options: NSString.CompareOptions.regularExpression, range: nil)
            let ext = "mp4"
            let assetURLStr = "assets-library://asset/asset.\(ext)?id=\(assetID)&ext=\(ext)"

            // Do what you want with your assetURLStr
        } else {
            if let errorMessage = error?.localizedDescription {
                print(errorMessage)
            }
        }
    })

Going further

Although this works perfectly for the moment, would anyone know a "cleaner" way to get this "assets-library" URL, without this "manual" solution ?

I found this interesting thread, but it only gives a "file:///var/..." URL

like image 40
Matthieu Avatar answered Oct 15 '22 00:10

Matthieu