I fetch
API 响应并解析 swift 中的选取 url。但我需要检查的是image url or video url :
如果我获得图像网址,则显示图像;如果获得视频网址,则播放视频:
if let url = postMedia?.url{
//need to check here
}
例如
这是我的视频网址:
https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4 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.mov
这是图像网址:
https://clips.vorwaerts-gmbh.de/big_buck_bunny.png https://clips.vorwaerts-gmbh.de/big_buck_bunny.png
https://clips.vorwaerts-gmbh.de/big_buck_bunny.jpg https://clips.vorwaerts-gmbh.de/big_buck_bunny.jpg
注意:我知道如何显示图像和播放视频
您可以检查如下图
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))")
}
同样你可以检查视频
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)