我正在尝试将Base64图片转换为UIImage
中的Objective C
。
我正在这样做: -
(UIImage *)decodeBase64ToImage:(NSString *)strEncodeData {
NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters];
return [UIImage imageWithData:data];
}
但是这个函数总是返回nil
。
我的Base64字符串是这样的: -
我使用https://codebeautify.org/base64-to-image-converter#验证了图片的有效性,并且显示效果很好。
修改
P.S我还在字符串中包含data:image/png;base64
。
我有办法解决这个问题吗?任何帮助表示赞赏。
答案 0 :(得分:4)
如下所示:
<强>更新强>
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *base64String = @"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAfQAAACWCAYAAAAonXpvAAAQD0lEQVR4Xu3cu4slxRoA8L7BsswgO4GiYGAggpEIIig+MBRTExEfiCgGiiCoIOIDBQ0FUcFYDfQfEGM1FYQVNBGTVQQN1mBlFdlL9bXPPTN75kz16Vc9fgsDOzvV3VW/r7q/qu/07H/Onj176eLFi82pU6ea06dPt1/h7/4QIECAAAECaQr8/fffTcjd4Sv8PeTu/5w7d+7Stdde2/zxxx/N+fPn26/w5+DgoP06c+ZMmqPRKwIECBAgUJHAtjz9888//z+hr5v8+eefq+R+4cKFQ8nd7r2i2WOoBAgQILCYQNh5ryfx/f39VT7e29s71K9jE/p6qz4nXGzULkyAAAECBAoQ2HVDHZXQj/oozRcwYwyBAAECBJIRGCOv7pTQleaTmQM6QoAAAQIZCkxR+R6c0JXmM5xJukyAAAECswvsWkqP7eioCV1pPpZdOwIECBCoQWCMUnqs06QJXWk+NgzaESBAgEAJAlOU0mNdZkvoSvOxIdGOAAECBHISmLqUHmuxSEJXmo8Nj3YECBAgkKLAnKX02PEnkdCV5mPDpR0BAgQILCGwZCk9drzJJXSl+djQaUeAAAECUwqkUkqPHWPSCV1pPjaM2hEgQIDAGAIpltJjx5VVQleajw2rdgQIECAQI5BDKT1mHKFNtg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JYUsOE6Xr/qhK6ks+Rt6doECBAYLuA5/n/DqhK6ld3wm8cZCBAgkKpA7ZXW4hN67QFO9cbTLwIECEwpUOMGrsiErgQz5W3i3AQIEMhPoIa8UERCr3Ellt/tpMcECBBIQ6DUym22Cb3UgKQx3fWCAAECdQiUtCHMKqHXUDKp4xYySgIECKQpkHOeSTqhl7RySnPq6hUBAgQIHCeQWyU4uYSeG6BbgQABAgTyFvj666+bu+66azWIm2++ufn000+bG2+8sf23N998s3n11Vfbv3/22WfNDTfc0Ozv7zcHBwfN559/3jz++OPtzz7++OPmoYceisYI+e65555rHnnkkebOO+9sj+v+7cMPP1yd54033mheeeWV9vsffviheeCBB5pvv/22eeqpp5p33nmn2dvba3+WRELPucQRHTkNCRAgQCBJgU8++aT58ccfV0lzvZMh2YeEHtp8//33q7+fOnWq+eabb5qXX365eemll5orrrii/dkHH3ywWghsG+x64v7qq69WCf33339vnnnmmeb111+/7DzdMXfffXdz//33t4uB8PduEbFIQldKT3JO6xQBAgSqFAiJ+Prrr9+4uw4/C3/CDvnojjok+S+//LJ56623mr/++qt5++23m6uvvrp58MEH2937mTNnmpD4j/7pdtm3335789NPP7Xn7nbo4Wchmb/33nvNlVdeeejQ8LNnn322effdd9tkHxYbH3300WqXPltCV0qv8j4xaAIECCQtsKns3XV4fUccdsFHv19P9uGY8P0///zTPPnkk80LL7zQJvmQrK+55pq2NB/K+GEREM4T/oRSeTjvekI/mqSPqxaEZL9ePQjfT5rQldKTnsc6R4AAgeoFQok7JNUvvvhiZdF9Fr4p2a/v5o/u7NdL992xt956a/uZe1eaD5/Pd7v37trrCT2c4+GHH1715d57720XAV0CX9+RH93Nj5rQldKrvzcAECBAICuBrvz9/vvvt2Xv9e9vueWWy15ai03oAWH9BbawSAife58/f779unDhQnPp0qV2Jx9euLvnnntat3D+c+fOrcro69+Hz+wnTehK6VnNXZ0lQIAAgRMEulL6888/f+jFs5iSezh190b6pgTdXTpsgMPn50888UTz2GOPNXfccUe7cw9f3Vvr3aKg+9z8t99+W72UN1rJXSnd/UCAAAECpQoctwvf9FLc+tvxR0vw4TPup59+uv0M/dFHH73spbv1kvtNN9202r0H1y65//LLL6uX5EJCX39hbqeX4pTSS522xkWAAIG6BY6+WNYl4e730I/7tbWwQ15/6zworr+Bvp6sr7rqqkM/68SPfobeLRhuu+225r777mt+/fXXdkd+3XXXtZ/BhzfmX3zxxfZX1Xr92ppSet2T3OgJECBQi8DR/1hm/ffCu7J59x/LHP3Z+kts3ct0XaIOn8l35ffQLnz+3b3gFs676aW4o/+xTHhj/rXXXmsuXrzY7uBDkg8fBXz33Xfb/2MZpfRapq9xEiBAgECOAtvydPuW+9mzZy+F7B+286dPn26/Nv0yfI6D12cCBAgQIFCiQPg4POTu8BX+HnL3fwGAyHYzePzAEQAAAABJRU5ErkJggg==";
NSURL *url = [NSURL URLWithString:base64String];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageData];
self.imgView.image = image;
}
答案 1 :(得分:0)
如果你按照基本的Apple开发人员教程,你可以得到答案。但是在确切的答案之下。
FileWriter
另请在此处找到解决方案Converting UIImage to NSData and NSData to NSString for posting images to a server