gpt4 book ai didi

ios - 在 iOS 13 中的图像上绘制文本

转载 作者:行者123 更新时间:2023-11-28 23:23:59 24 4
gpt4 key购买 nike

我正在尝试在 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 中找不到任何关于此类问题的提及。这导致我认为我一定是看错了。任何人都可以帮忙吗?

最佳答案

它在 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;
}

关于ios - 在 iOS 13 中的图像上绘制文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59155051/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com