将标记图标设置为UIImage并使用背景正确调整其大小使其显示为图标

时间:2016-09-03 04:51:57

标签: swift google-maps firebase uiimage firebase-realtime-database

我目前拥有的代码从firebase获取了个人资料图片,并将其作为标记图标显示在地图上。

                self.ref.child("users").child(location.key as! String).child("userPhoto").observeSingleEventOfType(.Value, withBlock: { (snapshot: FIRDataSnapshot) in
                    if(snapshot.exists()) {

                        let profileUrl = snapshot.value as? String
                        let url = NSURL(string: profileUrl!)
                        NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) in

                            if error != nil {
                                print(error)
                                return
                            }

                            dispatch_async(dispatch_get_main_queue(), {

                                if let downloadedImage = UIImage(data: data!) {
                                    marker.icon = downloadedImage

                                }


                            })

                        }).resume()
                    }
                })

不幸的是,这段代码只是在地图上拍了一张我不想要的大图像。我想要一个带有边框/背景的圆形图像,使其看起来像一个图钉/标记。像这样:

Ideal Deign of The Marker Icon

有人知道怎么做吗?

任何帮助将不胜感激。 谢谢!

P.S:我是swift的新手,如果你能用你提供的任何解决方案向我展示一些代码,我将非常感激。谢谢!

1 个答案:

答案 0 :(得分:1)

func changeImageDimensions(image: UIImage, newWidth: CGFloat, newHeight: CGFloat) -> UIImage {
 let widthRat  = newWidth/image!.size.width
 let heightRat = newHeight/image!.size.height
 var newSize: CGSize = CGSize() 
        if(widthRat > heightRat) {
          newSize = CGSizeMake(image!.size.width * heightRat, image!.size.height * heightRat)
         } else {
           newSize = CGSizeMake(image!.size.width * widthRat,  image!.size.height * widthRat)
     }

    UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight))
    let frameRect = CGRectMake(0, 0, newSize.width, newSize.height)
    image.drawInRect(frameRect)
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return newImage
}

self.ref.child("users").child(location.key as! String).child("userPhoto").observeSingleEventOfType(.Value, withBlock: { (snapshot: FIRDataSnapshot) in
                if(snapshot.exists()) {

                    let profileUrl = snapshot.value as? String
                    let url = NSURL(string: profileUrl!)
                    NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) in

                        if error != nil {
                            print(error)
                            return
                        }

                        dispatch_async(dispatch_get_main_queue(), {

                            if let downloadedImage = UIImage(data: data!) {
                                marker.icon = changeImageDimensions (downloadedImage!,newWidth:CGFloat(150),newHeight:CGFloat(150))

                            }


                        })

                    }).resume()
                }
            })