Badr Bujbara
Badr Bujbara

Reputation: 8671

iOS Swift: SDWebImage Image Loading Error

I'm using SDWebImage to load images into a paginated horizontal collection view. Some images aren't loaded and give an error "canceled."

Here is how I declare the cell:

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PHOTOS_CELL_ID, for: indexPath) as! PhotosCell

Here is how I load images into the cell's image view:

            cell.photoImgV.sd_setImage(with: URL(string:photo[Constants.PHOTO_URL] as! String), placeholderImage: nil, options: SDWebImageOptions(rawValue: 0), completed: { (img, err, cacheType, imgURL) in
            if err != nil{
                print("ERROR LOADING IMAGE: \(err?.localizedDescription ?? "ERRRRoR")")
            }
                // prints: ERROR LOADING IMAGE: cancelled

I hope there is a fix for that.

Upvotes: 0

Views: 2469

Answers (1)

trungduc
trungduc

Reputation: 12144

You should know how SDWebImage implement their sd_setImage methods. At the the beginning of methods, sd_cancelImageLoadOperationWithKey is called to cancel current request and start a request with new url.

enter image description here

In your situation, I guess error is logged because cells are reused.

When a cell is displayed on screen, it starts a request to fetch image by using sd_setImage. Before this request is completed, you scroll and make this cell move out of screen. At this moment, you continue to scroll and cell is reused. sd_setImage is called again on this cell with another url while current request isn't completed. And as I said above, sd_setImage cancels current request before starting a new request.

cancelled error is logged because sd_cancelImageLoadOperationWithKey is called at the beginning of sd_setImage method.

You shouldn't care about cancelled error in this situation. Your code is running right way.

Upvotes: 2

Related Questions