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

コードに変更を加えています。これを試して、教えてください

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 に答える
0

ディスパッチ キューを使用すると、任意のコード ブロックを呼び出し元に対して非同期または同期で実行できます。ディスパッチ キューを使用すると、個別のスレッドで実行していたほぼすべてのタスクを実行できます。ディスパッチ キューの利点は、対応するスレッド化されたコードよりも使いやすく、これらのタスクの実行がはるかに効率的であることです。同時実行プログラミング ガイド

于 2013-06-12T12:12:20.557 に答える