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];
1

4 Answers 4

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];
Sign up to request clarification or add additional context in comments.

5 Comments

NSMutableData *data = [NSMutableData new]; this line error
Redefinition of 'data' with a different type :'NSMutableData *_string' vs'NSdata *_string' this is error message
It is just because you already have data variable. I have renamed it with newData in the answer.
it's not Showing any error and then it's not Showing Image also please help me
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.
0

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

UIImage *image = [UIImage imageWithData:data];

Comments

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];
}

3 Comments

I want Byte array convert to image
You have said that you want to convert NSMUtableString
if you want to convert byteArray then simply change the function to -(UIImage *)convertToImageFromByteArray:(NSArray*)byteArray and remove the line NSArray *byteArray = [mutableStr componentsSeparatedByString:@","];.
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

4 Comments

thanks fro Replay but i using NSMutableString unsigned c = strings.count; NSString *str = [strings objectAtIndex:i]; this 2 lines are Error please help me
Raja propetry 'count' not found on Object of type 'NSmutbaleString *' and NO Visible @interface for 'NSMutableString' declares the selecor 'objectAtIndex:'
Are you using NSMutableString?
@Raja Image is Not display

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.