Is it possible to find the file extension of a UIImage ?

我的梦境 提交于 2019-12-03 08:04:14

You can, change image to NSData by using
UIImageJPEGRepresentation(<#UIImage *image#>, <#CGFloat compressionQuality#>)
OR
UIImagePNGRepresentation(<#UIImage *image#>)
method. Call it in this way:

- (void) yourMethod{
    NSData *imageData = UIImagePNGRepresentation(yourImage);
    NSString *str = [self contentTypeForImageData:imageData];

}

- (NSString *)contentTypeForImageData:(NSData *)data {
    uint8_t c;
    [data getBytes:&c length:1];

    switch (c) {
        case 0xFF:
                return @"image/jpeg";
        case 0x89:
                return @"image/png";
        case 0x47:
                return @"image/gif";
        case 0x49:
            break;
        case 0x42:
            return @"image/bmp";
        case 0x4D:
            return @"image/tiff";
    }
    return nil;
}

According to wikipedia a non-OS/2 BMP file start's with 0x42 0x4D (BM in ascii).

case 0x42:
    return @"image/bmp";
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!