Draw text on an image in iOS 13

168 views Asked by At

I am trying to draw an image and some text on a UISlider thumbnail. I have the following code that works fine on iOS 12 and below, but does not on iOS 13 and above(specifically iOS 13.2).

- (UIImage*)getSliderImageWithImage:(UIImage*)bgImage valueText:(NSString*)value {

  CGFloat scale = 5.0;
  //Drawing BG Image
  // Where bg image is some image on top of which i need to render text. 
  UIGraphicsBeginImageContextWithOptions(CGSizeMake(bgImage.size.width * scale, bgImage.size.height * scale), NO, 1);

  // This works
  [bgImage drawInRect:CGRectMake(0, 0, bgImage.size.width * scale, bgImage.size.height * scale)];


  CGRect valueBgRect;
  UIColor * textColor;
  UIFont * textFont;

  textColor = [UIColor whiteColor];
  valueBgRect = CGRectMake(18,40, bgImage.size.width,bgImage.size.height);
  textFont = [UIFont fontWithName:@"Helvetica-Bold" size:50];

  NSDictionary *attrsValue = @{ NSForegroundColorAttributeName : textColor,
                         NSFontAttributeName            : textFont,
                         NSTextEffectAttributeName      : NSTextEffectLetterpressStyle};
  // This doesn't.
  [value drawInRect:valueBgRect withAttributes:attrsValue];
  // Though I can see the image, the text that should be on top of it is not visible. 
  UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  return theImage; }

I also tried setting the text on a UILabel and rendering using

[view.layer renderInContext:UIGraphicsGetCurrentContext()];

and

[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];

But none of these work. Since this a very common scenario, I am surprised that I cannot find any mention of such an issue in iOS 13.2. That leads met to think that I must be looking at it the wrong way. Can anyone please help ?

1

There are 1 answers

0
skety777 On

It's working in iOS 13

enter image description here

 _imgView.image = [self drawFront:[UIImage imageNamed:@"chain_image.jpeg"] text:@"SANKET VAGHELA" atPoint:CGPointMake(50, 50)];

Call this function.

    -(UIImage*)drawFront:(UIImage*)image text:(NSString*)text atPoint: 
   (CGPoint)point
{
    UIFont *font = [UIFont systemFontOfSize:20];
    UIGraphicsBeginImageContext(image.size);
    [image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
    CGRect rect = CGRectMake(point.x, (point.y - 5), image.size.width, image.size.height);
    [[UIColor whiteColor] set];

    NSMutableAttributedString* attString = [[NSMutableAttributedString alloc] initWithString:text];
    NSRange range = NSMakeRange(0, [attString length]);

    [attString addAttribute:NSFontAttributeName value:font range:range];
    [attString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:range];

    NSShadow* shadow = [[NSShadow alloc] init];
    shadow.shadowColor = [UIColor darkGrayColor];
    shadow.shadowOffset = CGSizeMake(1.0f, 1.5f);
    [attString addAttribute:NSShadowAttributeName value:shadow range:range];

    [attString drawInRect:CGRectIntegral(rect)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}