- (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轮廓图像。对于大多数图像来说,这很好用。有几个会导致发生以下错误:<Error>:CGBitmapContextCreate:不支持的参数组合对较低分辨率图像
:CGBitmapContextCreate:不受支持的参数组合:8个整数位/组件; 32位/像素; 3分量色彩空间; kCGImageAlphaLast; 96字节/行。
我做了一些调试,它看起来像从导致错误的图像和不属于那些唯一的区别是参数,CGImageGetBitmapInfo(image.CGImage),创建上下文时。这会引发错误,并导致上下文为空。我试图将最后一个参数设置为kCGImageAlphaPremultipliedLast也无济于事。该图像是这次绘制的,但质量较差。有没有办法像其他人一样获得更高质量的图像?图片的路径是通过Twitter,所以不知道他们是否有不同的,你可以拉。
我也看到了关于这个错误的其他问题。没有人解决过这个问题。我看到了this post,但之后出现了错误的图像。并且将宽度和高度投射到NSInteger也不起作用。以下是两张配置文件图片及其质量的截图。第一个是导致错误。
没有人有任何想法是什么问题就在这里?
谢谢了。这一直在杀死我。
'imageWithAlpha'返回的原始图像是否具有1.0以外的'scale'值? – 2012-03-01 04:41:50
模糊的图像或错误图像的刻度值为2.0,而其他图像的刻度值为1.0。希望这有助于... – kschins 2012-03-01 04:59:53