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 convert UIImage to NSData?

SherihanSherihan Sri LankaPosts: 24New Users @
I want to find the image type (file extension )of a UIImage. but before doing that I need to convert my UIImage to NSData.

//functions checks the type of the image passed - (NSString *)contentTypeForImageData:(UIImage *)image { NSData *imageData = UIImageJPEGRepresentation(image, 1.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; }
Problem is this is not giving me the correct output but always gives as bmp. But the code is working for those who passed the image as a URL and get NSData from the URL itself. I want to know why the code is not working as expected.

I think the way I convert the UIImage to NSData is not correct. I want to know is there some other approach to do that without using UIImageJPEGRepresentation or UIImagePNGRepresentation? please give a help

thanks in adnvance
Tagged:

Replies

  • chuckc192000chuckc192000 FloridaPosts: 394New Users @ @
    Once you load the image data into a UIImage object, information about the original file is lost. The UIImage format converts the image data in the file to an internal bitmap format. To find out info about the original file, you'll have to load the file directly into an NSData object (using +dataWithContentsOfURL or -initWithContentsOfURL).
  • SherihanSherihan Sri LankaPosts: 24New Users @
    Hi, thanks for the reply. Actually, in this application I'm allowing the user to select an image from the PictureLibrary and displaying in a UIImageView. If needs to validate the image in the UIImageView is taken as an input to the function-->convert to NSData-->then checks for file type as above.

    But the ultimate goal is to make a phonegap application where the image will be selected from the html side and passed to the native plugin class as a Url. So, I hop your solution would work as expected with that approach.
  • Duncan CDuncan C Posts: 9,116Tutorial Authors, Registered Users @ @ @ @ @ @ @
    Sherihan said:

    Hi, thanks for the reply. Actually, in this application I'm allowing the user to select an image from the PictureLibrary and displaying in a UIImageView. If needs to validate the image in the UIImageView is taken as an input to the function-->convert to NSData-->then checks for file type as above.

    But the ultimate goal is to make a phonegap application where the image will be selected from the html side and passed to the native plugin class as a Url. So, I hop your solution would work as expected with that approach.


    The code you posted makes no sense. You're taking a UIImage, converting it to a JPEG (Thus the "JPEG" in the method name UIImageJPEGRepresentation) then trying to look at the resulting byte stream to find signature bytes to figure out the format of the original image.

    If you are looking at the byte stream you get back from UIImageJPEGRepresentation and deciding it's ever something other than a JPEG, you are doubly confused.

    You might be able to ask the UIImage for it's underlying CGImage object, get the bytes for that CGImage, and interrogate that to figure out the underlying type of the image data, but I'm not sure.

    On Mac OS an NSImage object contains one or more NSImageRep objects that preserve information about the source image type used to create them. I'm not sure if the same is true about the CGImage objects inside a UIImage.

    Looking at the CGImage reference, there is a function CGImageGetTypeID that you might be able to use to interrogate the source image type of the data for the image. I haven't used it before, but it's worth a shot.
    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 @
    edited July 2013
    Hi, thanks fro the reply. I could figure out the problem. Here's what I'm doing.
    I have a button control in my html (in the phoneGap application) page as below
    anyone can refer the phoneGap documentation in the following link. It's so helpful.I have used version 2.9.0
    http://docs.phonegap.com/en/2.9.0/cordova_camera_camera.md.html#cameraOptions

    <button onclick="getPhoto(Camera.pictureSource.PHOTOLIBRARY);">From Photo Library</button>
    and in my javascript, I have the function getPhoto() as below

    function getPhoto(source) { // Retrieve image file location from specified source navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 100, destinationType: navigator.camera.DestinationType.NATIVE_URI, sourceType: source }); }
    once, a photo is selected, this delegate will be called

    function onPhotoURISuccess(imageURI) { // Get image handle var largeImage = document.getElementById('largeImage'); // Unhide image elements largeImage.style.display = 'block'; // Show the captured photo // The inline CSS rules are used to resize the image largeImage.src = imageURI; checkFileExtention(imageURI); }
    checkFileExtention() function asynchronously calls the method of file extension checking at the native side. For the simplicity, I post only the application logic part

    //funtions checks the type of the image passed - (NSString *)contentTypeForImageData:(NSString *)imageUrl { NSURL *imageURL = [NSURL URLWithString:imageUrl]; NSData *imageData = [NSData dataWithContentsOfURL:imageURL]; 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; }
    and the process works as expected.
    I think the breakthrough to solve the issue was identifying that there are three diffrrent destination types in phoneGap camera as below

    Camera.DestinationType = { DATA_URL : 0, // Return image as base64-encoded string FILE_URI : 1, // Return image file URI NATIVE_URI : 2 // Return image native URI (e.g. assets-library:// on iOS or content: };
    FILE_URI returns saves the image locally and sends a URI which looks like below
    file://localhost/Users/user/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/B50E3AD6-74F8-43F6-9F91-F28D2B06DF62/tmp/cdv_photo_116.jpg

    apparently, the file type is hidden because it's always indicating as jpg

    when NATIVE_URI is used, it looks like this
    assets-library://asset/asset.JPG?id=5CF16D20-9A80-483A-AC1C-9692123610B1&ext=JPG
    note that above uri shows JPG becasue the image is JPG, else the real file extension of the selected image is shown.

    ultimately, if when the native uri is sent to the contentTypeForImageData funtion, it gives me the correct output :)

    from this post, I understood that when converting an image to UIImage or getting a picked image from the library as UIImage, it hides most of the image's underlying data. chuckc192000's reply was so useful since I could figure out about the behavior of UIImage finally

    chuckc192000:
    Once you load the image data into a UIImage object, information about the original file is lost. The UIImage format converts the image data in the file to an internal bitmap format. To find out info about the original file, you'll have to load the file directly into an NSData object (using +dataWithContentsOfURL or -initWithContentsOfURL).
    thanks to all, this thread was resourceful !
Sign In or Register to comment.