0

我正在尝试从我的 Application 上传多个图像。

我会有图像数组,我正在使用此代码上传所有图像。但是,如果图像更多,则在上传我的 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];
4

2 回答 2

0

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

于 2013-06-12T12:12:20.557 回答
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");

    }
于 2013-06-12T12:07:58.863 回答