簡體   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