我有两种方法可以将图像分享到 Facebook。
SDK方式(完美运行):
FBSDKSharePhotoContent *content = [[FBSDKSharePhotoContent alloc] init];
NSMutableArray *uploadImages = [[NSMutableArray alloc] init];
for (UIImage *image in images) {
FBSDKSharePhoto *sharePhoto = [[FBSDKSharePhoto alloc] init];
sharePhoto.caption = title;
sharePhoto.image = image;
[uploadImages addObject:sharePhoto];
}
content.photos = [NSArray arrayWithArray:uploadImages];
[FBSDKShareAPI shareWithContent:content delegate:self];
和SLRequest方式(只发送1张图片):
+ (void)uploadPhotoToFacebook:(NSMutableArray *)imagesData imageTitle:(NSString *)title account:(ACAccount*)account completionBlock:(shareSocialResponse)completion
{
SLRequest *facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:[NSURL URLWithString:@"https://graph.facebook.com/me/photos"]
parameters:@{@"message": title}];
for (NSData *imageData in imagesData) {
[facebookRequest addMultipartData:imageData
withName:@"source"
type:@"multipart/form-data"
filename:@"photo!"];
}
NSLog(@"facebookRequest %@",facebookRequest);
facebookRequest.account = account;
[facebookRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
if (error) {
NSLog(@"%@",error.description);
} else {
NSDictionary *returnedData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
NSLog(@"returnedData: %@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
if ([urlResponse statusCode] != 200) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *errorMessage = @"We could not process your request now";
if ([returnedData valueForKeyPath:@"error.message"] != nil) {
errorMessage = [returnedData valueForKeyPath:@"error.message"];
}
completion(NO, errorMessage);
});
} else {
completion(YES, @"Your clip was posted!");
}
}
}];
}
我相信如果在多部分中发送的每个数据都可以工作,但不是。
有人知道怎么做吗?谢谢。