2

我想在代表手表编号的圆圈中绘制 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,边框透明

在此处输入图像描述

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

用文字绘制

4

3 回答 3

6

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

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

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

于 2013-01-18T19:29:34.277 回答
0

尝试

CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
于 2013-01-18T19:16:15.417 回答
0

斯威夫特 3:

context!.interpolationQuality = .high
于 2017-08-12T16:24:32.107 回答