如何在obj-c中将图像转换为base64

时间:2016-03-28 08:27:39

标签: ios objective-c

我试过这个

+ (NSString *)base64StringFromImage:(UIImage *)image {    
    NSData *data = UIImagePNGRepresentation(image);
    NSString *base64Str = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

//    base64Str = (__bridge NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)base64Str, NULL, CFSTR(":/?#[]@!$&’()*+,;="), kCFStringEncodingUTF8);

    return base64Str;
}

但是这个返回值base64Str无法转换回在线base642image工具webstation上的图像

1 个答案:

答案 0 :(得分:2)

只需使用以下代码: -

NSData *imageData = UIImagePNGRepresentation(myImageView.image);
NSString * base64String = [imageData base64EncodedStringWithOptions:0];