How can I check if an image-url-string gives back a valid url? I'm talking about this url http://rscagen3t004-dev.tdlinx.dev/sites/all/themes/rsca/resources/images/RSCA_logo.png
如何检查image-url-string是否返回有效的URL?我在谈论这个网址http://rscagen3t004-dev.tdlinx.dev/sites/all/themes/rsca/resources/images/RSCA_logo.png
You can see when you click on the link that the webpage is not available. At the moment I'm doing this in my code:
您可以看到单击链接时网页不可用。目前我在我的代码中这样做:
if([meta.met_thumb hasPrefix:@"http://"]){
imgURL = meta.met_thumb;
NSData *walImage = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imgURL]];
NSLog(@"walImage is %@",walImage);
float newHeight2;
if (walImage != nil) {
UIImage *imgIcon = [UIImage imageWithData:walImage];
if (imgIcon != nil) {
float width = imgIcon.size.width;
float heigt = imgIcon.size.height;
if(heigt < 240){
float newWidth = (heigt / 240);
float newWidht2 = (newWidth/ width);
//[imgNews setFrame:CGRectMake(40,y, newWidht2,heigt)];
}
float newHeight = (width / 240);
newHeight2 = heigt/ newHeight;
}else{
newHeight2 = 240;
}
}else{
newHeight2 = 240;
}
height = [self heightForStatus:attString] + y + newHeight2 + 70;
}else{
height = [self heightForStatus:attString] + y + 240 + 70;
}
But it takes a very long time to execute the line of the [[NSData alloc] init]
.
但是执行[[NSData alloc] init]的行需要很长时间。
Can anybody help me with this?
任何人都可以帮我吗?
Thanks in advance !
提前致谢 !
1 个解决方案
#1
0
The long delay in executing the line with alloc/init
is probably caused by the slow response of the server. I just tested and after 30 seconds I neither got the image nor the error page.
使用alloc / init执行该行的长时间延迟可能是由于服务器响应缓慢造成的。我刚刚测试过,30秒后我既没有图像也没有错误页面。
To detect a "valid URL" you should check the server response. It should give a proper response of 404 = not found. Also see http response codes http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
要检测“有效URL”,您应该检查服务器响应。它应该给出正确的响应404 =未找到。另请参阅http响应代码http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
So what you should do is check wether you get a 404 or the according 2xx response. Then decide if you have data and create your image. Still, that may go wrong, if for example no image data is received but a web page.
那么你应该做的就是检查你得到404或2xx响应。然后决定是否有数据并创建图像。但是,如果例如没有接收到图像数据而是网页,则可能出错。
#1
0
The long delay in executing the line with alloc/init
is probably caused by the slow response of the server. I just tested and after 30 seconds I neither got the image nor the error page.
使用alloc / init执行该行的长时间延迟可能是由于服务器响应缓慢造成的。我刚刚测试过,30秒后我既没有图像也没有错误页面。
To detect a "valid URL" you should check the server response. It should give a proper response of 404 = not found. Also see http response codes http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
要检测“有效URL”,您应该检查服务器响应。它应该给出正确的响应404 =未找到。另请参阅http响应代码http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
So what you should do is check wether you get a 404 or the according 2xx response. Then decide if you have data and create your image. Still, that may go wrong, if for example no image data is received but a web page.
那么你应该做的就是检查你得到404或2xx响应。然后决定是否有数据并创建图像。但是,如果例如没有接收到图像数据而是网页,则可能出错。