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

sher17 picture sher17 · Jul 24, 2013 · Viewed 10.8k times · Source

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

Answer

rptwsthi picture rptwsthi · Jul 24, 2013

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