2016-11-10 31 views
0

我有細胞UICollectionView與從網址與標題一起下載的圖像。我做了類似如下: -緩存圖片和負載在UICollectionView滾動與SDWebImageDownloader Xamarin

internal void UpdateCell(ProfileValues mdata) 
     { 


      var mUrl = "someurl"; 

    var manager = SDWebImageManager.SharedManager; 
    manager.ImageCache.MaxCacheAge = 86400; 
      SDWebImageDownloader mDownloader = manager.ImageDownloader; 

      var authToken = "SomeToken""; 


      mDownloader.SetHttpHeaderValue(authToken, "Authorization"); 
      mDownloader.SetHttpHeaderValue("application/json; odata=verbose", "Accept"); 
      mDownloader.SetHttpHeaderValue("f", "X-FORMS_BASED_AUTH_ACCEPTED"); 


      try 
      { 
       mDownloader.DownloadImage(
       url: new NSUrl(mUrl), 
    options: SDWebImageDownloaderOptions.ProgressiveDownload, 
       progressBlock: (receivedSize, expectedSize) => 
       { 
        // Track progress... 
       }, 
       completedBlock: (image, data, error, finished) => 
       { 

        if (image != null && finished) 
        { 
         InvokeOnMainThread(() => 
         { 

          this.imgProfilePic.Image = image; 
         }); 

        } 
        if (error != null) 
        { 
         InvokeOnMainThread(() => 
         { 
          this.imgProfilePic.Image = errorImage; 
          this.imgProfilePic.BackgroundColor = UIColor.FromRGB(52, 31, 71); 
          //this.imgProfilePic.Layer.CornerRadius = this.imgProfilePic.Frame.Size.Width/2; 
          this.imgProfilePic.ClipsToBounds = true; 
          this.imgProfilePic.Layer.BorderWidth = 3.0f; 
          this.imgProfilePic.Layer.BorderColor = UIColor.White.CGColor; 
         }); 
        } 
       } 


      ); 
      } 
      catch (Exception ex) 
      { 
      } 
     } 

但在每次滾動的單元格重新加載和圖像下載每次。如何緩存圖像並在滾動時從緩存中加載圖像?任何幫助表示讚賞。

回答

0

使用FFImageLoading包&使用類cachedImage地方形象的

var cachedImage = new CachedImage() { 
    HorizontalOptions = LayoutOptions.Center, 
    VerticalOptions = LayoutOptions.Center, 
    WidthRequest = 300, 
    HeightRequest = 300, 
    CacheDuration = TimeSpan.FromDays(30), 
    DownsampleToViewSize = true, 
    RetryCount = 0, 
    RetryDelay = 250, 
    TransparencyEnabled = false, 
    Source = "http://loremflickr.com/600/600/nature?filename=simple.jpg", 
    CacheKeyFactory = new CustomCacheKeyFactory(), 
}; 
+0

感謝您的答覆。我還需要傳遞一些標題與請求。我怎樣才能做到這一點 –

+0

你想顯示你加載的圖像url的標題?像特定圖像的文件名? – imrohit

+0

沒有。當我下載圖像時,我想通過一個身份驗證令牌並通過url下載它。 –