简体   繁体   English

CGBitmapContextCreate对于宽度和高度的某些像素返回NULL

[英]CGBitmapContextCreate returns NULL for some pixels of width and height

I have the following code to create a new bitmap graphics context. 我有以下代码来创建新的位图图形上下文。

NSLog(@"newRect Width %f",newRect.size.width);
NSLog(@"newRect Height %f",newRect.size.height);
NSLog(@"imageRef %@",imageRef);

CGContextRef bitmap = CGBitmapContextCreate(NULL,
                                            newRect.size.width,
                                            newRect.size.height,
                                            CGImageGetBitsPerComponent(imageRef),
                                            0,
                                            CGImageGetColorSpace(imageRef),
                                            CGImageGetBitmapInfo(imageRef));

The newRect.size.width is the result of dividing the original width of UIImage by 2. The newRect.size.height is the result of dividing the original height of UIImage by 2. newRect.size.width是将UIImage的原始宽度除以2的结果。newRect.size.height是将UIImage的原始高度除以2的结果。

But for some UIImages with sizes {3000, 2002},{3024, 4032},{4032, 3024} are okay and returning bitmap context. 但是对于某些大小为{3000,2002},{3024,4032},{4032,3024}的UIImage来说,它可以返回位图上下文。

But for some UIImages with sizes {1242, 2208}. 但是对于某些大小为{1242,2208}的UIImage。 Normally these are screenshots from my devices and returning NULL value. 通常,这些是我设备的屏幕截图,返回NULL值。

这是失败的UIImage的日志 . 这是成功UIImage的日志 .

Is anyone can help on my issue. 有谁可以帮忙解决我的问题。 Thanks. 谢谢。

I was not able to reproduce the problem. 我无法重现该问题。 I would suggest to log all incoming parameters and append log to your question. 我建议记录所有传入参数并将日志附加到您的问题。 Also look into console log, CGBitmapContextCreate may write something there that may give the insight about the problem. 还要查看控制台日志,CGBitmapContextCreate可能会在其中写一些东西,以使您对问题有更深入的了解。

UPDATED : 更新时间

The problem you are facing has nothing to do with image size, but with combination of bitmap info/colorspace and bits per component. 您面临的问题与图像大小无关,而与位图信息/色彩空间和每个组件的位组合在一起。 Here what I got in system log trying to create bitmap context with info you provided: 这是我在系统日志中尝试使用您提供的信息创建位图上下文的内容:

CGBitmapContextCreate: unsupported parameter combination:
    16 bits/component; integer;
    64 bits/pixel;
    RGB color space model; kCGImageAlphaLast;
    kCGImageByteOrder16Little byte order;
    4992 bytes/row.
Valid parameters for RGB color space model are:
    16  bits per pixel,      5  bits per component,      kCGImageAlphaNoneSkipFirst
    32  bits per pixel,      8  bits per component,      kCGImageAlphaNoneSkipFirst
    32  bits per pixel,      8  bits per component,      kCGImageAlphaNoneSkipLast
    32  bits per pixel,      8  bits per component,      kCGImageAlphaPremultipliedFirst
    32  bits per pixel,      8  bits per component,      kCGImageAlphaPremultipliedLast
    64  bits per pixel,      16 bits per component,      kCGImageAlphaPremultipliedLast
    64  bits per pixel,      16 bits per component,      kCGImageAlphaNoneSkipLast
    64  bits per pixel,      16 bits per component,      kCGImageAlphaPremultipliedLast|kCGBitmapFloatComponents
    64  bits per pixel,      16 bits per component,      kCGImageAlphaNoneSkipLast|kCGBitmapFloatComponents
    128 bits per pixel,      32 bits per component,      kCGImageAlphaPremultipliedLast|kCGBitmapFloatComponents
    128 bits per pixel,      32 bits per component,      kCGImageAlphaNoneSkipLast|kCGBitmapFloatComponents
See Quartz 2D Programming Guide (available online) for more information.

You can try to use something standard always or in case of failure, eg 您可以尝试始终使用标准的东西,或者在失败的情况下使用,例如

CGContextRef bitmap = CGBitmapContextCreate(NULL,
                                            newRect.size.width,
                                            newRect.size.height,
                                            8,
                                            0,
                                            CGColorSpaceCreateDeviceRGB(),
                                            kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host));

Or let system choose appropriate context for you: 或者让系统为您选择适当的上下文:

   UIGraphicsBeginImageContextWithOptions(newRect.size, false, 1.0);
   CGContextRef bitmap = UIGraphicsGetCurrentContext();

   // Do your drawing here

   UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM