///imageType枚挙
enum imageType{
/// - parameter imageType: JPG,PNG,Tiff,gif,webp,bmp,None
case JPG,PNG,Tiff,gif,webp,bmp,None
}
func contentTypeForImageData(data:NSData) -> imageType {
var c:UInt8?
data.getBytes(&c, length: 1)
switch c {
case 0xFF:
return imageType.JPG;
case 0x89:
return imageType.PNG
case 0x47:
return imageType.gif
case 0x49,0x4D:
return imageType.Tiff
case 0x42:
return imageType.bmp
case 0x52:
if (data.length < 12) {
return imageType.None;
}
let testString:NSString = NSString(data: data.subdata(with: NSMakeRange(0, 12)), encoding: String.Encoding.ascii.rawValue)!
if testString.hasPrefix("RIFF"),testString.hasSuffix("WEBP") {
return imageType.webp
}
default:return imageType.None
}
return imageType.None
}
网友评论