2013-01-18 165 views
2

我想提请在代表手表的数字圆圈12张图像,我已经阅读计算器关于与透明边框图像的所有议题,但它不是我的情况下工作抗锯齿的UIImage看起来模糊或锯齿状

-(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

+0

抗锯齿不应与UIImageViews的一个问题。我认为这可能是另一个问题。你是否急剧缩小了你使用的图像? –

+0

没有图像被缩小,它们都具有原始尺寸 –

+0

您获得的质量是技术上可行的。当您创建原始图像时,会使用最佳的抗锯齿功能。当你旋转它时,它不再是最佳的。每次转换只会导致质量下降。最好使用文本绘图功能来创建时钟。 – Codo

回答

0

尝试

CGContextSetInterpolationQuality(context, kCGInterpolationHigh); 
+0

试过,但没有解决问题 –

6

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

甚至可以尝试4的分辨率值来进一步提高分辨率。

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

+0

我附上了一个带有字符串而不是图像的图像,但我仍然有抗锯齿功能不能正常工作 –

+0

但是您没有按照更高分辨率绘制图片,是吗? – matt

+0

不,我没有那样做 –

0

斯威夫特3:因为他们是默认抗锯齿,而不是UIView的的这往往显示为锯齿状

context!.interpolationQuality = .high