当前位置: 代码迷 >> Iphone >> ios检测网络图片是不是存在
  详细解决方案

ios检测网络图片是不是存在

热度:73   发布时间:2016-04-25 05:49:05.0
ios检测网络图片是否存在
有没有什么办法可以判断网上的一张图片是否存在的?我用uiwebview来显示网络图片的时候出现了一个问题,那就是网络图片有的已经不存在了不能够访问了,显示404的错误,我想把这样的url链接去掉,可是不知道怎么来检测这张图片是否存在
------解决方案--------------------
先用
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];  

判断一下当前的链接。
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    if ((([httpResponse statusCode]/100) == 2)) {
        // self.earthquakeData = [NSMutableData data];
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
        
        [ webView loadRequest:[ NSURLRequest requestWithURL: myUrl ]];
        webView.delegate = self;
    } else {
        NSDictionary *userInfo = [NSDictionary dictionaryWithObject:
                                  NSLocalizedString(@"HTTP Error",
                                                    @"Error message displayed when receving a connection error.")
                                                             forKey:NSLocalizedDescriptionKey];
        NSError *error = [NSError errorWithDomain:@"HTTP" code:[httpResponse statusCode] userInfo:userInfo];
        
        if ([error code] == 404) {
            NSLog(@"网络图片不存在");
            webView.hidden = YES; //隐藏webView或者用本地默认图片代替
        }        
        
    }
}

------解决方案--------------------
不知道楼主具体是怎么实现的,我是利用js实现的,在img标签中加上onerro,不只是不存在,要是访问出错也会座对应的操作
  相关解决方案