我目前拥有的代码从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:我是斯威夫特的新手,如果你能给我看一些代码和你提供的任何解决方案,我将非常感激。谢谢!
最佳答案
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()
}
})
关于swift - 将标记图标设置为UIImage并适当调整其背景大小以使其显示为图标,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39303359/