从NSString正确创建图像 [英] Create image from NSString properly

查看:57
本文介绍了从NSString正确创建图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在根据字符串变量创建图像.以下是创建图片的代码段

I am creating a image from string variable. Following is the code snippet to create image

-(UIImage *)imageFromText:(NSString *)text FontName:(UIFont *)font
{
    // set the font type and size
    //UIFont *font = [UIFont systemFontOfSize:20.0];  
    CGSize size  = [text sizeWithFont:font];

    UIGraphicsBeginImageContext(size);

    [text drawAtPoint:CGPointMake(0.0, 0.0) withFont:font];

    // transfer image
    CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
    CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES);

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

    return image;
}

以上代码效果很好,但问题是最终图像模糊.如果上面的代码有什么问题,请提出建议.

Above code works well but problem is that final image is blurred. Please advice if anything is wrong in above code.

推荐答案

使用 UIGraphicsBeginImageContextWithOptions(CGSize size,BOOL opaque,CGFloat scale)代替 UIGraphicsBeginImageContext(size )

根据屏幕尺寸设置比例.这将对您有帮助,并且您不会得到任何模糊的图像.

Set Scale according to the screen size. This will help you and you will not get any blurred image.

这篇关于从NSString正确创建图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆