Create image from NSString properly

陌路散爱 提交于 2019-12-23 17:22:25

问题


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.


回答1:


Use UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale) instead of UIGraphicsBeginImageContext(size)

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



来源:https://stackoverflow.com/questions/9162838/create-image-from-nsstring-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!