0

My requirement is that a local image will be converted to a byte array and then the byte array is converted back to the image

i have done the image to Byte array conversion but i trying bytearray convert to image and then display on UIIMageView code is no error but Image Converted to byte array but byte array converted io image but image is not display please give me any idea

Here is what I have so far

   UIImage *image = [UIImage imageNamed: @"shopper-home-copy_03.png"];




   //Output
   NSData *data = UIImagePNGRepresentation(image);

    NSUInteger len = data.length;

    uint8_t *bytes = (uint8_t *)[data bytes];

    NSMutableString *result = [NSMutableString stringWithCapacity:len * 3];


    [result appendString:@"["];

    for (NSUInteger i = 0; i < len; i++)
    {
        if (i)
        {

            [result appendString:@","];

        }

        [result appendFormat:@"%d", bytes[i]];
    }

    [result appendString:@"]"];
   NSMutableData *newdata = [NSMutableData new];
    NSScanner *theScanner = [NSScanner scannerWithString: result];
    while ([theScanner isAtEnd] == NO) {
        int a;
        [theScanner scanInt:&a];
        uint8_t b = a;
        [newdata appendBytes:(const void *)&b length:1];
    }

    UIImage *newImage = [UIImage imageWithData:newdata];
    self->imageView = [[UIImageView alloc] initWithImage:newImage];

    imageView.frame = CGRectMake(0, 25 , 150,150);

    [self.view addSubview:self->imageView];
Vasavi
  • 49
  • 1
  • 2
  • 7
  • Have a look, http://stackoverflow.com/questions/22316608/how-to-convert-byte-array-to-image-in-ios/22316710#comment33918939_22316710 – kukushi Mar 12 '14 at 09:09

4 Answers4

1

First of all you should check is Base64 is suitable for you. If you still need your own format try following

NSMutableData *newData = [NSMutableData new];
NSScanner *theScanner = [NSScanner scannerWithString: result];
theScanner.charactersToBeSkipped = [NSCharacterSet characterSetWithCharactersInString:@"[],"];
while ([theScanner isAtEnd] == NO) {
    int a;
    [theScanner scanInt:&a];
    uint8_t b = a;
    [newData appendBytes:(const void *)&b length:1];
}
UIImage *newImage = [UIImage imageWithData:newData];
Avt
  • 16,927
  • 4
  • 52
  • 72
  • NSMutableData *data = [NSMutableData new]; this line error – Vasavi Mar 12 '14 at 09:28
  • Redefinition of 'data' with a different type :'NSMutableData *_string' vs'NSdata *_string' this is error message – Vasavi Mar 12 '14 at 09:34
  • It is just because you already have `data` variable. I have renamed it with `newData` in the answer. – Avt Mar 12 '14 at 09:36
  • it's not Showing any error and then it's not Showing Image also please help me – Vasavi Mar 12 '14 at 09:54
  • I have just checked - everything works. Are you sure you did not forget to copy line `theScanner.charactersToBeSkipped = [NSCharacterSet characterSetWithCharactersInString:@"[],"];` ? I can not find it in code you copy-pasted to question. – Avt Mar 12 '14 at 10:26
0

Create an NSData object from the byte array. And then use this

UIImage *image = [UIImage imageWithData:data];
Shanti K
  • 2,873
  • 1
  • 16
  • 31
0

Try this to convert string to bytes array..

first step to convert mutable string to array.

NSArray *strings = [<mutableString> componentsSeparatedByString:@","];

unsigned c = strings.count; // strings is the array instance from the mutable string (separate using ",")
uint8_t *bytes = malloc(sizeof(*bytes) * c);

unsigned i;

for (i = 0; i < c; i++)

{
NSString *str = [strings objectAtIndex:i];

int byte = [str intValue];
bytes[i] = byte;
}

from bytes convert to image is as follows..

NSData *imageData = [NSData dataWithBytesNoCopy:bytes length:c freeWhenDone:YES];
UIImage *image = [UIImage imageWithData:imageData];

reference from Byte Array to NSData

Community
  • 1
  • 1
RAJA
  • 1,214
  • 10
  • 13
0

Convert byteArray to NSData and then to UIImage

-(UIImage *)convertToImageFromByteArray:(NSMutableString*)mutableStr{

    NSArray *byteArray = [mutableStr componentsSeparatedByString:@","];

    unsigned c = byteArray.count;
    uint8_t *bytes = malloc(sizeof(*bytes) * c);

    unsigned i;
    for (i = 0; i < c; i++)
    {
        NSString *str = [byteArray objectAtIndex:i];
        int byte = [str intValue];
        bytes[i] = (uint8_t)byte;
    }

    NSData *data = [NSData dataWithBytes:bytes length:c];

    return [UIImage imageWithData:data];
}
Himanshu Joshi
  • 3,391
  • 1
  • 19
  • 32