简体   繁体   English

如何将视频NSURL转换为ALAsset?

[英]How to convert a video NSURL to an ALAsset?

Facebook sharing requires an ALAsset such as the following: Facebook共享需要以下ALAsset:

let content = FBSDKShareVideoContent()

//The videos must be less than 12MB in size.

let bundle = NSBundle.mainBundle()
let path = bundle.URLForResource("a", withExtension: "mp4")

let video = FBSDKShareVideo()
// doesn't work; needs to be an "asset url" (ALAsset)
//video.videoURL = path

content.video = video

let dialog = FBSDKShareDialog()
dialog.shareContent = content
dialog.show()

How is it possible to take a local bundle document, or an NSData object, and convert it to an ALAsset? 如何获取本地包文档或NSData对象,并将其转换为ALAsset?

(My initial thinking was saving the video to the local camera roll, and then loading the list and selecting it, but that is unnecessary interface steps) (我最初的想法是将视频保存到本地摄像机胶卷,然后加载列表并选择它,但这是不必要的界面步骤)

The documentation for an ALAsset states that ALAsset的文档指出:

An ALAsset object represents a photo or a video managed by the Photo application. ALAsset对象代表由Photo应用程序管理的照片或视频。

so I'm pretty sure that you have to write the video to the camera roll before using it as an ALAsset . 因此,我非常确定您必须在将视频用作ALAsset之前将其写入相机胶卷。 However, you don't need to open the camera roll and have a user pick the asset in order to use it. 但是,您无需打开相机胶卷并让用户选择资产即可使用它。 When writing to the ALAssetLibrary using 使用写入ALAssetLibrary时

library.writeVideoAtPathToSavedPhotosAlbum(movieURL, completionBlock: { (newURL, error) -> Void

you get the asset url in that newUrl completion block variable. 您会在newUrl完成区块变量中获得资产网址。 Use it in the Facebook sharing call 在Facebook分享通话中使用它

let content = FBSDKShareVideoContent()
content.video = FBSDKShareVideo(videoURL: newURL)

FBSDKShareAPI.shareWithContent(content, delegate: self)
NSLog("Facebook content shared \(content.video.videoURL)")

You can do this sharing inside the completion block if you so desire, or you can save the newUrl from the completion block and use it somewhere else. 如果需要,您可以在完成块内进行此共享,也可以从完成块中保存newUrl并在其他地方使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM