【问题标题】:How do I read an image from a base64 encoded string in Objective C?如何从 Objective C 中的 base64 编码字符串中读取图像?
【发布时间】:2010-10-31 13:50:36
【问题描述】:

我得到一个包含 base64 编码的 XML 图像。我必须对其进行解码并需要显示图像。任何建议......

【问题讨论】:

    标签: ios objective-c xcode uiimage base64


    【解决方案1】:

    在 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);
    

    【讨论】:

    • 关于获取宽度和高度的任何建议?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    • 2018-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多