抗锯齿UIimage看起来模糊或锯齿状

时间:2013-01-18 18:03:26

标签: iphone ios image uiimageview uiimage

我想在一个代表手表编号的圆圈中绘制12个图像,我已经阅读了有关具有透明边框的图像的stackoverflow上的所有主题,但它在我的情况下不起作用

-(UIImage *)addImageNumber_:(UIImage *)img {

    int w = img.size.width;
    int h = img.size.height;

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
    CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
    CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1);
    CGContextSetShouldAntialias(context,YES);
    CGContextSetAllowsAntialiasing( context ,YES );
    CGAffineTransform transform;

    for (int x=0; x<=11; x++) {

        UIImage *timg1 = [UIImage imageNamed:@"2.png"];

        CGRect imageRect = CGRectMake(0, 0, timg1.size.width+2, timg1.size.height+2);
        UIGraphicsBeginImageContext(imageRect.size);
        [timg1 drawInRect:CGRectMake(1,1,timg1.size.width,timg1.size.height)];
        timg1 = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        transform = CGAffineTransformIdentity;

        CGContextDrawImage(context, CGRectMake((w-26)/2, 0, 26, 30), timg1.CGImage);

        transform = CGAffineTransformConcat(transform, CGAffineTransformMakeTranslation(-w/2, -w/2));
        transform = CGAffineTransformConcat(transform, CGAffineTransformMakeRotation(radians(30)));
        transform = CGAffineTransformConcat(transform, CGAffineTransformMakeTranslation(w/2, w/2));

        CGContextConcatCTM(context, transform);
    }

    CGImageRef imageMasked = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    return [UIImage imageWithCGImage:imageMasked];
}
UIViewEdgeAntialiasing = YES;
UIImage *img = [UIImage imageNamed:@"test.png"];
UIImage *img2 = [self addImageNumber_:img ];
R1.image = img2;
[self.view addSubview:R1];

测试img是手表的背景,2.png是带透明边框的透明png

enter image description here

12'o时钟和6'o时钟的数字看起来不错,因为它们没有旋转,其余的都是锯齿状的

draw with text

2 个答案:

答案 0 :(得分:6)

永远不要说UIGraphicsBeginImageContext(imageRect.size)。说UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, 0)。这样,在双分辨率屏幕上,您将获得双分辨率图形上下文。

你甚至可以尝试将分辨率值设为4来进一步提高分辨率。

当然,您从预先绘制的“2”图像开始这一事实可能会限制您的分辨率;你不能用母猪的耳朵做一个丝绸钱包。从头开始绘制“2”作为字符串可能会好得多。

答案 1 :(得分:0)

CGContextSetInterpolationQuality(context, kCGInterpolationHigh);