【发布时间】:2010-10-31 13:50:36
【问题描述】:
我得到一个包含 base64 编码的 XML 图像。我必须对其进行解码并需要显示图像。任何建议......
【问题讨论】:
标签: ios objective-c xcode uiimage base64
我得到一个包含 base64 编码的 XML 图像。我必须对其进行解码并需要显示图像。任何建议......
【问题讨论】:
标签: ios objective-c xcode uiimage base64
在 cocoawithlove.com 上有一个很好的 post,关于在 Mac OS 和 iPhone 上解码 base64。
这是创建 NSImage 的 Mac OS 方法:
unsigned char* data;
int width, height;
NSBitmapImageRep* rep;
rep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:&data
pixelsWide:width
pixelsHigh:height
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSCalibratedRGBColorSpace
bitmapFormat:NSAlphaNonpremultipliedBitmapFormat
bytesPerRow:32
bitsPerPixel:32];
NSImage* image = [[NSImage alloc] initWithSize:NSMakeSize(8, 8)];
[image addRepresentation:rep];
这可以在 iPhone 上创建一个 UIImage:
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGContextRef ctx = CGBitmapContextCreate(data, width, height, 8, 32, colorspace, kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorspace);
CGImageRef cgImage = CGBitmapContextCreateImage(ctx);
CGContextRelease(ctx);
UIImage* image = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
【讨论】: