raven99
raven99

Reputation: 261

SDWebImage does not show an image although the image exist remotely on the server

I'm developing an iOS application and I'm using SDWebImage as an image downloader API.

I'm using a UICollectionView and I do it in the method

- (UICollectionViewCell *)collectionView:(SMToplinksCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

Some images are downloaded, and some are not. The ones that does not downloaded shows the error message:

2014-06-15 12:11:50.203 c_shadow_ios_iphone[6867:60b] ===Error:Error Domain=NSURLErrorDomain Code=-1100 "The operation couldn’t be completed. (NSURLErrorDomain error -1100.)"

Error -1100: NSURLErrorFileDoesNotExist

Also, the UIImage seems to be nil.

I've checked the URL that SDWebImage tries to download and it gets me the image properly, so the Error does not seems reasonable

Here is the method code I use:

- (UICollectionViewCell *)collectionView:(SMToplinksCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{    
SMToplinkCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:TopLinksCollectionViewID forIndexPath:indexPath];

NSArray *collectionViewArray = [self getToplinksArrayAccordingToTheIndex:collectionView.index];

//  CONFIGURE THE CELL
SMTopLinksObject *dataObject = collectionViewArray[indexPath.item];

//  1. Set the toplink title
cell.title     = dataObject.name;

//  2. Get the default image and blur it
cell.imageName = dataObject.localImageName;

//  3. Activate the preloader that shows the loading status

//  4. Load the image from the server
SDWebImageManager *manager = [SDWebImageManager sharedManager];
NSString *toplinksGetRequest = dataObject.prepareToplinksGetRequest;
NSURL *toplinksURL = [NSURL URLWithString:toplinksGetRequest];

NSLog(@"=== Trying to download image from URL:%@", toplinksGetRequest);
[manager downloadWithURL:toplinksURL
                 options:0
                progress:^(NSInteger receivedSize, NSInteger expectedSize){
                    //progression tracking code                        
                } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished){
                    if(finished)
                    {
                        NSLog(@"===image finished loading, image:%@", image);
                        NSLog(@"===Error:%@", error);
                    }
                    if (image)
                    { cell.shadowPageImageView.image = image; }
}];    
NSLog(@"=========Cell:%@", cell);
return cell;
}

Output

image is NIL

Error: Error Domain=NSURLErrorDomain Code=-1100 "The operation couldn’t be completed. (NSURLErrorDomain error -1100.)"

What do I miss here?

Upvotes: 8

Views: 8175

Answers (8)

ajw
ajw

Reputation: 2702

First make sure the image urls are https, not http. if so you would see following error on the log

"The resource could not be loaded because the App Transport Security policy requires the use of a secure connection."

If so add the following to your plist file

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

Code is simple and straight forward

import SDWebImage

if let url = URL(string: "imageUrl") {
   yourImageView.sd_setImage(with: url, completed: nil)
}

Upvotes: 2

Dipang
Dipang

Reputation: 1121

Use like this

self.imgUserProfile.sd_setImage(with: imageUrl, placeholderImage: UIImage(named: "profile_placeholder"), options: SDWebImageOptions.refreshCached, progress: nil, completed: { (image, error, cache, url) in
        if error == nil {
           self.imgUserProfile.image = image
       } 
  }) 

Upvotes: 0

aqsa arshad
aqsa arshad

Reputation: 831

For me the issue was very low resolution images. For high resolution images this works perfectly!!!!

Upvotes: 0

user6457921
user6457921

Reputation: 1

I encountered the same problem. I tried all the way mentioned above but could not work well.

Finally I found out the reason is the name of the url of image. I think that IOS does not support the Chinese name of image url. So you should be trans-coding the url.

Example:

url = [_img_url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

Upvotes: 0

bharathi kumar
bharathi kumar

Reputation: 208

I had the same issue. I got "Error Domain=SDWebImageErrorDomain Code=-1 "Trying to load a nil url", but the image is present in that particular url. I found that,the error occurs for low resolution images. For better quality images , the loading happens well. Check the same code for better resolution images.

Upvotes: 3

Gautam Sareriya
Gautam Sareriya

Reputation: 1833

[cell.diviceIV sd_setImageWithURL:[NSURL URLWithString:imgStringUrl] placeholderImage:nil options:SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    if (image){
        // Set your image over here
       }else{
           //something went wrong
       }

}];

The main point is options:SDWebImageRetryFailed option you need to give in your method. It works for me you can try it out.

Upvotes: 4

Janmenjaya
Janmenjaya

Reputation: 4163

Try with following code

Import the category of imageView #import <SDWebImage/UIImageView+WebCache.h> which is already in SDWebImage

Then in your cell for cellForItemAtIndexPath

[cell.imgVwUser sd_setImageWithURL:[NSURL URLWithString:objPhoto.imageUrl] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    DLogs(@"completed");

    [cell.loadIndicator stopAnimating];

}];

Upvotes: 0

Tejinder
Tejinder

Reputation: 1557

Download Latest Version SDWebImage from https://github.com/rs/SDWebImage and try again.

It will work.

Upvotes: 1

Related Questions