Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to find the file extension of a UIImage is BMP or not

SherihanSherihan Sri LankaPosts: 24New Users @
I found the below code in http://stackoverflow.com/questions/4147311/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
Tagged:

Replies

  • nobre84nobre84 Posts: 981Registered Users @ @ @
    The header for BMP would start with 0x42. Just look at it with an hex editor and check out the first byte
  • chuckc192000chuckc192000 FloridaPosts: 394New Users @ @
    I think this method only works if you read the image file data directly into an NSData object (i.e., not into an UIImage object). I think when you load an image into a UIImage object, it creates a bitmap from the image data in the file and throws away details of the underlying file.
  • SherihanSherihan Sri LankaPosts: 24New Users @
    edited July 2013
    Hi,
    thanks for the reply. but I tried with 0x42 as well. But it always replies bmp for me.

    //funtions checks the type of the image passed - (NSString *)contentTypeForImageData:(UIImage*)image { //NSData *imageData =UIImagePNGRepresentation(image); NSData *imageData =UIImageJPEGRepresentation (image,0); uint8_t c; [imageData 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"; case 0x42: return @"@image/bmp"; } return nil; }
    that's my code. How can I modify it to get the correct output? I mean is there a correct way to convert the UIImage to NSData rather than the above two techniques?
  • chuckc192000chuckc192000 FloridaPosts: 394New Users @ @
    Like I said in your other thread, this method won't work. You need to load the file/URL into an NSData object directly (not using UIImage) to examine the contents of the file.

  • Duncan CDuncan C Posts: 9,116Tutorial Authors, Registered Users @ @ @ @ @ @ @
    As I said in your previous post, you can't convert a UIImage to a JPEG representation and then expect to find out the format of the original file.

    How are you getting this image in the first place? Look further up in your code. Are you being given a URL, or a file path? At that point you can examine the file extension, or read the file as data rather than as an image.
    Sherihan said:

    Hi,
    thanks for the reply. but I tried with 0x42 as well. But it always replies bmp for me.


    //funtions checks the type of the image passed - (NSString *)contentTypeForImageData:(UIImage*)image { //NSData *imageData =UIImagePNGRepresentation(image); NSData *imageData =UIImageJPEGRepresentation (image,0); uint8_t c; [imageData 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"; case 0x42: return @"@image/bmp"; } return nil; }
    that's my code. How can I modify it to get the correct output? I mean is there a correct way to convert the UIImage to NSData rather than the above two techniques?
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • SherihanSherihan Sri LankaPosts: 24New Users @
    Hi all,
    I'm out of the frying pan into the fire now :(
    Firstly I tried to obtain the UIImage of a user's selection from the photolibrary. At the same time I want to validate that the user has selected a bmp or png only because it’s going to be used as a logo. Anyways converting the UIImage to NSData is useless in the way I tried out and what you friends have mentioned above. Since my final product is a phonegap application, I’m using camera.getPicture() method to open up the photo library and let the user select an image. this is my reference

    http://docs.phonegap.com/en/2.9.0/cordova_camera_camera.md.html#Camera

    We can get the image as a base64 string or can get its URI and use in our application in the function onPhotoURISuccess(imageURI).
    I select the URI because It is easy for me to process. Even I assumed the above function’s problem will be solved by that. Again I got stuck because the image is returned always as jpeg and even the image’s fileSize cannot be measured. I traced the URI by debugging and it’s always similar to the below file://localhost/Users/user/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/B50E3AD6-74F8-43F6-9F91-F28D2B06DF62/tmp/cdv_photo_116.jpg
    It’s trying to save the image locally and takes any image with the extension of jpg....
    I’m wondering how to solve this issue.
    Anyone have any idea on how we can get the URL of the image from this approach? Please share your ideas, is possible with some code samples.
  • Son of a BeachSon of a Beach Tasmania, AustraliaPosts: 225Registered Users @ @
    edited July 2013
    I don't think you've yet understood what everybody has been telling you here. A UIImage does not have any such type at all. Converting it to data by means other than "UIImageJPEGRepresentation" or "UIImagePNGRepresentation" is not going to help you at all, because it isn't going to get an image type from the UIImage, because the UIImage does not have an image type (and using those methods... well, you'll know what type the resulting data is, because you're specifying the type in the method).

    Let me say that again... a UIImage does not have an image file type.

    When loading a UIImage from a file, it is converted from it's file type into raw image data (usually raw RGB or RGBA bytes). This data format is completely unrelated to the format of the data as it was stored on disk. The types you are talking about are file formats, not in-memory image data formats. UIImage is an in-memory image, not a file.

    If you want to know what file format an image was stored in, you have to get access to the original file data. The UIImage cannot help you with that.
  • SherihanSherihan Sri LankaPosts: 24New Users @
    edited July 2013
    Hi, thanks for the reply. Without getting the UIImage but using the image uri I could solve the issue. I posted the here. http://iphonedevsdk.com/forum/iphone-sdk-development/114746-how-to-convert-uiimage-to-nsdata.html#latest
Sign In or Register to comment.