gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 18:56:01 26 4
gpt4 key购买 nike

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

- (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 创建了您的上下文,那将自动发生(如果您提供了零的第三个参数),或者您可以显式设置第三个参数以提供一个 scale 为上下文,因此从它派生的图像。但是通过手动构建上下文,您放弃了为其提供规模的能力。

关于ios - iOS 中的 C 字符串看起来很模糊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32698060/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com