我正在发送一个异步http GET请求并正在调用completionHandler。像NSLog这样的回调代码会运行,因为我可以看到日志中的输出。但是,行:self.imageView.image =零;似乎并没有生效,直到NSLog语句“到这里”几秒之后。有谁知道发生了什么?样本代码如下:iOS:NSURLConnection sendAsynchronousRequest NSImageView上的延迟操作
在ViewController.m:
@interface ViewController()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
@synthesize imageView = _imageView;
-void viewDidLoad {
// ImageView
UIImage *image = [UIImage imageNamed:@"test.jpg"];
self.imageView.backgroundColor = [UIColor blackColor];
self.imageView.clipsToBounds = YES;
self.imageView.image = image;
}
-void test {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://someurl"] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10];
[request setHTTPMethod:@"GET"];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
self.imageView.image = nil;
NSLog(@"got here");
}];
}
@end
尝试发送零到队列参数有completionHandler主线程 – Mattias 2013-02-27 08:32:53
上执行我试着设置队列:无,但会要求NSURLConnection的的completionHandler不会被调用。 – tom 2013-02-27 08:38:12