2015-09-21 34 views
0

我一直在尝试渲染弧中的文本。文本如预期呈现,但看起来很模糊。我该如何解决这个问题。C在iOS中的字符串看起来模糊

- (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]; 
} 

回答

4

一个问题是,您不允许屏幕分辨率。使您的位图上下文两倍大,或三倍大;适当地乘以所有的值(如果你刚开始应用一个比例CTM,这是最容易的);然后在最后,不要致电imageWithCGImage:,拨打imageWithCGImage:scale:orientation:,设置相应的比例。

如果您已经创建了UIGraphicsBeginImageContextWithOptions的背景下,会自动发生(如果你已经提供的零第三个参数),或者你可以明确地设置了第三个参数提供上下文,因此图像的scale从它派生。但是通过手动构建您的环境,您丢掉了为其提供scale的能力。