获取新创建的资产的URL,iOS 9样式 [英] Get URL of a newly created asset, iOS 9 style

查看:123
本文介绍了获取新创建的资产的URL,iOS 9样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ALAssetsLibrary 这些天已被弃用,但实际上SO上的所有示例仍在使用它。为了我的目标,我需要知道添加到照片库的视频的URL,以便我可以将视频分享到Instagram应用程序(这是Instagram接受视频的唯一方式)。 URL应该以assets-library:// ...开头。

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://..."

这很简单, ALAssetsLibrary

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

上面,URL作为参数传递给完成块。但是与iOS 9兼容的方式与 PHPhotoLibrary 类和 performChanges 方法怎么样?

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


推荐答案

以下是我最终得到的结果:

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

这篇关于获取新创建的资产的URL,iOS 9样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆