Ios ImageURL显示白色图像时;空";
当图像URL为空时,我想显示一个名为“Tap_Edit_Profile”的默认图像,但我不确定为什么当图像URL为空时,它会显示白色图片。是我在代码中遗漏了什么吗Ios ImageURL显示白色图像时;空";,ios,swift,Ios,Swift,当图像URL为空时,我想显示一个名为“Tap_Edit_Profile”的默认图像,但我不确定为什么当图像URL为空时,它会显示白色图片。是我在代码中遗漏了什么吗 if let imageURL = self.currentAgent.userPictureURL { if imageURL != "null" { self.userPicture.setImageWithURLString(string: imageURL, shoul
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没有任何图像,因此它不应该显示“点击编辑配置文件”的默认图像吗??
因为你的if逻辑是错误的,所以提前thx伙计们。你应该把它改成
if let imageURL = self.currentAgent.userPictureURL, !imageUrl.isEmpty {
self.userPicture.setImageWithURLString(string: imageURL, shouldFadeIn: true)
} else {
self.userPicture.image = UIImage(named: "Tap_Edit_Profile")
}
因为你的if逻辑是错误的。你应该把它改成
if let imageURL = self.currentAgent.userPictureURL, !imageUrl.isEmpty {
self.userPicture.setImageWithURLString(string: imageURL, shouldFadeIn: true)
} else {
self.userPicture.image = UIImage(named: "Tap_Edit_Profile")
}
- 检查控件是否达到下面的代码行 self.userPicture.image=UIImage(名为:“点击编辑配置文件”)
- 检查
IBOutlet连接userPicture
- 检查您是否传递了正确的图像名称“点击编辑配置文件”
- 检查控件是否达到下面的代码行 self.userPicture.image=UIImage(名为:“点击编辑配置文件”)
- 检查
IBOutlet连接userPicture
- 检查您是否传递了正确的图像名称“点击编辑配置文件”
if let imageURL = self.currentAgent.userPictureURL {
if imageURL != "null" {
self.profilePicture.setImageWithURLString(string: imageURL, placeholderImage: UIImage(named: "Tap_Upload_Photo"), shouldFadeIn: true, withDuration: 0, completionBlock: nil)
}
else {
self.profilePicture.image = UIImage(named: "Tap_Upload_Photo")
}
}
也许不需要其他的。请随意编辑。干杯 解决了这个问题,并认为如果将来有人遇到同样的问题,可以分享答案。我使用了以下代码
if let imageURL = self.currentAgent.userPictureURL {
if imageURL != "null" {
self.profilePicture.setImageWithURLString(string: imageURL, placeholderImage: UIImage(named: "Tap_Upload_Photo"), shouldFadeIn: true, withDuration: 0, completionBlock: nil)
}
else {
self.profilePicture.image = UIImage(named: "Tap_Upload_Photo")
}
}
也许不需要其他的。请随意编辑。干杯 我认为您正在使用SDWebImage从url加载图像。默认情况下,库提供设置默认图像的选项。这是密码
self.profilePicture.sd_setImage(with: URL(string: "url"), placeholderImage: UIImage(named: "Tap_Upload_Photo.png"))
我认为您正在使用SDWebImage从url加载图像。默认情况下,库提供设置默认图像的选项。这是密码
self.profilePicture.sd_setImage(with: URL(string: "url"), placeholderImage: UIImage(named: "Tap_Upload_Photo.png"))
没有足够的信息,因为我们都不知道什么是
setImageWithURLString
,什么是imageURL
,等等。我们添加了这个函数,很抱歉遗漏了setImageWithURLString函数。当你说它为空时,请在你的json中显示图像的url好吗?是“null”还是“null”?信息不够,因为我们都不知道什么是setImageWithURLString
,什么是imageURL
,等等。我们添加了这个函数,很抱歉遗漏了setImageWithURLString函数。当你说它为null时,请在json中显示图像url好吗?是“空”还是“空”?尝试更改它,但仍然显示白色image@WilliamLoke您是否重新检查了图像名称,如果图像名称的类型为.png
,或者您在资源中设置了图像名称,则图像名称将显示。尝试更改图像名称,但仍然显示白色image@WilliamLoke你有没有重新检查你的图像名称,如果它的类型为.png
,或者您在资产中设置了它,它将显示。