【发布时间】:2012-02-29 23:12:06
【问题描述】:
- (UIImage *)roundedCornerImage:(NSInteger)cornerSize borderSize:(NSInteger)borderSize {
// If the image does not have an alpha layer, add one
UIImage *image = [self imageWithAlpha];
// Build a context that's the same dimensions as the new size
CGBitmapInfo info = CGImageGetBitmapInfo(image.CGImage);
CGContextRef context = CGBitmapContextCreate(NULL,
image.size.width,
image.size.height,
CGImageGetBitsPerComponent(image.CGImage),
0,
CGImageGetColorSpace(image.CGImage),
CGImageGetBitmapInfo(image.CGImage));
// Create a clipping path with rounded corners
CGContextBeginPath(context);
[self addRoundedRectToPath:CGRectMake(borderSize, borderSize, image.size.width - borderSize * 2, image.size.height - borderSize * 2)
context:context
ovalWidth:cornerSize
ovalHeight:cornerSize];
CGContextClosePath(context);
CGContextClip(context);
// Draw the image to the context; the clipping path will make anything outside the rounded rect transparent
CGContextDrawImage(context, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
// Create a CGImage from the context
CGImageRef clippedImage = CGBitmapContextCreateImage(context);
CGContextRelease(context);
// Create a UIImage from the CGImage
UIImage *roundedImage = [UIImage imageWithCGImage:clippedImage];
CGImageRelease(clippedImage);
return roundedImage;
}
我有上述方法,并正在为 Twitter 个人资料图片添加圆角。对于大多数图像,这都很棒。有几个会导致出现以下错误:
:CGBitmapContextCreate:不支持的参数组合:8个整数位/分量; 32 位/像素;三分量色彩空间; kCGImageAlphaLast; 96 字节/行。
我已经进行了一些调试,看起来与导致错误的图像的唯一区别是创建上下文时的参数 CGImageGetBitmapInfo(image.CGImage)。这会引发错误并导致上下文为空。我尝试将最后一个参数设置为 kCGImageAlphaPremultipliedLast 也无济于事。这次绘制了图像,但质量要低得多。有没有办法获得与其他人相提并论的更高质量的图像?图片的路径是通过 Twitter,所以不确定它们是否有不同的可以拉取。
我也看到了有关此错误的其他问题。都没有解决这个问题。我看到了this post,但之后错误的图像完全模糊了。并且将宽度和高度转换为 NSInteger 也不起作用。下面是两张个人资料图像及其质量的屏幕截图。第一个导致错误。
有人知道这是什么问题吗?
非常感谢。这让我很生气。
【问题讨论】:
-
imageWithAlpha返回的原始图像是否有一个不是 1.0 的scale值? -
模糊图像或错误图像的比例值为 2.0,而其他图像的比例值为 1.0。希望这会有所帮助...