【问题标题】:Facebook Multiple Images uploadFacebook 多张图片上传
【发布时间】:2013-06-12 11:55:24
【问题描述】:

我正在尝试从我的 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];

【问题讨论】:

    标签: ios facebook


    【解决方案1】:

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

    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");
    
        }
    

    【讨论】:

      【解决方案2】:

      调度队列让您可以相对于调用者异步或同步地执行任意代码块。您可以使用调度队列来执行您过去在单独线程上执行的几乎所有任务。调度队列的优点是使用起来更简单,并且在执行这些任务时比相应的线程代码更有效率。Concurrency Programming Guide

      【讨论】:

        猜你喜欢
        • 2011-09-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-15
        • 1970-01-01
        相关资源
        最近更新 更多