我在 Xcode 4.5.2 中有一个应用程序。它发送一个 URL 请求以下载图像并将图像设置在图像视图中。我有以下代码可以很好地完成此任务:
dispatch_queue_t downloadQueue = dispatch_queue_create("Get Facebook Friend", NULL);
dispatch_async(downloadQueue, ^{
self.firstFriendImage = [UIImage imageWithData:
[NSData dataWithContentsOfURL:
[NSURL URLWithString:
[[self.facebookPhotosAll objectAtIndex:self.randomIndex1]
objectForKey:@"pic_big"]]]];
dispatch_async(dispatch_get_main_queue(), ^{
[self postDownloadTasks:self.topView setLabel:self.firstFriendLabel
withFriendName:self.firstFriendName cropImage:self.firstFriendImage
inImageView:self.friendOneView atYPoint:22];
});
});
因此,虽然这段代码运行良好,但对于 Objective C 来说是新手,但我正在尝试稍微探索一下该语言,看看我还能如何做同样的事情。所以我尝试使用 NSURLConnection sendAsynchronousRequest: 方法(在查看了这里的一些示例之后),但我似乎无法让这个方法工作。这就是我所做的:
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
NSString *string = [[self.facebookPhotosAll
objectAtIndex:self.randomIndex1]objectForKey:@"pic_big"];
[NSURLConnection sendAsynchronousRequest: [NSURL URLWithString:
string] queue:queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
// Make sure eveything is ok
if(error){
// do something
}
self.firstFriendImage = [UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
[self postDownloadTasks:self.topView setLabel:self.firstFriendLabel
withFriendName:self.firstFriendName cropImage:self.firstFriendImage
inImageView:self.friendOneView atYPoint:22];
});
}];
所以这段代码根本不起作用(它没有返回任何数据)。调用该方法时应用程序崩溃,我收到以下消息:
** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSURL _CFURLRequest]:无法识别的选择器已发送到实例 0xa39dd20”
谁能告诉我如何使用 NSURLConnection sendAsynchronousRequest 方法完成我在第一个代码摘录中所做的事情?