如何在Swift中用组合图像创建GMSMarker [英] How to create GMSMarker with combined images in Swift

查看:76
本文介绍了如何在Swift中用组合图像创建GMSMarker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在iOS应用中为我的Google地图屏幕创建 GMSMarker 。我需要标记为图像的组合,即通用标记图像和用户图像。
例如:

在这个标记中,我需要适合用户的图像。我的资产中都有图片。
它试过

  func图像(byDrawingImage image:UIImage,inRect rect:CGRect) - > UIImage的! {
UIGraphicsBeginImageContext(size)

draw(in:CGRect(x:0,y:0,width:size.width,height:size.height))

image.draw(in:rect)
let result = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
返回结果
}

但是这返回了一个不完美的图像。
是否有其他解决方案?
注意:这里提供的示例标记图像不是我实际使用的图像。这是一个矩形标记。

解决方案

在这里,我为您提供了一些示例代码,尝试它,它会工作&根据您的要求定制/修改:

  let marker = GMSMarker()
let lat = Double(13.063754 )
let long = Double(80.24358699999993)
marker.position = CLLocationCoordinate2DMake(lat!,long!)

///为自定义标记创建UIView
让DynamicView = UIView(frame:CGRectMake(0,0,50,50))
DynamicView.backgroundColor = UIColor.clearColor()

//为自定义标记创建标记针图像
var imageViewForPinMarker:UIImageView
imageViewForPinMarker = UIImageView(frame:CGRectMake(0,0,40,50));
imageViewForPinMarker.image = UIImage(named:LocationPin)

//创建用户配置文件imageview
var imageViewForUserProfile:UIImageView $ b $ imageViewForUserProfile = UIImageView(frame:CGRectMake 0,0,35,35));
imageViewForUserProfile.image = UIImage(命名为:userprofile)

//将用户配置文件imageview添加到Marker Pin Imageview
imageViewForPinMarker.addSubview(imageViewForUserProfile)

//添加标记针对自定义标记的Imageview isdie视图
DynamicView.addSubview(imageViewForPinMarker)

//转换动态uiview以获取图像/标记图标。
UIGraphicsBeginImageContextWithOptions(DynamicView.frame.size,false,UIScreen.mainScreen()。scale)
DynamicView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let imageConverted:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

marker.icon = imageConverted
marker.map = self.mapView


I need to create a GMSMarker for my Google Maps screen in an iOS app. I need the marker to be a combination of images, i.e. a Generic marker image and a User Image. eg:

Inside this marker, I need to fit the User's image. I have both the images in my assets. it tried

func image(byDrawingImage image: UIImage, inRect rect: CGRect) -> UIImage! {
        UIGraphicsBeginImageContext(size)

        draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))

        image.draw(in: rect)
        let result = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return result
    }

But this returned an imperfect image. Is there an alternate solution? Note: the example marker image provided here is not the one I actually use. That is a rectangular marker.

解决方案

Here I am providing you some sample code, try it, it will work & customize/modify according to your requirement:

let marker = GMSMarker()
let lat = Double("13.063754")
let long = Double("80.24358699999993")
marker.position = CLLocationCoordinate2DMake(lat!,long!)

///Creating UIView for Custom Marker
let DynamicView=UIView(frame: CGRectMake(0, 0, 50, 50))
DynamicView.backgroundColor=UIColor.clearColor()

//Creating Marker Pin imageview for Custom Marker
var imageViewForPinMarker : UIImageView
imageViewForPinMarker  = UIImageView(frame:CGRectMake(0, 0, 40, 50));
imageViewForPinMarker.image = UIImage(named:"LocationPin")

//Creating User Profile imageview
var imageViewForUserProfile : UIImageView
imageViewForUserProfile  = UIImageView(frame:CGRectMake(0, 0, 35, 35));
imageViewForUserProfile.image = UIImage(named:"userprofile")

//Adding userprofile imageview inside Marker Pin Imageview
imageViewForPinMarker.addSubview(imageViewForUserProfile)

//Adding Marker Pin Imageview isdie view for Custom Marker
DynamicView.addSubview(imageViewForPinMarker)

//Converting dynamic uiview to get the image/marker icon.
UIGraphicsBeginImageContextWithOptions(DynamicView.frame.size, false, UIScreen.mainScreen().scale)
DynamicView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let imageConverted: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

marker.icon = imageConverted
marker.map = self.mapView

这篇关于如何在Swift中用组合图像创建GMSMarker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆