how to convert bytes array to image

Discussion in 'iOS Programming' started by MacccUser, Mar 19, 2014.

  1. MacccUser macrumors newbie

    Joined:
    Mar 19, 2014
    #1
    hello ! I've converted UIImage to bytes array.
    Code:
     NSString* filePath = [[NSBundle mainBundle] pathForResource:@"point" ofType:@"jpg"];
        NSData *data = [NSData dataWithContentsOfFile:filePath];
        unsigned char *bytesArray = data.bytes;
        NSUInteger lengthOfBytesArray = data.length;
        
         NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:data.length];
        if (data.length>0)
        {
           
            NSUInteger i = 0;
            for (i = 0; i<data.length; i++) {
                [array addObject:[NSNumber numberWithUnsignedChar:bytesArray[i]]];
    
             
            }
        }

    Now i need it back, I have no idea what should i do. Thanks )))
     
  2. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #2
    Where's the UIImage?

    You have converted an image file to an NSMutableArray containing data.length NSNumber objects. Is that really what you want?

    What do you mean by "need it back"?
     
  3. JohnsonK macrumors regular

    Joined:
    Mar 6, 2014
    #3
    Something like this?

    Code:
    UIImage *img = [UIImage imageWithData:[NSData dataWithBytes:data.bytes length:data.length]];
     
  4. MacccUser thread starter macrumors newbie

    Joined:
    Mar 19, 2014
    #4
    like this i created an empty UIImage *img. :confused:
     
  5. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #5
    What makes you think it is empty? If you tell us how you established that, we might be able to help :)
     
  6. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #6
    MacccUser, you should explan why you are doing this. Frankly, I can't think of a reason of converting an image to array of objects and back. When processing images manually, you acces the bytes directly via lower level methods.
     
  7. xArtx, Mar 21, 2014
    Last edited: Mar 21, 2014

    xArtx macrumors 6502a

    Joined:
    Mar 30, 2012
    #7
    If the slab ended up at the pointer ByteArray, he could have done anything with it up to and including rotating bytewise or bitwise, inverting bits, etc.
    Code:
    unsigned char *bytesArray = data.bytes;
    
    EDIT,, it looks like he's reading data from file because you're not supposed to be able to access data in an iOS UIImage object.
    But it looks like a funny array of arrays being created instead of a C array.
     
  8. xArtx, Mar 21, 2014
    Last edited by a moderator: Mar 21, 2014

    xArtx macrumors 6502a

    Joined:
    Mar 30, 2012
    #8
    It's hard to see what you're trying to do.
    I have code to share that will check your inbox for a bitmap,
    then determine the size and resolution from the bitmap header,
    then load that file's image data to a C array (and a UIImage for display only).
    It then encrypts or decrypts the image data only (not the header)
    using the first ten digits of the currently selected song title in iPod.
    Then writes back to file with the original bitmap header for email out.
    https://www.youtube.com/watch?v=HUx9w4gyQ5c
    bushprogrammer at the google email.
    Don't ask me to explain it though!
     

Share This Page