Facebook多个图片上传

时间:2013-06-12 11:55:25

标签: ios facebook

我正在尝试从我的应用程序上传多个图像。

我会有一系列图像,我正在使用此代码上传所有图像。但是,如果图像更多,上传我的UI会被阻止。所以请建议我如何开始在后台上传或建议我另类的方式..

 HUD = [[MBProgressHUD alloc] initWithView:self.view];
HUD.labelText = @"Uploading Images";
[self.view addSubview:HUD];
[HUD show:YES];

NSMutableArray *aryFetchedImages = [self fetchPhotosForAlbum];

for (int i = 0; i < aryFetchedImages.count ; i++) {
    NSLog(@"img%d",i);
    [params setObject:@" " forKey:@"message"];
    [params setObject:UIImageJPEGRepresentation([aryFetchedImages objectAtIndex:i], 50.0f)  forKey:@"picture"];
    [FBRequestConnection startWithGraphPath:@"me/photos"
                                 parameters:params
                                 HTTPMethod:@"POST"
                          completionHandler:^(FBRequestConnection *connection,
                                              id result,
                                              NSError *error)
     {
         if (error)
         {
             //showing an alert for failure
            #ifdef DEBUG
                NSLog(@"Unable to share the photo please try later - Fail %@",error);
            #endif
         }
         else
         {
             //showing an alert for success
            #ifdef DEBUG
                NSLog(@"Photo %@ uploaded on Facebook Successfully",UIImageJPEGRepresentation([aryFetchedImages objectAtIndex:i], 50.0f));
            #endif
         }
     }];
}
[HUD show:NO];
[HUD removeFromSuperview];

2 个答案:

答案 0 :(得分:0)

我正在对代码进行一些修改请试试这个,并告诉我

HUD = [[MBProgressHUD alloc] initWithView:self.view];
    HUD.labelText = @"Uploading Images";
    [self.view addSubview:HUD];
    [HUD show:YES];

    NSMutableArray *aryFetchedImages = [self fetchPhotosForAlbum];

    for (int i = 0; i < aryFetchedImages.count ; i++) {

    [self postImgWithMessage:[aryFetchedImages objectAtIndex:i];
    }


    [HUD show:NO];
    [HUD removeFromSuperview];


    //my code to be added

    -(void)postImgWithMessage:(UIImage *)image 
    {
        NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
        NSString *filePath =[[paths1 objectAtIndex:0] stringByAppendingPathComponent:@"image.png"];
        NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
        [imageData writeToFile:filePath atomically:YES];    
        if (filePath == nil) return;
        NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/me/photos"];
        ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
        [request addFile:filePath forKey:@"file"];
        [request setPostValue:[userDefaults stringForKey:KToken] forKey:@"access_token"];
        [request setDidFinishSelector:@selector(sendToPhotosFinished:)];
        [request setDelegate:self];
        [request startAsynchronous];

    }

    - (void)sendToPhotosFinished:(ASIHTTPRequest *)request
    {
        //NSLog(@"photo sent");

    }

答案 1 :(得分:0)

Dispatch队列允许您相对于调用者异步或同步地执行任意代码块。您可以使用调度队列执行几乎所有在单独线程上执行的任务。调度队列的优点是它们比相应的线程代码更易于使用,并且执行这些任务的效率更高。Concurrency Programming Guide