如何旋转字符串并将此字符串保存为 swift 中的 UIImage

How to rotate String and saved this String as UIImage in swift

我需要旋转 String 并将这个旋转字符串保存为 UIImage

假设我给定的字符串是= HELLO WORLD

现在我需要先旋转(比如 45 度)它然后保存为 UIImage 这样我就可以将这个 UIImage 显示到我的 UIImageView

请帮助我。所以我可以旋转 String 并将这个旋转字符串保存为 Swift 中的 UIImage?。

我找到了这个答案 Drawing rotated text with NSString drawInRect。但是无法实现结果。

可能是下面的代码对你有帮助,我实现了将字符串旋转 45 度的相同结果

-(UIImage*) drawText:(NSString*) text
         atPoint:(CGPoint)   point
{
UIGraphicsBeginImageContextWithOptions(CGSizeMake(65, 65),NO,0.0);

CGRect rect = CGRectMake(point.x, point.y, 50, 30);
[[UIColor whiteColor] set];
CGContextConcatCTM(UIGraphicsGetCurrentContext(), CGAffineTransformMakeTranslation(32.5, 32.5));
CGContextConcatCTM(UIGraphicsGetCurrentContext(), CGAffineTransformMakeRotation(M_PI / 4));
CGContextConcatCTM(UIGraphicsGetCurrentContext(), CGAffineTransformMakeTranslation(-32.5, -32.5));
//[text drawInRect:CGRectIntegral(rect) withFont:font ];

NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
[paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];
[paragraphStyle setAlignment:NSTextAlignmentCenter];
NSDictionary *attributes = @{ NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:12],
                              NSForegroundColorAttributeName: [UIColor blackColor],

                              NSParagraphStyleAttributeName:paragraphStyle};

[text drawInRect:CGRectIntegral(rect) withAttributes:attributes];


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

return newImage;
}

下面是我得到结果的截图