我正在尝试通过Facebook FBSDKShareKit共享视频。视频先存储在设备文档的本地,然后再存储在照片库中,然后从那里创建素材资源URL,将其发送到Facebook。
// videoURLString = file:///var/mobile/Containers/Data/Application/5341AB22-ABF3-43BB-8790-A7B5B94FA89F/Documents/video/video.mp4
func copyVideoToAssets(videoURL: URL, completion: @escaping (URL?) -> ()) {
let photoLibrary = PHPhotoLibrary.shared()
var videoAssetPlaceholder: PHObjectPlaceholder!
photoLibrary.performChanges({
if let request = PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: videoURL) {
videoAssetPlaceholder = request.placeholderForCreatedAsset
}
}, completionHandler: { success, error in
if success {
let options: PHVideoRequestOptions = PHVideoRequestOptions()
options.version = .current
options.deliveryMode = .automatic
options.isNetworkAccessAllowed = true
let asset = PHAsset.fetchAssets(withLocalIdentifiers: [videoAssetPlaceholder.localIdentifier], options: nil)
if let phasset = asset.firstObject {
PHImageManager.default().requestAVAsset(forVideo: phasset, options: options, resultHandler: {(asset: AVAsset?, audioMix: AVAudioMix?, info: [AnyHashable : Any]?) -> Void in
if let urlAsset = asset as? AVURLAsset {
let localVideoUrl: URL = urlAsset.url as URL
let pathExtension = localVideoUrl.pathExtension
let id = phasset.localIdentifier
let uuid = id.components(separatedBy: "/")[0]
let asseturl = "assets-library://asset/asset.\(pathExtension)?id=\(uuid)&ext=\(pathExtension)"
let url = URL(string: asseturl)
self.shareVideo(url)
}
})
}
}
})
}
assetURL: assets-library://asset/asset.MP4?id=2777C4AC-1CCC-404C-AFBE-0A0395AF62E0&ext=MP4
func shareVideo(assetURL: URL) {
let video = FBSDKShareVideo()
video.videoURL = assetURL
let content = FBSDKShareVideoContent()
content.video = video
self.completion = completion
self.controller = controller
FBSDKShareDialog.show(from: controller, with: content, delegate: self)
}
这不起作用,没有视频的共享模式也会出现。