使用iOS sdk将照片上传到Facebook上的相册

时间:2014-05-01 05:15:20

标签: ios objective-c facebook web-services

我正在尝试在用户的Facebook帐户上创建新相册,并将图像上传到该相册。我已经成功创建了一个专辑,但是当我开始上传图像时,只有选中的最后一个图像上传了我选择的图像的次数。

这是我创建Facebook相册并上传图像的代码

- (void)createFacebookAlbum {
    NSMutableDictionary* parameters = [NSMutableDictionary dictionary];
    [parameters setObject:albumNAME forKey:@"name"];
    [parameters setObject:@"Test message" forKey:@"message"];

    FBRequest* request = [FBRequest requestWithGraphPath:@"me/albums" parameters:parameters HTTPMethod:@"POST"];

    NSLog(@"creating facebook album");
    FBRequestConnection *connection = [[FBRequestConnection alloc] init];
    [connection addRequest:request
         completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
             if (!error) {
                 albumId = [result objectForKey:@"id"];
                 NSLog(@"OK %@", albumId);
                 [self publishContentToAlbum:[result objectForKey:@"id"]];
             }
             else {
                 NSLog(@"Error: %@",error.userInfo);
             }
         }];
    [connection start];
}

   -(void)uploadBulkPhotosToAlbum:(NSMutableArray *)photoArray albumId:(NSMutableArray *)albumId{
FBRequestConnection *connection = [[FBRequestConnection alloc] init];
NSString *graphPath = [NSString stringWithFormat:@"%@/photos",albumId];

NSMutableString *jsonFormat = [[NSMutableString alloc] init];
[jsonFormat setString:@"["];

for (int i = 0; i < (NSUInteger *)countere; i++)
{
    if (i != 0)
    {
        [jsonFormat appendString:@","];
    }

    NSString *fileName = [NSString stringWithFormat:@"file%d",i];
    NSString *jsonRequest = [NSString stringWithFormat:@"{ \"method\": \"POST\",    \"relative_url\": \"%@\", \"attached_files\": \"%@\" }",graphPath,fileName];
    [jsonFormat appendString:[NSString stringWithFormat:@" %@",jsonRequest]];
}

[jsonFormat appendString:@" ]"];

NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:jsonFormat,@"batch",nil];


for (int i = 0; i <(NSUInteger *)countere; i++)
{
    NSString *fileName = [NSString stringWithFormat:@"file%d",i];

    NSData *data = UIImagePNGRepresentation([photoArray objectAtIndex:i]);
    [params setObject:data forKey:fileName];
}

FBRequest *request1 = [FBRequest requestWithGraphPath:graphPath
                                           parameters:params
                                           HTTPMethod:@"POST"];

    [connection addRequest:request1
     completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {

         if (!error) {

             NSLog(@"Photo uploaded successfuly! %@",result);

         } else {

             NSLog(@"Photo uploaded failed :( %@",error.userInfo);
         }

     }];

[connection start];

}

任何人都可以帮我解决问题吗? 谢谢你的回复。

0 个答案:

没有答案