iOS 获取图片的格式

+ (NSString *)imageFormatFromImageData:(NSData *)imageData{
     
    
    uint8_t first_byte;
    [imageData getBytes:&first_byte length:1];
    switch (first_byte) {
     
        case 0xFF:
            return @"jpeg";
        case 0x89:
            return @"png";
        case 0x47:
            return @"gif";
        case 0x49:
        case 0x4D:
            return @"tiff";
        case 0x52:
            if ([imageData length] < 12) {
     
                return @"";
            }
            NSString *dataString = [[NSString alloc] initWithData:[imageData subdataWithRange:NSMakeRange(0, 12)] encoding:NSASCIIStringEncoding];
            if ([dataString hasPrefix:@"RIFF"] && [dataString hasSuffix:@"WEBP"]) {
     
                return @"webp";
            }
            return @"";
    }
    return nil;
}

你可能感兴趣的:(iOS,获取图片格式,图片,格式)