通过图片NSData数据来获取图片格式
2018-06-04 本文已影响13人
伯牙呀
+ (NSString *)imageTypeWithData:(NSData *)data {
uint8_t type;
[data getBytes:&type length:1];
switch (type) {
case 0xFF:
return @"jpeg";
case 0x89:
return @"png";
case 0x47:
return @"gif";
case 0x49:
case 0x4D:
return @"tiff";
case 0x52:
if([data length] < 12) {
return nil;
}
NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, 12)] encoding:NSASCIIStringEncoding];
if([testString hasPrefix:@"RIFF"] && [testString hasSuffix:@"WEBP"]) {
return@"webp";
}
return nil;
}
return nil;
}