我有一个包含json格式的网址。
{
"name":"RAI Radiodue",
"cat":"Adult Contemporary",
"img":"http://www.rai.it/dl/portaleRadio/images/ico_radio2.jpg",
"url":"http://www.listenlive.eu/rai2.m3u"
},
{
"name":"RAI Radiotre",
"cat":"Classical/Culture",
"img":"http://www.rai.it/dl/portaleRadio/images/ico_radio3.jpg",
"url":"http://www.listenlive.eu/rai3.m3u"
}........etc
我正在尝试使用以下代码显示许多单选按钮:
for (int i = 0; i<[namesArray count]; i++) {
NSURL *url = [NSURL URLWithString: [pictureUrl objectAtIndex:i]];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImageView *subview = [[UIImageView alloc] init];
[subview setImage:[UIImage imageWithData:data]];
UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
int xxx = i%3;
int yyy = i/3.0f;
[bt setFrame:CGRectMake(35+95*xxx, 15+100*yyy, 60, 60)];
[bt setBackgroundImage:subview forState:UIControlStateNormal];
bt.tag = 200+i;
bt.selected = NO;
[bt.layer setMasksToBounds:NO];
[bt.layer setShadowColor:[UIColor blackColor].CGColor];
[bt.layer setShadowOpacity:0.8f];
[bt.layer setShadowOffset:CGSizeMake(1.0f, 1.0f)];
[bt addTarget:self action:@selector(Play:) forControlEvents:UIControlEventTouchUpInside];
[mainscroll addSubview:bt];
}
并为每个按钮添加一个已填充的pictureUrl中的图像,但是当我想要转到此视图控制器时,我在控制台中获得此输出:
2014-12-18 17:32:40.763 TV[3128:935103] NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)
2014-12-18 17:32:46.041 TV[3128:935134] void SendDelegateMessage(NSInvocation *): delegate (webView:decidePolicyForNavigationAction:request:frame:decisionListener:) failed to return after waiting 10 seconds. main run loop mode: kCFRunLoopDefaultMode.
我该怎么办?
答案 0 :(得分:0)
NSData *data = [NSData dataWithContentsOfURL:url];
此行阻塞主线程,主线程在运行此行之前等待数据下载:
UIImageView *subview = [[UIImageView alloc] init];
[subview setImage:[UIImage imageWithData:data]];
您必须在其他线程中运行下载数据,并在下载数据时设置图像。
答案 1 :(得分:0)
必须有pictureUrl数组出错,或者在运行代码时没有互联网连接。当我运行你的代码,传入你在帖子中显示的2个字符串时,代码工作正常,
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *stringsArray = @[@"http://www.rai.it/dl/portaleRadio/images/ico_radio2.jpg", @"http://www.rai.it/dl/portaleRadio/images/ico_radio3.jpg"];
[self downloadImagesFromNamesArray:stringsArray];
}
-(void)downloadImagesFromNamesArray:(NSArray *) namesArray {
for (int i = 0; i<[namesArray count]; i++) {
NSURL *url = [NSURL URLWithString: [namesArray objectAtIndex:i]];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [UIImage imageWithData:data];
UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
int xxx = i%3;
int yyy = i/3.0f;
[bt setFrame:CGRectMake(35+95*xxx, 15+100*yyy, 60, 60)];
[bt setBackgroundImage:img forState:UIControlStateNormal];
bt.tag = 200+i;
bt.selected = NO;
[bt.layer setMasksToBounds:NO];
[bt.layer setShadowColor:[UIColor blackColor].CGColor];
[bt.layer setShadowOpacity:0.8f];
[bt.layer setShadowOffset:CGSizeMake(1.0f, 1.0f)];
[bt addTarget:self action:@selector(Play:) forControlEvents:UIControlEventTouchUpInside];
[self.mainscroll addSubview:bt];
}
}
另请注意,我修复了您尝试使用图像视图而不是图像设置按钮背景图像的问题。
更好的下载方法是使用异步下载方法,如sendAsynchronousRequest:queue:completionHandler :(或NSURLSession方法)。如果您这样做,您将得到一些错误处理,并且在下载发生时不会阻止该应用程序。这样做时,你不能使用循环,而是应该从异步方法的完成块下载下一个图像。
-(void) downloadImagesFromNamesArray:(NSArray *) imageURLStrings {
static int count = 0;
NSString *urlString = imageURLStrings[count];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *req = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
[NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError) {
NSLog(@"%@", connectionError);
count ++;
if (count < imageURLStrings.count) {
[self downloadImagesFromNamesArray:imageURLStrings];
}
}else{
UIImage *img = [UIImage imageWithData:data];
if (img) {
UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
int xxx = count%3;
int yyy = count/3.0f;
[bt setFrame:CGRectMake(35+95*xxx, 15+100*yyy, 60, 60)];
[bt setBackgroundImage:img forState:UIControlStateNormal];
bt.tag = 200+ count;
bt.selected = NO;
[bt.layer setMasksToBounds:NO];
[bt.layer setShadowColor:[UIColor blackColor].CGColor];
[bt.layer setShadowOpacity:0.8f];
[bt.layer setShadowOffset:CGSizeMake(1.0f, 1.0f)];
[bt addTarget:self action:@selector(Play:) forControlEvents:UIControlEventTouchUpInside];
[self.mainscroll addSubview:bt];
}else{
NSLog(@"Data was received but no image was created from the data");
}
count ++;
if (count < imageURLStrings.count) {
[self downloadImagesFromNamesArray:imageURLStrings];
}
}
}];
}