【问题标题】:Image resized error: CGBitmapContextCreate: unsupported parameter图像大小调整错误:CGBitmapContextCreate:不支持的参数
【发布时间】:2010-02-10 07:52:54
【问题描述】:

我正在使用以下代码(来自博客文章)来调整图像大小

if (inImage.size.width <= inImage.size.height) {
    // Portrait
    ratio = inImage.size.height / inImage.size.width;
    resizedRect = CGRectMake(0, 0, width, width * ratio);
}
else {
    // Landscape
    ratio = inImage.size.width / inImage.size.height;
    resizedRect = CGRectMake(0, 0, height * ratio, height);
}

CGImageRef          imageRef = [inImage CGImage];
CGImageAlphaInfo    alphaInfo = CGImageGetAlphaInfo(imageRef);

if (alphaInfo == kCGImageAlphaNone)
    alphaInfo = kCGImageAlphaNoneSkipLast;

CGContextRef bitmap = CGBitmapContextCreate(
                                            NULL,
                                            resizedRect.size.width,     // width
                                            resizedRect.size.height,        // height
                                            CGImageGetBitsPerComponent(imageRef),   // really needs to always be 8
                                            4 * resizedRect.size.width, // rowbytes
                                            CGImageGetColorSpace(imageRef),
                                            alphaInfo
                                            );

但由于某种原因,根据我尝试调整大小的大小,我会生成以下错误

CGBitmapContextCreate:不支持 参数组合:8位整数 位/组件; 32 位/像素; 三分量色彩空间; kCGImageAlphaNoneSkipFirst; XXX 字节/行。

其中 XXX 因图像而异。

我创建的矩形与图像成比例,我从宽度/高度(取决于纵横比)和目标宽度/高度的倍数中取一个比率。

以下是一些示例(X 错误,/没有),调整大小将是 50xX 或 Xx50,具体取决于方面:

Source   50x50   69x69
430x320  /      X
240x320  /      /
272x320  /      /
480x419  /      X
426x320  X      X
480x256  X      X

【问题讨论】:

    标签: iphone objective-c


    【解决方案1】:

    你在哪里写了thumbRect,你的意思是resizedRectthumbRect 不会以其他方式出现。

    我怀疑问题在于resizedRect.size.width 不是整数。请注意,它是浮点数。

    CGBitmapContextCreate 的 width 和 bytesPerRow 参数被声明为整数。当您传递浮点值时,例如此处,它会被截断。

    假设你的 resizedRect.size.width 是 1.25。然后你最终会传递 1 作为宽度,而 floor(1.25 * 4) == 5 作为每行的字节数。那是不一致的。对于每行字节的宽度,您总是希望传递四次。

    顺便说一句,您也可以将 bytesPerRow 保留为 0。然后系统选择最好的 bytesPerRow (通常大于宽度的 4 倍 - 它为对齐而填充)。

    【讨论】:

    • 是的(复制和粘贴错误将其格式化为问题)是的,我同意 resizedRect.size.width 可能是一个浮点数。你能再解释一下吗,我只需要四舍五入吗?
    • 请更新您的回复,我只是将值的 3 种用法转换为 int 并解决了问题。 (int)resizedRect.size.width 等
    • 我已经将 bytesPerRow 设置为 0,但是当 float 值的大小时仍然出现错误。 CGContextConcatCTM:无效上下文 0x0 CGContextSetInterpolationQuality:无效上下文 0x0 CGContextDrawImage:无效上下文 0x0 CGBitmapContextCreateImage:无效上下文 0x0
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多