'How to check url is image or video?
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
Solution 1:[1]
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
}
}
Solution 2:[2]
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;
}
Solution 3:[3]
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
Solution 4:[4]
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
}
}
Solution 5:[5]
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)
}
}
Solution 6:[6]
if you are sure that the file can only be a photo or a video you can use this simple code:
if let data = try? Data(contentsOf: YOUR_LOCAL_URL) {
if let _: UIImage = UIImage(data: (data)) {
print("image!")
} else {
print("video!")
}
}
Solution 7:[7]
Use import UniformTypeIdentifiers
extension URL {
var isImageFile: Bool {
UTType(filenameExtension: pathExtension)?.conforms(to: .image) ?? false
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | MilanPanchal |
| Solution 2 | Jay |
| Solution 3 | Raman Srivastava |
| Solution 4 | Cœur |
| Solution 5 | Alirza Eram |
| Solution 6 | thomas_994 |
| Solution 7 | onmyway133 |
