Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create image from NSString properly

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.

like image 986
Rupesh Avatar asked Jun 26 '26 00:06

Rupesh


1 Answers

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.

like image 52
Sahil Mahajan Avatar answered Jun 28 '26 18:06

Sahil Mahajan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!