简体   繁体   English

虽然图像远程存在于服务器上,但SDWebImage不显示图像

[英]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. 我正在开发一个iOS应用程序,我正在使用SDWebImage作为图像下载程序API。

I'm using a UICollectionView and I do it in the method 我正在使用UICollectionView,我在方法中使用它

- (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.)" 2014-06-15 12:11:50.203 c_shadow_ios_iphone [6867:60b] ===错误:错误域= NSURLErrorDomain代码= -1100“操作无法完成。(NSURLErrorDomain错误-1100。)”

Error -1100: NSURLErrorFileDoesNotExist 错误-1100:NSURLErrorFileDoesNotExist

Also, the UIImage seems to be nil. 此外,UIImage似乎是零。

I've checked the URL that SDWebImage tries to download and it gets me the image properly, so the Error does not seems reasonable 我已经检查了SDWebImage尝试下载的URL,它正确地获取了图像,因此Error似乎不合理

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 图像是NIL

Error: Error Domain=NSURLErrorDomain Code=-1100 "The operation couldn't be completed. (NSURLErrorDomain error -1100.)" 错误:错误域= NSURLErrorDomain代码= -1100“操作无法完成。(NSURLErrorDomain错误-1100。)”

What do I miss here? 我在这里想念什么?

[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. 重点是选项:您需要在方法中提供的SDWebImageRetryFailed选项。 It works for me you can try it out. 它对你有用,你可以尝试一下。

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. 我得到了“Error Domain = SDWebImageErrorDomain Code = -1”试图加载一个nil url“,但图像出现在那个特定的url中。我发现,低分辨率图像会出现错误。为了获得更高质量的图像,加载发生好吧。检查相同的代码以获得更好的分辨率图像。

Download Latest Version SDWebImage from https://github.com/rs/SDWebImage and try again. https://github.com/rs/SDWebImage下载最新版本SDWebImage,然后重试。

It will work. 它会工作。

Try with following code 尝试使用以下代码

Import the category of imageView #import <SDWebImage/UIImageView+WebCache.h> which is already in SDWebImage 导入已存在于SDWebImage中的SDWebImage #import <SDWebImage/UIImageView+WebCache.h> SDWebImage

Then in your cell for cellForItemAtIndexPath 然后在您的单元格中为cellForItemAtIndexPath

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

    [cell.loadIndicator stopAnimating];

}];

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. 我认为IOS不支持图片网址的中文名称。 So you should be trans-coding the url. 所以你应该对网址进行转码。

Example: 例:

url = [_img_url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

For me the issue was very low resolution images. 对我来说,问题是分辨率非常低的图像。 For high resolution images this works perfectly!!!! 对于高分辨率图像,这非常有效!!!!

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

First make sure the image urls are https, not http. 首先确保图片网址是https,而不是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." “无法加载资源,因为App Transport Security策略要求使用安全连接。”

If so add the following to your plist file 如果是这样,请将以下内容添加到plist文件中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM