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
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
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
}
}
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;
}
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
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
}
}
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)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With