我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
注意:我知道如何显示图像和播放视频
你可以检查下面的图像
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)
同样你可以查看视频
迅捷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)
归档时间: |
|
查看次数: |
3473 次 |
最近记录: |