如何查看图片网址是真的存在 - iOS?

时间:2016-11-29 03:32:41

标签: ios image url

在iOS中,我是否可以检查url是否是真实的图像路径?

如:

1)http://www.imgs.ie/uploads/banners/esb1.png

这是url的真实image路径,但如何检查它是否存在于代码中?

2)http://www.imgs.ie/uploads/banners/esb2.png

这是url的失败image路径,但如何检查代码中是否存在?

修改

感谢rmaddy的建议,我的要求是不要下​​载image,只是想知道网址是否是真实的图片网址,就像probe一样的网址。我们可以检查一下。

3 个答案:

答案 0 :(得分:0)

为什么不试试这个:

mysql> SELECT * FROM data WHERE ts BETWEEN '2016-11-27 00:11:00' AND '2016-11-29 00:11:00' AND id LIKE '0' OR id LIKE '1' ORDER BY ts ASC LIMIT 10;
+-------+---------------------+----+-------+
| idx   | ts                  | id | value |
+-------+---------------------+----+-------+
| 14117 | 2016-11-12 15:24:16 |  1 |  0.00 |
| 20144 | 2016-11-16 20:03:50 |  1 | 56.00 |
| 20147 | 2016-11-16 20:04:10 |  1 | 52.00 |
| 20150 | 2016-11-16 20:05:10 |  1 | 52.00 |
| 20153 | 2016-11-16 20:06:11 |  1 | 52.00 |
| 20156 | 2016-11-16 20:07:11 |  1 | 52.00 |
| 20159 | 2016-11-16 20:08:17 |  1 | 52.00 |
| 20162 | 2016-11-16 20:09:18 |  1 | 52.00 |
| 20165 | 2016-11-16 20:10:21 |  1 | 52.00 |
| 20168 | 2016-11-16 20:11:27 |  1 | 52.00 |
+-------+---------------------+----+-------+

当你想使用imageURL时:

var imageURL: NSURL? {
    didSet {
        image = nil
        if view.window != nil {
            fetchImage()
        }
    }
}

private func fetchImage() {
    if let url = imageURL {
        DispatchQueue.global(qos: .userInitiated).async {
            self.spinner?.startAnimating()
            let contentsOfURL = NSData(contentsOf: url as URL)
            DispatchQueue.main.async {
                if url == self.imageURL {
                    if let imageData = contentsOfURL {
                        self.image = UIImage(data: imageData as Data)
                    } else {
                        self.spinner?.stopAnimating()
                    }
                } else {
                    print("ignored data returned from url \(url)")
                }
            }
        }
    }
}

答案 1 :(得分:0)

uiimage *image = [uiimage imagewithurl:@"your url"];
if(image){
//exist
}

答案 2 :(得分:0)

你可以使用Alamofire:

Alamofire.request(.GET, completeURLImage).response { (request, response, data, error) in
 if data != nil{
     imgView.image = UIImage(data: data!, scale: 1)
    }
}