gpt4 book ai didi

ios - 倾斜文本后创建 UIImage

转载 作者:行者123 更新时间:2023-11-29 12:58:23 24 4
gpt4 key购买 nike

我正在尝试从具有倾斜转换的 NSAttributedString 创建一个 UIImage。我可以创建一个 UILabel 或 UIImageView 并倾斜该 View ,但不幸的是我需要在字符串倾斜后创建一个 UIImage。我试过以下..

CGAffineTransform skewTransform = CGAffineTransformIdentity;
skewTransform.b = (M_1_PI / 2);

UIGraphicsBeginImageContextWithOptions(mSize, NO, 1.0);
UILabel* skewedLabel = [[UILabel alloc] init];
skewedLabel.attributedText = stringToBeSkewed;
CGContextSetTextMatrix(UIGraphicsGetCurrentContext(), skewTransform);
skewedLabel.layer.affineTransform = skewTransform;

// The following 3 calls haven't worked
// [stringToBeSkewed drawInRect:inputRectParam.rect];
// [skewedLabel.layer renderInContext:UIGraphicsGetCurrentContext()];
// [skewedLabel.layer drawInContext:UIGraphicsGetCurrentContext()];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

我知道 CALayer 是 CoreAnimation 而 CoreGraphics 不识别这一点,但是有没有办法让倾斜的文本在上下文中被捕获并转换成 UIImage?

最佳答案

通过执行以下操作,我能够从文本中获取 UIImage,希望这对某人有所帮助。此外,这不是最干净的图像,因此我们将不胜感激任何关于锐化它的建议。

UIFont* font = [UIFont fontWithName:@"(your font name)" size:60];
CTFontRef skewFont = CTFontCreateWithName((__bridge CFStringRef)font.fontName, font.pointSize, NULL);

// Create an attributed string for shadowed text
CFStringRef skewKeys[] = { kCTFontAttributeName, kCTForegroundColorAttributeName };
CFTypeRef skewValues[] = { skewFont, [UIColor colorWithRed:0 green:0 blue:0 alpha:.4].CGColor };
CFDictionaryRef skewAttributes = CFDictionaryCreate(NULL, (const void **)&skewKeys, (const void **)&skewValues,
sizeof(skewKeys) / sizeof(skewKeys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFStringRef theCFString = (__bridge CFStringRef)inputStringParam.string;
CFAttributedStringRef skewString = CFAttributedStringCreate(NULL, theCFString, skewAttributes);
CFRelease(skewAttributes);


// create skew transform
CGAffineTransform skewTransform = CGAffineTransformIdentity;
skewTransform.b = (value to be skewed);

// Draw the string
CTLineRef skewLine = CTLineCreateWithAttributedString(skewString);

CGContextSetTextMatrix(UIGraphicsGetCurrentContext(), CGAffineTransformMakeScale(1.0, -1.0));
CGContextConcatCTM(UIGraphicsGetCurrentContext(), skewTransform);

// draw and capture shadow image
CGContextSetTextPosition(UIGraphicsGetCurrentContext(), inputDrawPointParam.point.x, inputDrawPointParam.point.y);
CTLineDraw(skewLine, UIGraphicsGetCurrentContext());

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

CFRelease(skewLine);
CFRelease(skewString);
CFRelease(skewFont);

关于ios - 倾斜文本后创建 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20414363/

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