我有一个使用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方法完成我在第一个代码摘录中所做的事情?
发布于 2013-02-13 00:59:18
在发送asynchronousRequest方法中,您的URL字符串缺少一部分。它应该与您的第一个有效方法类似:
[[self.facebookPhotosAll objectAtIndex:self.randomIndex1] objectForKey:@"pic_big"]https://stackoverflow.com/questions/14837183
复制相似问题