SDWebImage无法显示图片,尽管该图片在远程服务器上存在。

8
我正在开发一个iOS应用程序,使用SDWebImage作为图像下载器API。我在一个UICollectionView中使用它,并在方法中处理:
- (UICollectionViewCell *)collectionView:(SMToplinksCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
一些图像已经被下载,而有些则没有。未下载的那些显示错误信息:
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.)"
错误-1100:NSURLErrorFileDoesNotExist
此外,UIImage似乎为nil。
我已经检查了SDWebImage尝试下载的URL,并成功获取了图像,因此错误似乎不合理。
以下是我使用的方法代码:
- (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;
}

输出

图像为NIL

错误:错误域=NSURLErrorDomain Code=-1100“操作无法完成。(NSURLErrorDomain error -1100.)”

我错过了什么?


你有使用过UIImageView+AFNetworking来加载图片吗? - Nirav Jain
我已经使用了UIImageView+WebCache.h,因为它在SDWebImage的ReadMe中被建议使用。 - raven99
1
你解决了吗?我有类似的问题。谢谢。 - George
你在使用故事板吗? - Daljeet
我有同样的问题,错误提示为“尝试加载空url”。 - bharathi kumar
8个回答

4
[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
       }

}];

关键点是选项:你需要在方法中提供SDWebImageRetryFailed选项。 这对我很有效,你可以试一下。


这个“特性”(failedURLs)基本上是 SDWebImage 内置的一个 bug。我建议看看其他框架(有很多)。 - kean

3

我也遇到了同样的问题。 错误提示:"Error Domain=SDWebImageErrorDomain Code=-1 "Trying to load a nil url",但是图片确实在该URL中存在。 我发现,这个错误是由低分辨率图片引起的。 对于高质量的图片,加载工作就可以正常进行。 请尝试使用更高分辨率的图片,并检查相应的代码。


2

首先确保图像url是https,而不是http。如果是http,您将在日志中看到以下错误:

"由于应用程序传输安全策略要求使用安全连接,因此无法加载资源。"

如果是这样,请将以下内容添加到您的plist文件中

```xml NSAppTransportSecurity NSAllowsArbitraryLoads ```

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

最初的回答很简单明了,代码简洁易懂。
import SDWebImage

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

1

0

尝试使用以下代码

导入imageView的类别#import <SDWebImage/UIImageView+WebCache.h>,该类别已经在SDWebImage中。

然后在cellForItemAtIndexPath中使用。

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

    [cell.loadIndicator stopAnimating];

}];

0

使用方法如下

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

0

我遇到了同样的问题。我尝试了上面提到的所有方法,但都无法正常工作。

最后我发现问题出在图像 URL 的名称上。我认为 IOS 不支持图像 URL 的中文名称。因此,你应该进行转码处理。

示例:

url = [_img_url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

0
对我来说,问题在于图像分辨率太低。对于高分辨率的图像,这个程序完美运行!!!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接