如何在 UIImage 中写入一些带有特殊字符的文本?

发布于 2024-12-01 02:50:26 字数 1642 浏览 2 评论 0 原文

我想在 UIImage 中写入一些文本,我发现了这个 link 它可以工作,但是如果您尝试编写一些特殊字符,例如“á,é,í,ö,ô...”,那么它会失败。

代码:

//Add text to UIImage
-(UIImage *)addText:(UIImage *)img text:(NSString *)text1{
    int w = img.size.width;
    int h = img.size.height; 
    //lon = h - lon;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);

    CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
    CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1);

    char* text  = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];// "05/05/09";
    CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode(context, kCGTextFill);
    CGContextSetRGBFillColor(context, 255, 255, 255, 1);


    //rotate text
    CGContextSetTextMatrix(context, CGAffineTransformMakeRotation( -M_PI/4 ));

    CGContextShowTextAtPoint(context, 4, 52, text, strlen(text));


    CGImageRef imageMasked = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);

    return [UIImage imageWithCGImage:imageMasked];
}

有什么想法可以解决这个问题吗?

谢谢。

编辑:

我尝试更改:

char* text  = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];

char* text  = (char *)[text1 cStringUsingEncoding:NSUTF8StringEncoding];

它不起作用。

I want to write some text in a UIImage, I've found this link and it works, but if you try to write some special characters like "á, é, í, ö, ô..." then it fails.

The code:

//Add text to UIImage
-(UIImage *)addText:(UIImage *)img text:(NSString *)text1{
    int w = img.size.width;
    int h = img.size.height; 
    //lon = h - lon;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);

    CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
    CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1);

    char* text  = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];// "05/05/09";
    CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode(context, kCGTextFill);
    CGContextSetRGBFillColor(context, 255, 255, 255, 1);


    //rotate text
    CGContextSetTextMatrix(context, CGAffineTransformMakeRotation( -M_PI/4 ));

    CGContextShowTextAtPoint(context, 4, 52, text, strlen(text));


    CGImageRef imageMasked = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);

    return [UIImage imageWithCGImage:imageMasked];
}

Is there any idea to solve this issue?

Thank you.

EDIT:

I've tried to change:

char* text  = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];

to

char* text  = (char *)[text1 cStringUsingEncoding:NSUTF8StringEncoding];

but it doesn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

≈。彩虹 2024-12-08 02:50:26

另一位用户告诉我该怎么做:

-(UIImage *)writeText:(NSString *)text1 toImage:(UIImage *)img{

    CGSize sizeText = [text1 sizeWithFont:[UIFont fontWithName:@"Helvetica" size:36] minFontSize:36 actualFontSize:nil forWidth:783 lineBreakMode:UILineBreakModeTailTruncation];

    CGFloat posX = 1024.0 - 230.0 - sizeText.width;


    UIGraphicsBeginImageContextWithOptions(img.size, NO, 0.0f);
    [img drawAtPoint:CGPointMake(0.0f, 0.0f)];
    [text1 drawAtPoint:CGPointMake(posX, 588.0) withFont:[UIFont fontWithName:@"Helvetica" size:36]];
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return result;
} 

在此 链接

Another user told me how to do it:

-(UIImage *)writeText:(NSString *)text1 toImage:(UIImage *)img{

    CGSize sizeText = [text1 sizeWithFont:[UIFont fontWithName:@"Helvetica" size:36] minFontSize:36 actualFontSize:nil forWidth:783 lineBreakMode:UILineBreakModeTailTruncation];

    CGFloat posX = 1024.0 - 230.0 - sizeText.width;


    UIGraphicsBeginImageContextWithOptions(img.size, NO, 0.0f);
    [img drawAtPoint:CGPointMake(0.0f, 0.0f)];
    [text1 drawAtPoint:CGPointMake(posX, 588.0) withFont:[UIFont fontWithName:@"Helvetica" size:36]];
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return result;
} 

In this link

凉城凉梦凉人心 2024-12-08 02:50:26

尝试使用 NSUTF8StringEncoding 而不是 NSASCIIStringEncoding

try using NSUTF8StringEncoding instead of NSASCIIStringEncoding

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文