Ant*_*ain 10 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 = 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
);
Run Code Online (Sandbox Code Playgroud)
但由于某种原因,我试图调整大小取决于我得到以下错误生成
CGBitmapContextCreate:不支持的参数组合:8个整数位/组件; 32位/像素; 3分量色彩空间; kCGImageAlphaNoneSkipFirst; XXX个字节/行.
XXX根据哪个图像而不同.
我正在创建的矩形是图像的比例,我从宽度/高度(取决于方面)和多个目标宽度/高度的比例.
以下是一些示例(X错误,/ doesnt),调整大小将为50xX或Xx50,具体取决于方面:
Source 50x50 69x69
430x320 / X
240x320 / /
272x320 / /
480x419 / X
426x320 X X
480x256 X X
Run Code Online (Sandbox Code Playgroud)
Ken*_*Ken 12
你写的地方thumbRect,你的意思是resizedRect?thumbRect否则就不会发生.
我怀疑问题resizedRect.size.width是不完整的.请注意,它是浮点数.
width和bytesPerRow参数CGBitmapContextCreate声明为整数.传递浮点值(例如此处)时,它会被截断.
假设你的resizedRect.size.width是1.25.然后你将最终传递1作为宽度,并将floor(1.25*4)== 5作为每行的字节数.这是不一致的.对于每行的字节宽度,您总是希望传递四次.
顺便说一下,你也可以将bytesPerRow保留为0.然后系统选择最好的bytesPerRow(通常大于宽度的4倍 - 它填充对齐).