我正在尝试在 UISlider 缩略图上绘制图像和一些文本。我有以下代码在 iOS 12 及更低版本上运行良好,但在 iOS 13 及更高版本(特别是 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; }
我还尝试在 UILabel 上设置文本并使用
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
和
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
但这些都不起作用。由于这是一个非常常见的场景,我很惊讶在 iOS 13.2 中找不到任何提及此类问题的内容。这导致人们认为我一定是以错误的方式看待它。有人可以帮忙吗?