图像已调整大小错误:CGBitmapContextCreate:不支持的参数 [英] Image resized error: CGBitmapContextCreate: unsupported parameter
问题描述
我使用以下代码(来自博客文章)来调整图片大小
I am using the following code (from a blog post) to resize an image
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
);
但由于某种原因,我会尝试调整大小,因此会产生以下错误
but for some reason depending on the size I am try to resize to I get the following error generated
CGBitmapContextCreate:unsupported
参数组合:8整数
bits / component; 32位/像素;
3分量色彩空间;
kCGImageAlphaNoneSkipFirst; XXX
bytes / row。
CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component colorspace; kCGImageAlphaNoneSkipFirst; XXX bytes/row.
其中XXX因图像而异。
where XXX differs depending on which image.
我正在创建的矩形是图像的比例,我从宽度/高度(取决于方面)和多个目标宽度/高度的比率。
The rect I am creating is propotional to the image, I take a ratio from the width/height (depending on aspect) and multiple that be target width/height.
以下是一些示例(X错误,/ doesnt),调整大小将为50xX或Xx50,具体取决于方面:
Here are some examples (X errors, / doesnt), the resize size will be 50xX or Xx50 depending on aspect:
Source 50x50 69x69
430x320 / X
240x320 / /
272x320 / /
480x419 / X
426x320 X X
480x256 X X
推荐答案
你写的地方 thumbRect
,你的意思是 resizedRect
? thumbRect
否则不会发生。
Where you wrote thumbRect
, did you mean resizedRect
? thumbRect
does not otherwise occur.
我怀疑问题是 resizedRect.size。 width
是非整数。请注意,它是浮点数。
I suspect the problem is that resizedRect.size.width
is non integral. Note that it's floating point.
CGBitmapContextCreate
的width和bytesPerRow参数被声明为整数。传递浮点值(例如此处)时,它会被截断。
The width and bytesPerRow parameters of CGBitmapContextCreate
are declared as integers. When you pass a floating point value, such as here, it gets truncated.
假设你的resizedRect.size.width是1.25。然后你最终会传递1作为宽度,而floor(1.25 * 4)== 5作为每行的字节数。这是不一致的。对于每行字节的宽度,你总是希望传递四次。
Suppose your resizedRect.size.width is 1.25. Then you will end up passing 1 for the width, and floor(1.25 * 4) == 5 as the bytes per row. That's inconsistent. You always want to pass four times whatever you passed for the width for the bytes per row.
顺便说一句,你也可以将bytesPerRow保留为0。然后系统选择最好的bytesPerRow(通常大于宽度的4倍 - 它填充对齐)。
You can also just leave bytesPerRow as 0, by the way. Then the system picks the best bytesPerRow (which is often larger than 4 times the width - it pads out for alignment).
这篇关于图像已调整大小错误:CGBitmapContextCreate:不支持的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!