![](/img/trans.png)
[英]upload video to facebook using facebook sdk 3.1 on ios 6.0
[英]Could we upload the documents directory video to facebook using Facebook SDK
我在使用Facebook SDK將視頻上傳到Facebook時遇到問題。我正在嘗試從SavedPhotos選擇的視頻可以正常工作。 但是,當我嘗試從我的文檔目錄上載視頻時,它說的是以下錯誤。 我所知道的是,我們可以上傳包含素材資源網址的視頻 。 還有其他方法可以將文檔目錄視頻上傳到Facebook嗎???
錯誤是
2015-05-26 16:30:02.369 graphtwentysixth[3025:1413799] FB: ERROR=Error Domain=com.facebook.sdk.share Code=2 "The operation couldn’t be completed. (com.facebook.sdk.share error 2.)" UserInfo=0x156b0f90 {com.facebook.sdk:FBSDKErrorArgumentValueKey=file:///private/var/mobile/Containers/Bundle/Application/48DA75B3-63BA-400A-AC92-BE6B4A2B954B/graphtwentysixth.app/demo-video-high-quality.mov, com.facebook.sdk:FBSDKErrorArgumentNameKey=videoURL, com.facebook.sdk:FBSDKErrorDeveloperMessageKey=Invalid value for videoURL: file:///private/var/mobile/Containers/Bundle/Application/48DA75B3-63BA-400A-AC92-BE6B4A2B954B/graphtwentysixth.app/demo-video-high-quality.mov}
碼
NSURL *videoURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"demo-video-high-quality" ofType:@"mov"]];
FBSDKShareVideo *video = [[FBSDKShareVideo alloc] init];
video.videoURL = videoURL;
FBSDKShareVideoContent *content1 = [[FBSDKShareVideoContent alloc] init];
content1.video = video;
[FBSDKShareAPI shareWithContent:content1 delegate:self];
謝謝您的寶貴時間
無法從文檔目錄上載視頻。 您可以通過將視頻作為資產來實現此目的,然后將資產的網址提供給Facebook,並在完成處理程序調用時將其從圖庫中刪除。 這是一個技巧,但不是一個好的解決方案,因為當您將視頻變成廚房資產時,它將在savePhotos中可見。
就像Shoaib所說的那樣,您需要首先將視頻變成資產。 確保在您的班級中包含#import <AssetsLibrary/AssetsLibrary.h>
。
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
ALAssetsLibraryWriteVideoCompletionBlock videoWriteCompletionBlock = ^(NSURL *newURL, NSError *error) {
if (error)
{
NSLog( @"Error writing image with metadata to Photo Library: %@", error );
}
else
{
NSLog(@"Wrote image with metadata to Photo Library %@", newURL.absoluteString);
FBSDKShareVideo* video = [FBSDKShareVideo videoWithVideoURL:newURL];
FBSDKShareVideoContent* content = [[FBSDKShareVideoContent alloc] init];
content.video = video;
[FBSDKShareAPI shareWithContent:content delegate:self];
}
};
NSURL *videoURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"demo-video-high-quality" ofType:@"mov"]];
if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:videoURL])
{
[library writeVideoAtPathToSavedPhotosAlbum:videoURL completionBlock:videoWriteCompletionBlock];
}
(編輯部分受此職位啟發)
您應該先將視頻保存到媒體庫,然后獲取正確的PHAsset並生成正確的URL:
guard let schemaUrl = URL(string: "fb://") else {
return //be safe
}
if UIApplication.shared.canOpenURL(schemaUrl) {
PHPhotoLibrary.requestAuthorization({ [weak self]
(newStatus) in
guard let strongSelf = self else {
return
}
if newStatus == PHAuthorizationStatus.authorized {
strongSelf.saveVideoToCameraRoll(url: assetURL, completion: { (result, phAsset) in
phAsset?.getURL(completionHandler: { (url) in
if let url = url {
dispatchAsyncOnMainQueue {
let video = FBSDKShareVideo()
video.videoURL = url
let content = FBSDKShareVideoContent()
content.video = video
dialog.shareContent = content
dialog.show()
}
}
})
})
} else {
//unauthorized
}
})
} else {
//facebookAppNotInstalled
}
...
func saveVideoToCameraRoll(url: URL, completion:@escaping (Bool, PHAsset?) -> ()) {
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: url)
}) { saved, error in
if saved {
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
fetchOptions.fetchLimit = 1
let fetchResult = PHAsset.fetchAssets(with: .video, options: fetchOptions).firstObject
completion(true, fetchResult)
} else {
completion(false, nil)
}
}
}
...
extension PHAsset {
func getURL(completionHandler : @escaping ((_ responseURL : URL?) -> Void)){
if self.mediaType == .video {
let options: PHVideoRequestOptions = PHVideoRequestOptions()
options.version = .original
let nameParts = self.localIdentifier.components(separatedBy: "/")
if nameParts.count > 0 {
let assetFormatString = "assets-library://asset/asset.MP4?id=%@&ext=MP4"
let name = nameParts[0]
let urlString = String(format: assetFormatString, name)
if let url = URL(string: urlString) {
completionHandler(url)
} else {
completionHandler(nil)
}
}
}
}
}
您只需嘗試以下代碼,可能會對您有所幫助。
- (void)upload{
if (FBSession.activeSession.isOpen) {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"demo-video-high-quality" ofType:@"mov"];
NSURL *pathURL = [[NSURL alloc]initFileURLWithPath:filePath isDirectory:NO];
NSData *videoData = [NSData dataWithContentsOfFile:filePath];
NSDictionary *videoObject = @{
@"title": @"FB SDK 3.1",
@"description": @"hello there !",
[pathURL absoluteString]: videoData
};
FBRequest *uploadRequest = [FBRequest requestWithGraphPath:@"me/videos"
parameters:videoObject
HTTPMethod:@"POST"];
[uploadRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error)
NSLog(@"Done: %@", result);
else
NSLog(@"Error: %@", error.localizedDescription);
}];
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.