【问题标题】:create UIImage programmatically for show marker on google map?以编程方式创建 UIImage 以在谷歌地图上显示标记?
【发布时间】:2014-06-25 12:44:00
【问题描述】:

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

【问题讨论】:

标签: ios objective-c google-maps google-maps-sdk-ios


【解决方案1】:

感谢@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
}

【讨论】:

    猜你喜欢
    • 2019-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-08
    • 2019-06-04
    • 2013-07-22
    • 1970-01-01
    • 2021-07-07
    相关资源
    最近更新 更多