iOS中的C字符串看起来很模糊



我一直在尝试以弧线呈现文本。文本按预期呈现,但看起来很模糊。如何解决这个问题?

- (UIImage*) createMenuRingWithFrame:(CGRect)frame
{
    NSArray* sections = [[NSArray alloc] initWithObjects:@"daily", @"yearly", @"monthly", @"weekly",nil];
    CGRect imageSize = frame;
    float perSectionDegrees = 360 / [sections count];
    float totalRotation = 135;
    float fontSize = ((frame.size.width/2) /2)/2;
    self.menuItemsFont = [UIFont fontWithName:@"Avenir" size:fontSize];

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, imageSize.size.width, imageSize.size.height, 8, 4 * imageSize.size.width, colorSpace,(CGBitmapInfo) kCGImageAlphaPremultipliedFirst);

    CGPoint centerPoint = CGPointMake(imageSize.size.width / 2, imageSize.size.height / 2);
    double radius = (frame.size.width / 2)-2;

    for (int index = 0; index < [sections count]; index++)
    {
        BOOL textRotationDown = NO;
        NSString* menuItemText = [sections objectAtIndex:index];
        CGSize textSize =  [menuItemText sizeWithAttributes:
                            @{NSFontAttributeName: self.menuItemsFont}];
        char* menuItemTextChar = (char*)[menuItemText cStringUsingEncoding:NSASCIIStringEncoding];
        if (totalRotation>200.0 && totalRotation <= 320.0) {
            textRotationDown = YES;
        }
        else
            textRotationDown= NO;
        float x = centerPoint.x + radius * cos(DEGREES_TO_RADIANS(totalRotation));
        float y = centerPoint.y + radius * sin(DEGREES_TO_RADIANS(totalRotation));
        CGContextSaveGState(context);
        CFStringRef font_name = CFStringCreateWithCString(NULL, "Avenir", kCFStringEncodingMacRoman);
        CTFontRef font = CTFontCreateWithName(font_name, fontSize, NULL);
        CFStringRef keys[] = { kCTFontAttributeName };
        CFTypeRef values[] = { font };
        CFDictionaryRef font_attributes = CFDictionaryCreate(kCFAllocatorDefault, (const void **)&keys, (const void **)&values, sizeof(keys) / sizeof(keys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
        CFRelease(font_name);
        CFRelease(font);

        CFStringRef string = CFStringCreateWithCString(NULL, menuItemTextChar, kCFStringEncodingMacRoman);
        CFAttributedStringRef attr_string = CFAttributedStringCreate(NULL, string, font_attributes);
        CTLineRef line = CTLineCreateWithAttributedString(attr_string);
        CGContextTranslateCTM(context, x, y);

         CGContextRotateCTM(context, DEGREES_TO_RADIANS(totalRotation - (textRotationDown?275:90)));
        CGContextSetTextPosition(context,0 - (textSize.width / 2), 0 - (textSize.height / (textRotationDown?20:4)));
        CTLineDraw(line, context);
        CFRelease(line);
        CFRelease(string);
        CFRelease(attr_string);
        CGContextRestoreGState(context);
        totalRotation += perSectionDegrees;
    }
    CGImageRef contextImage = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    return [UIImage imageWithCGImage:contextImage];
}

一个问题是你没有考虑屏幕分辨率。将您的位图上下文设置为两倍或三倍大;适当地将所有值相乘(如果你在一开始就使用比例CTM,这是最简单的);最后,不呼叫imageWithCGImage:,呼叫imageWithCGImage:scale:orientation:,设置相应的刻度。

如果您使用UIGraphicsBeginImageContextWithOptions创建了上下文,那么这将自动发生(如果您提供了第三个参数为0),或者您可以显式地设置第三个参数来为上下文提供scale,从而从它派生出图像。但是,通过手动构建上下文,您放弃了为其提供scale的能力。

最新更新