gpt4 book ai didi

ios - "null"时显示白色图像的 ImageURL

转载 作者:搜寻专家 更新时间:2023-11-01 06:01:13 27 4
gpt4 key购买 nike

当图像 URL 为空时,我想显示一个名为“Tap_Edit_Profile”的默认图像,但我不确定为什么当它为空时,它显示一张白色图片。这是我在代码中遗漏了什么吗?

 if let imageURL = self.currentAgent.userPictureURL {
if imageURL != "null" {
self.userPicture.setImageWithURLString(string: imageURL, shouldFadeIn: true)

}
else {
self.userPicture.image = UIImage(named: "Tap_Edit_Profile") }

下面是我的 setImageWithURLString 函数:

    func setImageWithURLString(string: String?, placeholderImage: UIImage? = nil ,shouldFadeIn fade: Bool, withDuration duration: TimeInterval = 0.5, completionBlock: (() -> Void)? = nil) {

if let string = string {
if let url: URL = URL(string: string) {
if SDWebImageManager.shared().cachedImageExists(for: url) || fade == false {
self.alpha = 1
} else {
self.alpha = 0
}

self.sd_setImage(with: url, placeholderImage: placeholderImage, options: .retryFailed, completed: { (image, error, type, url) in
completionBlock?()
if type == SDImageCacheType.none {
UIView.animate(withDuration: duration, animations: {
self.alpha = 1
})
} else {
self.alpha = 1
}
})
return
}
}
if fade {
UIView.animate(withDuration: duration, animations: { self.alpha = 1 })
}
}

imageURL 没有任何图像,因此它不应该显示我的“Tap_Edit_Profile”默认图像吗?提前谢谢大家

最佳答案

我认为您正在使用 SDWebImage 从 url 加载图像。默认情况下,库提供了一个选项来设置默认图像。这是代码

self.profilePicture.sd_setImage(with: URL(string: "url"), placeholderImage: UIImage(named: "Tap_Upload_Photo.png")) 

关于ios - "null"时显示白色图像的 ImageURL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49889582/

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