2013-06-12 194 views
0

我想從我的應用程序上傳多個圖像。Facebook多個圖像上傳

我會有圖像陣列,我正在使用此代碼上傳所有圖像。但是,如果圖片更多,上傳我的用戶界面會被阻止。所以,請建議我如何開始上傳背景或建議我的另一種方法..

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]; 

回答

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

    } 
0

調度隊列讓你可以與調用者異步或同步地執行任意代碼塊。您可以使用分派隊列來執行幾乎所有用於在單獨的線程上執行的任務。調度隊列的優點是它們比相應的線程代碼更簡單,更有效地執行這些任務。 Concurrency Programming Guide