这是我的代码。
UIImage *img = [UIImage imageNamed:@"white.jpeg"];
NSData *imageData = UIImagePNGRepresentation(img);
NSString *imageString = [imageData base64EncodedStringWithOptions:0];
NSLog(@"%@", imageString);
我总是得到包含空格的无效base64字符串。
iVBORw0KGgoAAAANSUhEUgAAARMAAAC3CAIAAAC MS2jAAAAHGlET1QAAAACAAAAAAAAAFwAAAAoAAAAXAAAAFsAAAMC oRdmgAAAs5JREFUeAHs1tFKI1EABFH//5NNVh02uA LQhwoCZ0zj5LbTlffInl58yCAwM8JvPz8iBMIIPDGHJcAgTMEmHOGmjMIMMcdQOAMAeacoeYMAsxxBxA4Q4A5Z6g5gwBz3IGHIPD zfMQL/fVSzDnKyr 9usEvhHn/ddf5N5/yJx7SflcSoA5KV7hz0Lgn0gPW9h3zsNO81wvdr1e/3w R3PmPNf82p4mcLlcDnlu/txCbvKcDqwP s6pCcu/i8Bhzn/yMOcucD705ARe/z43eY5fbQcN5jz5lVB/k4Bfa5u7alUTYE5NWP4mAeZs7qpVTYA5NWH5mwSYs7mrVjUB5tSE5W8SYM7mrlrVBJhTE5a/SYA5m7tqVRNgTk1Y/iYB5mzuqlVNgDk1YfmbBJizuatWNQHm1ITlbxJgzuauWtUEmFMTlr9JgDmbu2pVE2BOTVj JgHmbO6qVU2AOTVh ZsEmLO5q1Y1AebUhOVvEmDO5q5a1QSYUxOWv0mAOZu7alUTYE5NWP4mAeZs7qpVTYA5NWH5mwSYs7mrVjUB5tSE5W8SYM7mrlrVBJhTE5a/SYA5m7tqVRNgTk1Y/iYB5mzuqlVNgDk1YfmbBJizuatWNQHm1ITlbxJgzuauWtUEmFMTlr9JgDmbu2pVE2BOTVj JgHmbO6qVU2AOTVh ZsEmLO5q1Y1AebUhOVvEmDO5q5a1QSYUxOWv0mAOZu7alUTYE5NWP4mAeZs7qpVTYA5NWH5mwSYs7mrVjUB5tSE5W8SYM7mrlrVBJhTE5a/SYA5m7tqVRNgTk1Y/iYB5mzuqlVNgDk1YfmbBJizuatWNQHm1ITlbxJgzuauWtUEmFMTlr9JgDmbu2pVE2BOTVj JgHmbO6qVU2AOTVh ZsEmLO5q1Y1AebUhOVvEmDO5q5a1QSYUxOWv0mAOZu7alUTYE5NWP4mAeZs7qpVTeADAAD//66TqMcAAAIISURBVO3TsQ0AAAjDMP4/GnEDmc3exSKzjgCBv8D8JxYECKxyPAGBIqCcomZDQDl gEARUE5RsyGgHD9AoAgop6jZEFCOHyBQBJRT1GwIKMcPECgCyilqNgSU4wcIFAHlFDUbAsrxAwSKgHKKmg0B5fgBAkVAOUXNhoBy/ACBIqCcomZDQDl gEARUE5RsyGgHD9AoAgop6jZEFCOHyBQBJRT1GwIKMcPECgCyilqNgSU4wcIFAHlFDUbAsrxAwSKgHKKmg0B5fgBAkVAOUXNhoBy/ACBIqCcomZDQDl gEARUE5RsyGgHD9AoAgop6jZEFCOHyBQBJRT1GwIKMcPECgCyilqNgSU4wcIFAHlFDUbAsrxAwSKgHKKmg0B5fgBAkVAOUXNhoBy/ACBIqCcomZDQDl gEARUE5RsyGgHD9AoAgop6jZEFCOHyBQBJRT1GwIKMcPECgCyilqNgSU4wcIFAHlFDUbAsrxAwSKgHKKmg0B5fgBAkVAOUXNhoBy/ACBIqCcomZDQDl gEARUE5RsyGgHD9AoAgop6jZEFCOHyBQBJRT1GwIKMcPECgCyilqNgSU4wcIFAHlFDUbAsrxAwSKgHKKmg0B5fgBAkVAOUXNhoBy/ACBIqCcomZDQDl gEARUE5RsyGgHD9AoAgop6jZEFCOHyBQBJRT1GwIKMcPECgCB/GnRcSfso/fAAAAAElFTkSuQmCC
太困惑了。
当我尝试删除base64字符串中的空格并解码时。
我无法获得原始图像。
答案 0 :(得分:21)
我认为你应该替换你的选项参数
变化:
NSString *imageString = [imageData base64EncodedStringWithOptions:0];
要:
NSString *imageString = [imageData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
以防万一你喜欢它:
- (NSString *)imageToNSString:(UIImage *)image
{
NSData *data = UIImagePNGRepresentation(image);
return [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
}
- (UIImage *)stringToUIImage:(NSString *)string
{
NSData *data = [[NSData alloc]initWithBase64EncodedString:string options:NSDataBase64DecodingIgnoreUnknownCharacters];
return [UIImage imageWithData:data];
}
请记住,这是iOS 7 API。
答案 1 :(得分:4)
将 + 替换为%2B ios替换所有+到“”空格,使图像无效
后端也必须处理它
- (NSString *)base64String:(UIImage*)image {return [[UIImageJPEGRepresentation(image,1) base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed] stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];}
答案 2 :(得分:3)
是的,我发现问题是我不对网址进行编码。
在发布数据中,我的base64“+”字符被翻译为“”字符。
所以我得到了无效的base64字符串。
谢谢洛根。
答案 3 :(得分:0)
stringBase64 = stringBase64.replacingOccurrences(of: "+", with: "%2B")