gpt4 book ai didi

swift - 将标记图标设置为 UIImage 并使用背景适当调整其大小以使其显示为图标

转载 作者:行者123 更新时间:2023-11-28 16:13:47 26 4
gpt4 key购买 nike

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

                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()
}
})

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

Ideal Deign of The Marker Icon

有人知道怎么做吗?

如有任何帮助,我们将不胜感激。谢谢!

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

最佳答案

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com