将zlib压缩的base 64字符串转换为Uiimage的问题

时间:2016-09-23 09:35:08

标签: ios objective-c uiimage base64

在我当前的一个项目中,我必须解析base 64字符串,该字符串由flex(Adobe Flash)应用程序编码和存储。通过与flex dev团队讨论,我发现他们以位图格式存储图像字符串,因此我假设它是一个像素表示。

所以,第一个问题是,在上面提到的情况下,我可以使用base64解码类直接将原始数据转换为UIimage,还是必须使用CGBitmapContext?

但是,目前我已经实现了下面提到的转换代码。

`  //转换为Base64数据

    Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/Address
            at java.lang.Class.getDeclaredMethods0(Native Method)
            at java.lang.Class.privateGetDeclaredMethods(Class.java:2625)
            at java.lang.Class.getMethod0(Class.java:2866)
            at java.lang.Class.getMethod(Class.java:1676)
            at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
            at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
    Caused by: java.lang.ClassNotFoundException: javax.mail.Address
            at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
            at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
            at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:358)

下面是使用核心图形转换图像中的原始数据的方法

NSData *decodedData = [QSStrings decodeBase64WithString:_settingSerializedImage.currentValue];


NSError *error = nil;
NSData *unCompressedData = [decodedData bbs_dataByInflatingWithError:&error];



NSData *dataImage = [NSMutableData new];
NSUInteger bytePosition = 0;
uint8_t * bytes =  malloc(5);
[unCompressedData getBytes:&bytes range:NSMakeRange(unCompressedData.length-3, 3)];

bytes = OSSwapBigToHostInt16(bytes);
int width = (int)bytes;

[unCompressedData getBytes:&bytes range:NSMakeRange(unCompressedData.length-5, 5)];

bytes = OSSwapBigToHostInt16(bytes);dataImage = [unCompressedData subdataWithRange:NSMakeRange(0, (unCompressedData.length -5))];

NSUInteger length = [dataImage length];
unsigned char *bytesData = malloc( length * sizeof(unsigned char) ); [dataImage getBytes:bytesData length:length];UIImage *imgScreenShot = [ScreenshotPortlet convertBitmapRGBA8ToUIImage:bytesData withWidth:width withHeight:height];`

}

Here is the output I'm getting , a distorted image :

那么有人可以建议我更好的解决方案或告诉我是否正确的方向吗?提前谢谢:)

对于参考here,您可以找到原始基础64字符串

1 个答案:

答案 0 :(得分:1)

将图片转换为base64string:

- (NSString *)imageToNSString:(UIImage *)image
{
    NSData *data = UIImagePNGRepresentation(image);

    return [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
}

将base64string转换为图片:

- (UIImage *)stringToUIImage:(NSString *)string
{
    NSData *data = [[NSData alloc]initWithBase64EncodedString:string options:NSDataBase64DecodingIgnoreUnknownCharacters];

    return [UIImage imageWithData:data];
}