Get image file type programmatically in fast mode

I load parsing images into PNG and JPEG files.

When the image is uploaded to the application, I need to determine what type of file so that I can process the image accordingly.

I looked at the API for uiimageview and did a search, but could not find any solution in swift.

Any input is appreciated.

Trying to get URL from PFFIle ยง:

let imageURLFromParse = NSURL(string : caseImageFile2.url); // Error here: 'NSURL?' does not have a member named 'pathExtension' if(imageURLFromParse.pathExtension!.lowercaseString == ".jpg" || imageURLFromParse.pathExtension.lowercaseString == ".jpeg"){ println("Parse image ext is a jpg: \(imageURLFromParse.pathExtension.lowercaseString)"); fileExtenion = ".jpg"; } else { println("Parse image is a png: \(imageURLFromParse.pathExtension.lowercaseString)"); fileExtenion = ".png"; } 
+6
source share
5 answers

Update for Swift 3.0.2

Based on answer from Hoa and Kingfisher Library

 import UIKit import ImageIO struct ImageHeaderData{ static var PNG: [UInt8] = [0x89] static var JPEG: [UInt8] = [0xFF] static var GIF: [UInt8] = [0x47] static var TIFF_01: [UInt8] = [0x49] static var TIFF_02: [UInt8] = [0x4D] } enum ImageFormat{ case Unknown, PNG, JPEG, GIF, TIFF } extension NSData{ var imageFormat: ImageFormat{ var buffer = [UInt8](repeating: 0, count: 1) self.getBytes(&buffer, range: NSRange(location: 0,length: 1)) if buffer == ImageHeaderData.PNG { return .PNG } else if buffer == ImageHeaderData.JPEG { return .JPEG } else if buffer == ImageHeaderData.GIF { return .GIF } else if buffer == ImageHeaderData.TIFF_01 || buffer == ImageHeaderData.TIFF_02{ return .TIFF } else{ return .Unknown } } } 

Using

 let imageURLFromParse = NSURL(string : "https://i.stack.imgur.com/R64uj.jpg") let imageData = NSData(contentsOf: imageURLFromParse! as URL) print(imageData!.imageFormat) 

You can use this with both local and online images.

+10
source

You should get the first byte of your image in binary format. This byte indicates what type of image. Here is the code I used for my project, but in Objective-c:

 uint8_t c; [_receivedData getBytes:&c length:1]; NSString *extension = @"jpg"; switch (c) { case 0xFF: { extension = @"jpg"; } case 0x89: { extension = @"png"; } break; case 0x47: { extension = @"gif"; } break; case 0x49: case 0x4D: { extension = @"tiff"; } break; default: FLog(@"unknown image type"); } 

Try this with swift (1.2, otherwise you should use var ext ):

 func imageType(imgData : NSData) -> String { var c = [UInt8](count: 1, repeatedValue: 0) imgData.getBytes(&c, length: 1) let ext : String switch (c[0]) { case 0xFF: ext = "jpg" case 0x89: ext = "png" case 0x47: ext = "gif" case 0x49, 0x4D : ext = "tiff" default: ext = "" //unknown } return ext } 
+4
source

I wanted to know which extension is my image after I selected it. I used this:

 func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { if (!(picker.sourceType == UIImagePickerControllerSourceType.Camera)) { let assetPath = info[UIImagePickerControllerReferenceURL] as! NSURL if assetPath.absoluteString.hasSuffix("JPG") { 
+1
source

Why not just do the following:

 let ext = NSURL(fileURLWithPath: path/of/your/file as! String).pathExtension print(ext!) 

This will give you the file extension.

0
source

You can check in the project https://github.com/bonyadmitr/ImageFormat

Add to project

 import Foundation /// can be done "heic", "heix", "hevc", "hevx" enum ImageFormat: String { case png, jpg, gif, tiff, webp, heic, unknown } extension ImageFormat { static func get(from data: Data) -> ImageFormat { switch data[0] { case 0x89: return .png case 0xFF: return .jpg case 0x47: return .gif case 0x49, 0x4D: return .tiff case 0x52 where data.count >= 12: let subdata = data[0...11] if let dataString = String(data: subdata, encoding: .ascii), dataString.hasPrefix("RIFF"), dataString.hasSuffix("WEBP") { return .webp } case 0x00 where data.count >= 12 : let subdata = data[8...11] if let dataString = String(data: subdata, encoding: .ascii), Set(["heic", "heix", "hevc", "hevx"]).contains(dataString) ///OLD: "ftypheic", "ftypheix", "ftyphevc", "ftyphevx" { return .heic } default: break } return .unknown } var contentType: String { return "image/\(rawValue)" } } 

Using

 for file in ["1.jpg", "2.png", "3.gif", "4.svg", "5.TIF", "6.webp", "7.HEIC"] { if let data = Data(bundleFileName: file) { print(file, ImageFormat.get(from: data)) } } /// Result /// 1.jpg jpg /// 2.png png /// 3.gif gif /// 4.svg unknown /// 5.TIF tiff /// 6.webp webp /// 7.HEIC heic 
0
source

Source: https://habr.com/ru/post/985166/


All Articles