gpt4 book ai didi

ios - 使用 SLRequest 将多张图片上传到 facebook

转载 作者:行者123 更新时间:2023-11-29 12:01:54 25 4
gpt4 key购买 nike

我有两种方法可以将图片分享到 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!");
}
}
}];
}

I believed that if each data sent in a multipart might work, but no.

有人知道怎么做吗?谢谢。

最佳答案

facebook 文档中有一些文章可能对您有所帮助。

https://developers.facebook.com/docs/graph-api/making-multiple-requests

因此,在您的情况下,您需要在请求中发送一个名为 batch 的字段,并且每个文件都必须附加一个唯一的名称,这样它就不会被另一个人重写。

batch 字段的内容将是一个 json,您将在其中指定请求的端点,如下所示(缩小顺便说一句):

[
{
"method": "POST",
"relative_url": "me/photos",
"body": "message=labrys",
"attached_files": "file1"
},
{
"method": "POST",
"relative_url": "me/photos",
"body": "message=circle",
"attached_files": "file2"
}
]

我用 postman 试过了,该请求的设置应该是这样的:

image

很抱歉没有发布代码,我不精通 Objective-C ,但我希望你能理解这一点。

关于ios - 使用 SLRequest 将多张图片上传到 facebook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36581617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com