0

I have looked at similar answers to this type of question but am still falling short of converting a Base64 encoded string into a UIImage correctly. I have used http://www.freeformatter.com/base64-encoder.html to test my string and can successfully get an image back as a response.

However, when I use initWithBase64EncodedString, I get a nil response. Below is the code I am using and an example of the Base64 string that I used to test with. What am I missing here?

Code:

imageData = [[NSData alloc] initWithBase64EncodedString:checkBytesString options:0]; checkImage = [UIImage imageWithData:imageData];

String (It is rather large so I am sharing it via OneDrive):

https://onedrive.live.com/redir?resid=60AA391B8FEA9C36!107&authkey=!AFK_y5UHOFsdYsKZI&ithint=file%2c.rtf

2 Answers 2

2

Answer was to use an external library, NSData+Base64. It implements method dataFromBase64String that returned imageData properly so it could be converted into an image.

https://github.com/l4u/NSData-Base64

imageData = [NSData dataFromBase64String:frontCheckBytesString];
checkImage = [UIImage imageWithData:imageData];
Sign up to request clarification or add additional context in comments.

3 Comments

Your code showing no known method for dataFromBase64String.
@Muju My answer was using an external library, I updated to include the link.
sorry I have not seen your whole answer. Your answer solve my problem. Thanks you very much.
1

Simple in iOS 7 onwards

- (UIImage *)decodeBase64ToImage:(NSString *)strEncodeData
{
    NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters];
    return [UIImage imageWithData:data];
}

UIImage *zeroImage = [[UIImage alloc]init];
if(![strb64Image isEqualToString:@""] && strb64Image)
{
    zeroImage = [self decodeBase64ToImage:strB64Image];
}

Comments

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.