0

次のコードがありますが、機能しません。その背後で何かが働いていますか?

[operationQueue addOperationWithBlock:^{
        imageData =  [NSData dataWithContentsOfURL:imageURL];
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            UIImage *image = nil;

            if(imageData){
                UIImage *image = [UIImage imageWithData:imageData];
                cell.imageView.image = image;
            }
        }];
    }];

NSOperationのサブクラスを作成し、それを初期化しても、思ったとおりに機能しません。実行するには、常にNSOperationサブクラスに対してstartを呼び出す必要がありますが、開始メッセージをNSOperationに送信すると、バックグラウンドスレッドではなく、メインスレッドで実行されると思います。

4

1 に答える 1

1

GCDを使用して代替ソリューションを追加したい:

backgroundQueue = dispatch_queue_create("com.razeware.imagegrabber.bgqueue", NULL);
dispatch_async(backgroundQueue, ^{
                          /* put the codes which makes UI unresponsive like reading from network*/ 
                         imageData =  [NSData dataWithContentsOfURL:imageURL];
                         .....   ;
                         dispatch_async(dispatch_get_main_queue(),^{
                                         /* do the UI related work on main thread */
                                         UIImage *image = [UIImage imageWithData:imageData];
                                         cell.imageView.image = image;
                                         ......;   });
                                  });
dispatch_release(backgroundQueue);

これがあなたを助けたかどうか教えてください;)

参照

于 2012-10-30T18:58:49.320 に答える