【问题标题】:iOS/Objective C: Converting RGB Data to UIImageiOS/Objective C:将 RGB 数据转换为 UIImage
【发布时间】:2013-07-16 10:40:49
【问题描述】:

我需要帮助将 24/32 位 RGB 原始图像转换为 uiimage。

我在这里尝试了 Paul Solt 和其他人的示例,但没有任何效果。有人可以举个例子或教程吗?

图像数据保存在 nsdata 中,我想要一个 jpg 或 png 图像。

谢谢 托尔斯滕

我正在使用 Paul Solt 的代码,它做了一些事情,但图像看起来像是一张图像中的图像信息的四倍。我不能在这里张贴图片: 编辑:我在 cmets 之间的方法开头添加了行,现在它可以工作了:-)

+ (UIImage *) convertBitmapRGBA8ToUIImage:(unsigned char *) buffer
                                withWidth:(int) width
                               withHeight:(int) height {

    // added code
    char* rgba = (char*)malloc(width*height*4);
    for(int i=0; i < width*height; ++i) {
        rgba[4*i] = buffer[3*i];
        rgba[4*i+1] = buffer[3*i+1];
        rgba[4*i+2] = buffer[3*i+2];
        rgba[4*i+3] = 255;
    }
    // 


    size_t bufferLength = width * height * 4;
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, rgba, bufferLength, NULL);
    size_t bitsPerComponent = 8;
    size_t bitsPerPixel = 32;
    size_t bytesPerRow = 4 * width;

    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    if(colorSpaceRef == NULL) {
        NSLog(@"Error allocating color space");
        CGDataProviderRelease(provider);
        return nil;
    }

    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

    CGImageRef iref = CGImageCreate(width,
                                    height,
                                    bitsPerComponent,
                                    bitsPerPixel,
                                    bytesPerRow,
                                    colorSpaceRef,
                                    bitmapInfo,
                                    provider,   // data provider
                                    NULL,       // decode
                                    YES,            // should interpolate
                                    renderingIntent);

    uint32_t* pixels = (uint32_t*)malloc(bufferLength);

    if(pixels == NULL) {
        NSLog(@"Error: Memory not allocated for bitmap");
        CGDataProviderRelease(provider);
        CGColorSpaceRelease(colorSpaceRef);
        CGImageRelease(iref);
        return nil;
    }

    CGContextRef context = CGBitmapContextCreate(pixels,
                                                 width,
                                                 height,
                                                 bitsPerComponent,
                                                 bytesPerRow,
                                                 colorSpaceRef,
                                                 bitmapInfo);

    if(context == NULL) {
        NSLog(@"Error context not created");
        free(pixels);
    }

    UIImage *image = nil;
    if(context) {

        CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, width, height), iref);

        CGImageRef imageRef = CGBitmapContextCreateImage(context);

        // Support both iPad 3.2 and iPhone 4 Retina displays with the correct scale
        if([UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)]) {
            float scale = [[UIScreen mainScreen] scale];
            image = [UIImage imageWithCGImage:imageRef scale:scale orientation:UIImageOrientationUp];
        } else {
            image = [UIImage imageWithCGImage:imageRef];
        }

        CGImageRelease(imageRef);   
        CGContextRelease(context);  
    }

    CGColorSpaceRelease(colorSpaceRef);
    CGImageRelease(iref);
    CGDataProviderRelease(provider);

    if(pixels) {
        free(pixels);
    }   
    return image;
}

【问题讨论】:

  • 发布一些代码......你已经尝试过什么
  • 原始数据到底是什么意思?您是指制造商特定的相机 RAW 还是仅指 RGB 位图数据?
  • 我#正在尝试连接到扫描仪。 sanner 在“颜色 RGB 原始数据”中发送“预览”数据。这就是我拥有的所有信息。
  • stackoverflow.com/questions/1579631/… 看看这个可能对你有帮助

标签: ios objective-c uiimage rgb


【解决方案1】:

解决方案:

我的位图图像数据只有3个字节,但是ios要4个字节,第四个是给alpha的。因此插入以下添加了第 4 个字节的代码解决了问题。

char* rgba = (char*)malloc(width*height*4);
for(int i=0; i < width*height; ++i) {
    rgba[4*i] = buffer[3*i];
    rgba[4*i+1] = buffer[3*i+1];
    rgba[4*i+2] = buffer[3*i+2];
    rgba[4*i+3] = 255;
}

【讨论】:

  • kCGImageAlphaNone 这会解决你的问题
【解决方案2】:

这里是 NSData 到 UIImage 的转换:

NSData *imageData = UIImagePNGRepresentation(image);

UIImage *image=[UIImage imageWithData:data];

我已经使用此代码创建了 PNG 图像,希望它也适用于您。

【讨论】:

  • 抱歉,没有。正如我所写,我没有 png 表示。数据是原始 rgb 24/32 位。但是谢谢 :-)
猜你喜欢
  • 2010-12-07
  • 2014-10-14
  • 2018-02-08
  • 1970-01-01
  • 2018-03-23
  • 2012-07-27
  • 1970-01-01
  • 2012-10-03
  • 1970-01-01
相关资源
最近更新 更多