0

以下代码工作正常:

   NSURL* urlAddressJSonUrl = [NSURL URLWithString:urlString];
        dispatch_async(globalQueue,
                       ^{
                           NSData *imageData = [NSData dataWithContentsOfURL:urlAddressJSonUrl];

                           [self performSelectorOnMainThread:@selector(setImage:)
                                                  withObject:[UIImage imageWithData:imageData] waitUntilDone:NO];
                       });

因为我需要从不同的 viewController 调用它,所以我想将它放在具有公共全局帮助功能 (+) 的通用类中,所以我正在寻找一种方法将选择器和目标作为参数传递给 performSelectorOnMainThread 以运行

4

1 回答 1

2

你暗示你有目标。你为什么不直接用目标来调用它?

NSURL* urlAddressJSonUrl = [NSURL URLWithString:urlString];
dispatch_async(globalQueue, ^{
    NSData *imageData = [NSData dataWithContentsOfURL:urlAddressJSonUrl];

   [target performSelectorOnMainThread:@selector(setImage:)
                            withObject:[UIImage imageWithData:imageData]
                         waitUntilDone:NO];
});

或更好

NSURL* urlAddressJSonUrl = [NSURL URLWithString:urlString];
dispatch_async(globalQueue, ^{
    NSData *imageData = [NSData dataWithContentsOfURL:urlAddressJSonUrl];

    dispatch_async(dispatch_get_main_queue(), ^{
        [target setImage:[UIImage imageWithData:imageData]];
    });
});
于 2014-02-04T03:27:00.157 回答