5

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

Community
  • 1
  • 1
sher17
  • 607
  • 1
  • 12
  • 31

2 Answers2

5

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;
}
rptwsthi
  • 10,094
  • 10
  • 68
  • 109
  • 1
    Hi, thanks for the response but I heard that this approach wont work with a UIImage because converting an image to UIImage hides all it's underlying data and creates it as a bitmap image. http://iphonedevsdk.com/discussion/comment/471725#Comment_471725 may be that's why the above approach (that's what I exactly tried for few hours ago) always return a bmp as a result for any kind of image. So, I corrected the function to get the url of the image rather than taking as a UIImage. – sher17 Jul 24 '13 at 19:50
  • how you get the image? – rptwsthi Jul 24 '13 at 20:41
  • It's a phoneGap application, so I use getPhoto(source) method of it where the onPhotoURISuccess(imageURI) method provides the image's URI. Check this http://stackoverflow.com/questions/17873816/how-to-get-image-metadata-from-native-uri-in-ios FILE_URI always returned me a jpeg image.If used NATIVE_URI instead of FILE_URI, the image's underlying data wont be lost. actually if I passed the NATIVE_URI to the above shown contentTypeForImageData() function where I directly convert the URI to NSData, the function returns the correct output ! – sher17 Jul 28 '13 at 17:44
  • 6
    If you did UIImagePNGRepresentation, you already know it's a PNG. – Aaron Zinman Oct 23 '14 at 19:10
1

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

case 0x42:
    return @"image/bmp";
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
  • Hi, thanks for the reply. Does Wiki say 0x42 or 0x4D both bmp? but case 0x4D:return @"image/tiff"; already recognizes it as tiff, right? so, is it correct for me to check only 0x4D for BMPs? Sorry, I don't have an idea about this. correct me please. – sher17 Jul 24 '13 at 08:18
  • Does anyone know how the UIimage should be converted to NSdata before sending to this method. I used NSData *imageData=UIimageJPEGRepresentatio(image, 1.0); and even used PNGRepresentation as well. but it always returns it as bmp – sher17 Jul 24 '13 at 09:16