gpt4 book ai didi

ios - 以编程方式创建 UIImage 以在谷歌地图上显示标记?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:22:35 25 4
gpt4 key购买 nike

在我的应用程序中,我集成了适用于 iOS 的 Google map SDK。我想显示带有序列号的标记。比如 this image标记的数量将在运行时决定,所以我不能简单地为每个标记放置一个法师,我必须以某种方式以编程方式创建它。我有一张没有序列的图像。我的想法是使用该图像创建一个图像并在创建它时在其上写上数字。但是不知道怎么办。任何帮助,将不胜感激。

最佳答案

感谢@knshn 在评论中给我链接。这是我的解决方案

-(UIImage *)getImage :(UIImage *)icon stop:(NSString *)stopNumber color:(UIColor *)color
{
// create label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, icon.size.width,icon.size.height)];
[label setText:stopNumber];
[label setTextColor:color];
[label setFont:[UIFont boldSystemFontOfSize:11]];
label.textAlignment = NSTextAlignmentCenter;

// use UIGraphicsBeginImageContext() to draw them on top of each other

//start drawing
UIGraphicsBeginImageContext(icon.size);

//draw image
[icon drawInRect:CGRectMake(0, 0, icon.size.width, icon.size.height)];

//draw label
[label drawTextInRect:CGRectMake((icon.size.width - label.frame.size.width)/2, -5, label.frame.size.width, label.frame.size.height)];

//get the final image
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
return resultImage;
}

在 Swift 中使用

func getImage(_ icon: UIImage?, stop stopNumber: String?, color: UIColor?) -> UIImage? {
// create label
let label = UILabel(frame: CGRect(x: 0, y: 0, width: icon?.size.width ?? 0.0, height: icon?.size.height ?? 0.0))
label.text = stopNumber
label.textColor = color
label.font = FontFamily.Metropolis.semiBold.font(size: 15)
label.textAlignment = .center

//start drawing
UIGraphicsBeginImageContext(icon?.size ?? CGSize.zero)

//draw image
icon?.draw(in: CGRect(x: 0, y: 0, width: icon?.size.width ?? 0.0, height: icon?.size.height ?? 0.0))

//draw label
label.drawText(in: CGRect(x: ((icon?.size.width ?? 0.0) - label.frame.size.width) / 2, y: -3, width: label.frame.size.width, height: label.frame.size.height))

//get the final image
let resultImage = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()
return resultImage
}

关于ios - 以编程方式创建 UIImage 以在谷歌地图上显示标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24409028/

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