【问题标题】:Creating UIImage from raw RGBA data从原始 RGBA 数据创建 UIImage
【发布时间】:2012-11-08 19:20:02
【问题描述】:

我一直在尝试将数组 RGBA 数据(int 字节)转换为 UIImage。我的代码如下所示:

/*height and width are integers denoting the dimensions of the image*/
unsigned char *rawData = malloc(width*height*4);

for (int i=0; i<width*height; ++i) 
{
    rawData[4*i] = <red_val>;
    rawData[4*i+1] = <green_val>;
    rawData[4*i+2] = <blue_val>;
    rawData[4*i+3] = 255;
}


/*I Have the correct values displayed
    - ensuring the rawData is well populated*/

NSLog(@"(%i,%i,%i,%f)",rawData[0],rawData[1],rawData[2],rawData[3]/255.0f);
NSLog(@"(%i,%i,%i,%f)",rawData[4],rawData[5],rawData[6],rawData[7]/255.0f);
NSLog(@"(%i,%i,%i,%f)",rawData[8],rawData[9],rawData[10],rawData[11]/255.0f);



CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, 
                                                          rawData, 
                                                          width*height*4, 
                                                          NULL);

int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4*width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef imageRef = CGImageCreate(width,
                                    height,
                                    8,
                                    32,
                                    4*width,colorSpaceRef,
                                    bitmapInfo,
                                    provider,NULL,NO,renderingIntent);
/*I get the current dimensions displayed here */
NSLog(@"width=%i, height: %i", CGImageGetWidth(imageRef),
      CGImageGetHeight(imageRef) );
UIImage *newImage = [UIImage imageWithCGImage:imageRef];
/*This is where the problem lies. 
  The width, height displayed are of completely different dimensions
  viz. the width is always zero and the height is a very huge number */

NSLog(@"resultImg width:%i, height:%i",
          newImage.size.width,newImage.size.height);


return newImage;

我收到的输出图像是宽度为 0、高度为 1080950784 的图像(假设我的初始高度和宽度分别为 240 和 240)。我一直在努力解决这个问题,并检查了许多相关论坛,例如(link text) 关于如何去做但收效甚微。

【问题讨论】:

  • 请注意,kCGBitmapByteOrderDefault 似乎不会将 Alpha 通道传输到图像。请改用kCGBitmapByteOrder32Big|kCGImageAlphaLast
  • 我认为你应该在返回 newImage 之前释放 colorSpaceRef (CGColorSpaceRelease(colorSpaceRef))。
  • @velkyel 他还必须发布数据提供者CGDataProviderRelease(provider);
  • 任何带有“create”的CG功能都应该被释放。所以,除了velkyel和medvedNick提到的colorSpaceRefprovider,你还需要调用CGImageRelease(imageRef);来释放CGImageRef
  • 您可能需要考虑NSMutableData* mutData = [[NSMutableData alloc] initWithCapacity:height * width * 4]; unsigned char *rawData =mutData.mutableBytes; 而不是unsigned char *rawData = malloc(width*height*4);

标签: iphone image-processing uiimage


【解决方案1】:

事实证明,这个问题是我们俩都忽略的一个非常愚蠢的错误。 UIImage 尺寸存储为浮点数,而不是整数。 :D

试试

NSLog(@"resultImg width:%f, height:%f",
          newImage.size.width,newImage.size.height);

相反。图片尺寸已正确传输。

【讨论】:

    【解决方案2】:

    现在问题解决了。我得到了我想要显示的图像,但仍然无法弄清楚为什么宽度和高度不同。每个人说的程序基本上没有错。唯一的问题是宽度和高度。

    【讨论】:

      【解决方案3】:

      我刚刚独立验证了这个问题,我认为它应该作为一个错误报告给苹果。 +(UIImage)imageWithCGImage: 没有正确地将源 CGImageRef 的宽度和高度转移到 UIImage

      【讨论】:

        猜你喜欢
        • 2015-07-02
        • 1970-01-01
        • 1970-01-01
        • 2011-05-13
        • 1970-01-01
        • 1970-01-01
        • 2012-10-20
        • 1970-01-01
        • 2017-02-26
        相关资源
        最近更新 更多