繁体   English   中英

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

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

我正在开发一个iOS应用程序,我正在使用SDWebImage作为图像下载程序API。

我正在使用UICollectionView,我在方法中使用它

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

有些图像是下载的,有些则不是。 未下载的那些显示错误消息:

2014-06-15 12:11:50.203 c_shadow_ios_iphone [6867:60b] ===错误:错误域= NSURLErrorDomain代码= -1100“操作无法完成。(NSURLErrorDomain错误-1100。)”

错误-1100:NSURLErrorFileDoesNotExist

此外,UIImage似乎是零。

我已经检查了SDWebImage尝试下载的URL,它正确地获取了图像,因此Error似乎不合理

这是我使用的方法代码:

- (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代码= -1100“操作无法完成。(NSURLErrorDomain错误-1100。)”

我在这里想念什么?

[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选项。 它对你有用,你可以尝试一下。

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

https://github.com/rs/SDWebImage下载最新版本SDWebImage,然后重试。

它会工作。

尝试使用以下代码

导入已存在于SDWebImage中的SDWebImage #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];

}];

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

最后我发现原因是图像网址的名称。 我认为IOS不支持图片网址的中文名称。 所以你应该对网址进行转码。

例:

url = [_img_url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

对我来说,问题是分辨率非常低的图像。 对于高分辨率图像,这非常有效!!!!

像这样使用

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

首先确保图片网址是https,而不是http。 如果是这样,您会在日志中看到以下错误

“无法加载资源,因为App Transport Security策略要求使用安全连接。”

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

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

代码简单直接

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