Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check url is image or video?

Tags:

ios

swift

I fetch API response and parse pick url in swift . but i need to check is image url or video url :

If i get image url then show image and if get video url then play video :

 if let url = postMedia?.url{

    //need to check here 
  }

For example

Here is my video url :

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

here is image url :

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

note : i know how to show image and play video

like image 562
cristainlika3 Avatar asked Jun 30 '17 10:06

cristainlika3


6 Answers

You can check image like below

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))")
            }

Same you can check for video

like image 117
Vibha Singh Avatar answered Nov 12 '22 12:11

Vibha Singh


Swift 3

Hope will help you

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
    }
}
like image 34
Jaydeep Vora Avatar answered Nov 12 '22 10:11

Jaydeep Vora


This answer asks the framework to check the filename extension.

#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;
}
like image 24
Jay Avatar answered Nov 12 '22 10:11

Jay


Check The extension of the url by following code:

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

if your extension is mp4 or mov it is video else if it is jpg, jpeg,png etc , it will be image

like image 20
Raman Srivastava Avatar answered Nov 12 '22 12:11

Raman Srivastava


Swift 3

Please try this one:

 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
        }

    }
like image 45
Kamal Thakur Avatar answered Nov 12 '22 12:11

Kamal Thakur


Swift

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)
    }
 }
like image 42
Alirza Eram Avatar answered Nov 12 '22 11:11

Alirza Eram