Reputation: 49
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];
Upvotes: 0
Views: 1534
Reputation: 17043
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];
Upvotes: 1
Reputation: 1214
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
Upvotes: 0
Reputation: 3399
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];
}
Upvotes: 0
Reputation: 2918
Create an NSData object from the byte array. And then use this
UIImage *image = [UIImage imageWithData:data];
Upvotes: 0