Search code examples
iosswiftsdwebimage

SDWebImage throws an error when setting the image from the url mentioned


When I try to set the image from the following url using SDWebImage I am getting the following error :-

URL :- https://s3.ap-south-1.amazonaws.com/quickride1/1563967975923.png Error :- Error Domain=SDWebImageErrorDomain Code=2001 "(null)" UserInfo={SDWebImageErrorDownloadStatusCodeKey=404}

Have no idea why the issue is popping up. Any solution Thanks in advance

Have gone through several tutorials on sdwebimage and documentation but none of them address my issue

public func setImageToView(imageView : UIImageView, imageUrl : String?,gender : String){
    AppDelegate.getAppDelegate().log.debug("setImageToView() \(gender)")
    if imageUrl == nil || imageUrl?.isEmpty == true || QRSessionManager.getInstance()?.getUserId() == nil || QRSessionManager.getInstance()?.getUserId().isEmpty == true {
        imageView.image = getDefaultUserImage(gender: gender)
        return
    }

    let URL = NSURL(string: imageUrl!)
    if URL == nil
    {
        imageView.image = getDefaultUserImage(gender: gender)
        return
    }
    imageView.sd_imageIndicator = SDWebImageActivityIndicator.gray
    imageView.sd_setImage(with: URL! as URL, placeholderImage: nil , options: SDWebImageOptions(rawValue: 0)) { (image, error, cacheType, url) in
        AppDelegate.getAppDelegate().log.debug("Image :- \(image),error :- \(error),cacheType :- \(cacheType),url :- \(url)")
        if error != nil{
            imageView.image = self.getDefaultUserImage(gender: gender)
            return
        }
         self.checkAndSetCircularImage(imageView: imageView, image: image)
     }

}

Solution

  • Just tried your code and everything works fine :

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        let imgURL = "https://s3.ap-south-1.amazonaws.com/quickride1/1563967975923.png"
    
        setImageToView(imageView : imageView, imageUrl : imgURL,gender : "Male")
    }
    
    public func setImageToView(imageView : UIImageView, imageUrl : String?,gender : String){
    
        let URL = NSURL(string: imageUrl!)
    
        imageView.sd_imageIndicator = SDWebImageActivityIndicator.gray
        imageView.sd_setImage(with: URL! as URL, placeholderImage: nil , options: SDWebImageOptions(rawValue: 0)) { (image, error, cacheType, url) in
            if error != nil{
                print(error)
                return
            }
        }
    
    }
    

    The error message consists in two infos:

    1) SDWebImageErrorDomain Code=2001, which SDWebImageError.h file says:

    SDWebImageErrorInvalidDownloadStatusCode = 2001, 
    // The image downloda response a invalid status code. You can check the status code in error's userInfo under `SDWebImageErrorDownloadStatusCodeKey` 
    

    2) UserInfo={SDWebImageErrorDownloadStatusCodeKey=404}:

    Apparently a Not Found error

    So it appears your device / simulator just isn't reaching the image.

    When exactly does this happen? Are you sure you aren't calling this method multiple times or with malformed image URLs?

    EDIT

    imageView.sd_cancelCurrentImageLoad() before calling the method again solved the problem