2012-02-29 79 views
1
- (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也不起作用。以下是两张配置文件图片及其质量的截图。第一个是导致错误。

Here is a screenshot of the two images and their quality

没有人有任何想法是什么问题就在这里?

谢谢了。这一直在杀死我。

+0

'imageWithAlpha'返回的原始图像是否具有1.0以外的'scale'值? – 2012-03-01 04:41:50

+0

模糊的图像或错误图像的刻度值为2.0,而其他图像的刻度值为1.0。希望这有助于... – kschins 2012-03-01 04:59:53

回答

4

iOS不支持kCGImageAlphaLast。您需要使用kCGImageAlphaPremultipliedLast

您还需要处理初始图像的比例。您当前的代码不会,因此如果其缩放比例为2.0,则会缩小图像的样本。

您可以使用UIKit函数和类更简单地编写整个函数。 UIKit将为您量身定制;当您要求创建图形上下文时,您只需传递原始图像的比例。

- (UIImage *)roundedCornerImage:(NSInteger)cornerSize borderSize:(NSInteger)borderSize { 
    // If the image does not have an alpha layer, add one 
    UIImage *image = [self imageWithAlpha]; 

    UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale); { 
     CGRect imageRect = (CGRect){ CGPointZero, image.size }; 
     CGRect borderRect = CGRectInset(imageRect, borderSize, borderSize); 
     UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:borderRect 
      byRoundingCorners:UIRectCornerAllCorners 
      cornerRadii:CGSizeMake(cornerSize, cornerSize)]; 
     [path addClip]; 
     [image drawAtPoint:CGPointZero]; 
    } 
    UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return roundedImage; 
} 

如果您imageWithAlpha方法本身创建从另一个UIImage一个UIImage,它需要也传播规模。

+0

图像仍然模糊。我从Twitter获得的图像是其他人的一半。有没有办法获得更高分辨率或更大的图像?或者我该如何处理它的质量? – kschins 2012-03-01 01:34:48

+0

我修改了我的答案。 – 2012-03-01 05:11:03

+0

地狱是 - 非常感谢。 – kschins 2012-03-01 05:27:16