Kyle Griffith Kyle Griffith - 18 days ago 8
Objective-C Question

base 64 encoding objective-c

I am trying to convert copied images on the iOS pasteboard from a string to an image. 90% of the time this works under the scheme i have with URL's. However 1/10 times the image is not a url but is of what seems to be a base 64 encoding. The string looks like this.

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxMSEBIPExMVFRAVEBAQEBYQFRUVDxAQFREWFhUVFRUYHSggGBolHRUVITEhJSkrLi4uFx8zODMtNygtLisBCgoKDg0OGhAQGy0lICUtLS0wLS8tLS0tLS0tLS8tLS0tLS0tLS0tLS0tLi0tLS0tLSstLS4tLS0tLSstLS0tLf/AABEIALcBEwMBIgACEQEDEQH/xAAbAAABBQEBAAAAAAAAAAAAAAADAAECBAUGB//EAEIQAAEDAwIDBQYDBgQEBwAAAAEAAhEDBCExUQUSQRMiYXGBBhQykaGxQsHwBxVSYnPRIzNygpKz4fEXJFNkorLS/8QAGgEAAwEBAQEAAAAAAAAAAAAAAQIDAAQFBv/EAC8RAAICAQIEAgoDAQEAAAAAAAABAhEDEiEEEzFRQbEFIjJhcZHB0eHwFIGh8UL/2gAMAwEAAhEDEQA/AO2YjNWPSvlYbfBfOaGe22majSpgrMF6FIXo3W0sTSafMlzLPF2N1IXQ3QoHLL8pwVQFyN1MV0KNy2XQU4KqdunFdYXQy4CnBVUVlIVkBXBloOUw9U+3TisgLoZeD1MPVEVlMVlhXAvCopCoqIrKQrLCOBd7RN2iqdql2q1g0Fo1FA1FX7VN2iwdAcvUC9BL0xesMohS5RLkPmTF6I2knKaVDnTc6waCSlKFzJ+ZENE5SJQ+ZKVjUOUNykSoErUMgZSTlJChzl6Vg5WGWRWy0BTwq81m0oxvdHJjauW1AT8oQ5jDRhG2cnFu9bvZhSFILcxmtGIy3cjNpOWuKITiihrBqRkcjku8tnsExt0HP3B5iMgPcpio7ZaJt0vd0uv3B1ozu1dsnbVdstEWymLUIORubEzxVKftitAWYU22KXUDmwKNOoSrlK2qO0atawsAMxlab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YVSvZhHtbHNOSDIzDTjON8/JVaNM5kg56iBGIA+SIWxjXfonGukJkK0SMkkg+DYjEYESJ2KIxxIEuzMGMwYznCE74onEBM+qBIwAc9UwrAXLIIkjAB8jGh8UAMEidNW4yJzpOdVbZTBP4YiTHVRdTAyP9o6BU1MTSiDHAHIE41AaI6ERqoEDEHIxHT0RXsxOpQ+zODtr6rIxB7z/1P3TsqxJ18B1xrk/qVGqNY8spNGMjER67phSTnnq3p4SPTpr9VB0SHRH0MqT3QBgdNNVF1XyGNtfNMrQrpgvefA/VJOSTnRJPqiJpZWbUPTRSaJ6qsyqjU3KggcslM6niEmuUi5AZEOzCcUgkHJuZAIixGt7ca9eiC10qzSJBSz2Q0N2SFHMozaQj7IRcpseSueSZ0JoTaIB+6J2KEapBTseRlI0x00TeTGY0WW6mSVarV5KEHQrQjROUkwYpiM9EANVprxMnZBqJ0nYsmq2K7GGVYbRG+ZTsaEZjQjIEF3AimQiMCMGBHp0xEqbZSrKhONVetHQJKE5jSnpjELTVxFW0izUIgmdUmkxk4Q+QwEziQIKikUbCEAmVCpS8QfNQFQAKDHyU4hJzcQMeScvIAkIbjAU2PkaogGbWORCTavh1RGviEqhRABc/qAk+v03+QRJGqjduDtBAj6ogBjIlQGnRRnEJgcIgGNUJkPkKSNi0UaZRw9OkrkSbUZqSSDGQxdCTjhOkggsjTRqZJTpIS6Gh1LDW4SmEklzHUJplQrVOiZJGK3BJ7AAniUklayaREsTCmkkinsK1uPypuqSSUeg0wFHtCkktQWx+dPTqwUkln0FvoaDKuNFGq+eiSS5ki7BBkqYpDVJJGwA30ZQjSKSSMWLJEhTKlyHdJJNYtCcxMWpJJrBRXe3qq5fASSRQrBGoU6SS1go//9k=


Now I use the following code to recognize its base64 based off a post I found.

- (BOOL)isBase64Data :(NSString *)input{
input=[[input componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] componentsJoinedByString:@""];
if ([input length] % 4 == 0) {
static NSCharacterSet *invertedBase64CharacterSet = nil;
if (invertedBase64CharacterSet == nil) {
invertedBase64CharacterSet = [[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="]invertedSet];
}
return [input rangeOfCharacterFromSet:invertedBase64CharacterSet options:NSLiteralSearch].location == NSNotFound;
}
return NO;


}

However it always returns false. Does this string I show need some trimming in some way? I find the length of the string to be 8391 therefore modulo 4==0 is never invoked.

Answer

/9j/4AAQSkZJ is a typical beginning of JPEG data, converted to hex as FFD8FFE00010+JFIF. If you want to convert it to an image, you need to point the converter to the point AFTER the data:image/jpeg;base64, text.

I think you can do something like this:

NSData *base64 = [yourData subdataWithRange:NSMakeRange(24, yourData.length-24)];
NSData *imgData = [[NSData alloc] initWithBase64EncodedData:base64 options:0];
UIImage *img = [UIImage imageWithData:imgData];

I might be wrong with the exact implementation but this is the general idea...