Iphone 图像大小调整错误:CGBitmapContextCreate:不支持的参数
我正在使用以下代码(来自一篇博客文章)来调整图像的大小Iphone 图像大小调整错误:CGBitmapContextCreate:不支持的参数,iphone,objective-c,Iphone,Objective C,我正在使用以下代码(来自一篇博客文章)来调整图像的大小 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 = inIm
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
);
您在哪里编写的
thumbRect
,是指resizedRect
<代码>thumbRect不会以其他方式出现
我怀疑问题在于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 etcI已将bytesPerRow设置为0,但当大小为浮点值时仍会出现错误。CGContextConcatCTM:无效上下文0x0 CGContextSetInterpolationQuality:无效上下文0x0 CGContextDrawImage:无效上下文0x0 CGBitmapContextCreateImage:无效上下文0x0
Source 50x50 69x69
430x320 / X
240x320 / /
272x320 / /
480x419 / X
426x320 X X
480x256 X X