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

冷暖自知 提交于 2019-12-04 13:14:18

问题


I found the below code in Finding image type from NSData or UIImage which helps to check four different image types of a UIimage

     (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:
    case 0x4D:
        return @"image/tiff";
    }
    return nil;
}

I want to know how to find the file is a bitmap image or not that it has .bmp extension. can someone please help me with it. Either modify the above code to find bmp as well or please provide me a solution with some code.

thanks in adnvance


回答1:


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;
}



回答2:


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

case 0x42:
    return @"image/bmp";


来源:https://stackoverflow.com/questions/17828044/is-it-possible-to-find-the-file-extension-of-a-uiimage

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