2017-11-27 13 views
0

그래서 SDWebImage Download image and store to cache for key 이것은 내 응용 프로그램에서 수행하고자하는 작업입니다. 이미지를 키에 저장하고 나중에 이미지를 가져 오려고합니다. 이것은 코드 블록입니다.SDWebImage Download Image Completion Block이 호출되지 않음

SDWebImageManager.shared().imageDownloader?.downloadImage(with: URL.init(string: url), options: SDWebImageDownloaderOptions.highPriority, progress: { (receivedSize:Int,expectedSize:Int,url : URL?) in 
    print("progressing here in this block ") 
}, completed: ({(image : UIImage, data : Data,error : Error, finished : Bool) in 
    print("reached in the completion block ") 

    if finished { 
    SDImageCache.shared().store(image, forKey: "d", toDisk: true, completion: nil) 
    } else { 
    print(error.localizedDescription) 
    showAlert(viewController: self, title: "", message: error.localizedDescription) 
    }   
} as? SDWebImageDownloaderCompletedBlock)) 

그러나 완료 블록은 호출되지 않습니다.

+0

print ("이 블록에서 진행 중입니다") : 인쇄가 진행되고 있습니까? –

+0

그렇지 않으면 진행 블록에 도달하지만 완료 블록에 도달하지 못합니다. –

+0

해결책이 있습니까? –

답변

1

사용이 코드 조각 :

SDWebImageManager.shared().loadImage(with: NSURL.init(string: individualCellData["cover_image"] as! String) as URL?, options: .continueInBackground, progress: { (recieved, expected, nil) in 
      print(recieved,expected) 
     }, completed: { (downloadedImage, data, error, SDImageCacheType, true, imageUrlString) in 
      DispatchQueue.main.async { 
       if downloadedImage != nil{ 


       } 
      } 
     }) 

그리고 당신은 SDWebImage이 이미하고있는 것처럼 이미지를 다시 캐시 할 필요가 없습니다. 캐시에서 이미지를 검색하려면이 코드에서 동일한 URL을 사용하면 캐시에서 이미지를 가져옵니다.

업데이트 코드 :

그런 다음 자신의 키를 사용 loadImage 대신 imageDownloader를 사용하려는 경우 :이 경우 오류가 이미지를 캐시 캐싱 코드를 사용하지 print("Completed")

SDWebImageManager.shared().imageDownloader?.downloadImage(with: NSURL.init(string: individualCellData["cover_image"] as! String) as URL?, options: .continueInBackground, progress: { (recieved, expected, nil) in 
       print(recieved,expected) 
      }, completed: { (image, data, error, true) in 
       print("Completed") 
      }) 

귀하의 사용자 정의 키.

+0

키에 저장하고 나중에 검색해야하지 않습니까? 애플리케이션의 일부 경우와 마찬가지로 특정 토큰이 URL에 추가되고 이미지의 URL이 일정하더라도 토큰은 매번 달라집니다. 따라서 이러한 경우 키를 업데이트하면됩니다. 이것에 대한 아이디어가 있습니까? –

+0

업데이트 된 코드를 확인하십시오. 완료 블록에는 diff가 있습니다. –