【问题标题】:CGBitmapContextCreate returns NULL for some pixels of width and heightCGBitmapContextCreate 为某些宽度和高度的像素返回 NULL
【发布时间】:2018-03-26 07:47:48
【问题描述】:

我有以下代码来创建一个新的位图图形上下文。

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

newRect.size.width 是 UIImage 的原始宽度除以 2 的结果。 newRect.size.height 是 UIImage 的原始高度除以 2 的结果。

但对于一些尺寸为 {3000, 2002},{3024, 4032},{4032, 3024} 的 UIImage 可以并返回位图上下文。

但对于一些尺寸为 {1242, 2208} 的 UIImage。通常这些是我设备的屏幕截图并返回 NULL 值。

.

有没有人可以帮助解决我的问题。谢谢。

【问题讨论】:

  • 尝试用 CGImageGetWidth(imageRef) 和 CGI​​mageGetHeight(imageRef) 改变你的宽度、高度
  • @QuocNguyen 你能详细解释一下吗?我不明白你想让我做什么。谢谢你的回答。
  • 试试这个 CGContextRef 位图 = CGBitmapContextCreate(NULL, CGImageGetWidth(imageRef) , CGImageGetHeight(imageRef), CGImageGetBitsPerComponent(imageRef), 0, CGImageGetColorSpace(imageRef), CGImageGetBitmapInfo(imageRef));
  • @QuocNguyen 仍然遇到同样的问题“[Unknown process name] CGBitmapContextCreate: unsupported parameter combination: set CGBITMAP_CONTEXT_LOG_ERRORS environment variable to see the details”

标签: ios objective-c core-graphics cgbitmapcontextcreate cgbitmapcontext


【解决方案1】:

我无法重现该问题。 我建议记录所有传入参数并将日志附加到您的问题中。 还要查看控制台日志,CGBitmapContextCreate 可能会在那里写一些可以提供有关问题的见解的内容。

更新

您面临的问题与图像大小无关,而是与位图信息/色彩空间和每个组件的位数的组合有关。这是我在系统日志中尝试使用您提供的信息创建位图上下文的内容:

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.

你可以尝试使用标准的东西,或者在失败的情况下,例如

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

或者让系统为你选择合适的上下文:

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

   // Do your drawing here

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

【讨论】:

  • 感谢您的回答,请查看已编辑的问题以获取更多信息。
  • @NyeinEiEiTun 我已经更新了答案,看看是否有帮助。
  • 你太棒了。你的两个答案都对我有用。谢谢。如果您不介意,您能否分享一种打印出 CGBitmapContextCreate 日志的方法。再次感谢您的回答。
  • @NyeinEiEiTun 要启用日志,您可以尝试这种方式stackoverflow.com/questions/39496435/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-27
  • 1970-01-01
  • 2011-12-05
  • 2014-12-06
  • 1970-01-01
  • 2015-01-02
相关资源
最近更新 更多