当“null”

时间:2018-04-18 01:04:56

标签: ios swift

我想在图片网址为空时显示名为“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”?? thx提前家伙

4 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:1)

因为你的逻辑是错误的。你应该把它改成

var repoTypes = asm.GetTypes().Where(x =>
                                         !x.IsInterface && !x.IsAbstract && x.IsClass &&
                                         !x.IsGenericType &&
                                         x.GetInterfaces().All(y => !y.IsGenericType || !reportRepo.IsAssignableFrom(y.GetGenericTypeDefinition())) &&
                                         x.GetInterfaces().Any(y => y.IsGenericType &&
                                                                    repoInterfaceType.IsAssignableFrom(y.GetGenericTypeDefinition())))
                   .ToList();

答案 2 :(得分:1)

解决了它,并且如果有人在将来遇到同样的问题,我们会分享答案。我使用了以下代码

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

也许其他方面没有必要。随意编辑。干杯!

答案 3 :(得分:0)

  • 检查控件是否到达以下代码行

    self.userPicture.image = UIImage(名称:" Tap_Edit_Profile")

  • 检查replace_regexpr | -------------------------------------------------------------------| extends the functionality one of the two REPLACE function three by | IBOutlet连接

  • 检查您是否传递了正确的图像名称" Tap_Edit_Profile"