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

时间:2017-06-30 10:03:23

标签: ios swift

fetch API响应并在swift中解析pick 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.mov

这是图片网址:

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

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

6 个答案:

答案 0 :(得分: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))")
            }

同样可以查看视频

答案 1 :(得分:4)

Swift 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
    }
}

答案 2 :(得分:2)

通过以下代码检查网址的扩展名:

NSString *stringURL = @"http://...";
NSURL *url = [NSURL URLWithString:stringURL];
NSString *path = [url path];
NSString *extension = [path pathExtension];

如果你的扩展名是mp4或者移动它是视频,如果它是jpg,jpeg,png等,它将是图像

答案 3 :(得分:1)

Swift 3

请尝试这个:

 extension String {

        public func isImage() -> Bool {
            // Add here your image formats.
            let imageFormats = ["jpg", "jpeg", "png", "gif"]

            if let ext = self.getExtension() {
                return imageFormats.contains(ext)           
            }

            return false
        }

        public func getExtension() -> String? {
           let ext = (self as NSString).pathExtension

           if ext.isEmpty {
               return nil
           }

           return ext
        }

        public func isURL() -> Bool {
           return URL(string: self) != nil
        }

    }

答案 4 :(得分:0)

此答案要求框架检查文件扩展名。

#import <MobileCoreServices/MobileCoreServices.h>

// ...

+ (BOOL) urlIsImage:(NSURL*)fileUrl
{
    if (fileUrl) {
        NSString *extension = fileUrl.pathExtension;
        if (extension && extension.length > 0) {
            CFStringRef uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)extension, NULL);
            return uti && UTTypeConformsTo(uti, kUTTypeImage);
        }
    }

    return NO;
}

答案 5 :(得分:0)

迅捷

import MobileCoreServices

extension URL {
    func mimeType() -> String {
         let pathExtension = self.pathExtension
         if let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension as NSString, nil)?.takeRetainedValue() {
             if let mimetype = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType)?.takeRetainedValue() {
                return mimetype as String
             }
         }
         return "application/octet-stream"
    }

    var containsImage: Bool {
        let mimeType = self.mimeType()
        guard let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, mimeType as CFString, nil)?.takeRetainedValue() else {
             return false
        }
        return UTTypeConformsTo(uti, kUTTypeImage)
    }

    var containsAudio: Bool {
        let mimeType = self.mimeType()
        guard let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, mimeType as CFString, nil)?.takeRetainedValue() else {
              return false
        }
        return UTTypeConformsTo(uti, kUTTypeAudio)
    }
    var containsVideo: Bool {
        let mimeType = self.mimeType()
        guard  let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, mimeType as CFString, nil)?.takeRetainedValue() else {
               return false
        }
        return UTTypeConformsTo(uti, kUTTypeMovie)
    }
 }