如何查看网址是图片还是视频?

cri*_*ka3 2 ios swift

fetch 在swift中的API响应和解析pick url.但我需要检查图像网址或视频网址:

如果我得到图像网址然后显示图像,如果获得视频网址,然后播放视频:

 if let url = postMedia?.url{

    //need to check here 
  }
Run Code Online (Sandbox Code Playgroud)

例如

这是我的视频网址:

https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4
https://clips.vorwaerts-gmbh.de/big_buck_bunny.mov

这是图片网址:

https://clips.vorwaerts-gmbh.de/big_buck_bunny.png
https://clips.vorwaerts-gmbh.de/big_buck_bunny.jpg

注意:我知道如何显示图像和播放视频

Vib*_*ngh 9

你可以检查下面的图像

let url1 : String = "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"
        let imageExtensions = ["png", "jpg", "gif"]
        //...
        // Iterate & match the URL objects from your checking results
        let url: URL? = NSURL(fileURLWithPath: url1) as URL
        let pathExtention = url?.pathExtension
            if imageExtensions.contains(pathExtention!)
            {
                print("Image URL: \(String(describing: url))")
                // Do something with it
            }else
            {
               print("Movie URL: \(String(describing: url))")
            }
Run Code Online (Sandbox Code Playgroud)

同样你可以查看视频

  • 如果 url 没有扩展名怎么办?就像“https://sub.domain.com/video”一样。 (3认同)

Jay*_*eep 5

迅捷3

希望能帮到您

extension String {
    public func isImageType() -> Bool {
        // image formats which you want to check
        let imageFormats = ["jpg", "png", "gif"]

        if URL(string: self) != nil  {

            let extensi = (self as NSString).pathExtension

            return imageFormats.contains(extensi)
        }
        return false
    }
}
Run Code Online (Sandbox Code Playgroud)