2
votes

I'm trying to convert a data:image/png;base64 to an UIImage, then get a CGImage and from the UIImage and send it to a Zebra printer.

I try this code:

NSData *datad = [[NSData alloc] initWithData:[NSData dataFromBase64String:@"data:image/png;base64,iVBORw0KGgoAAAANS...etc"]];
UIImage *image = [UIImage imageWithData:datad];
[image CGIamge]

And i'm trying with this code:

NSString *base64Url = @"data:image/png;base64,iVBORw0KGgoAAAANS...etc";
NSURL *url = [NSURL URLWithString:base64Url];
NSString *base64Image = [NSString stringWithContentsOfURL:url];
url = [NSURL URLWithString:base64Image];
NSData *rawImageData = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:rawImageData];

With the second part of the code i get a black image.

I'm doing something wrong for sure.

I'm also try to do this How to create CGImageRef from NSData string data (NOT UIImage)

aaaaand nothing.

Please, if someone could help me, you will be my heroe!

2

2 Answers

3
votes

You have to remove that data:image/png;base64, prefix, and only include the actual base64 payload (including any = characters at the end) when you call dataFromBase64String.

1
votes

You should try this:

NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters];
UIImage *image = [UIImage imageWithData:data];