在我的应用程序中,我集成了适用于 iOS 的 Google map SDK。我想在上面显示带有序列号的标记。如
标记的数量将在运行时决定,所以我不能简单地为每个标记放置一个法师,我必须以某种方式创建它。我有一张没有序列的图像。我的想法是使用该图像创建图像并在创建时在其上写上数字。但不知道如何。
任何帮助,将不胜感激。
Best Answer-推荐答案 strong>
感谢@knshn 在评论中给我链接。这是我的解决方案
-(UIImage *)getImage UIImage *)icon stopNSString *)stopNumber colorUIColor *)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/
|