2017-08-29 8 views
0

私はKingfisherを使用してURLのセットから画像をダウンロードしています。Kingfisherを使用してダウンロードした画像をドキュメントディレクトリに保存します

ドキュメントディレクトリにダウンロードまたはキャッシュされたイメージ(実際のサイズのイメージ)を保存する必要があります。

これは可能ですか?次のコードを使用して画像表示にして

let documentsDirectoryURL = try! FileManager().url(for: 
.documentDirectory, in: .userDomainMask, appropriateFor: nil, create: 
true) 
// create a name for your image 
let fileURL = 
documentsDirectoryURL.appendingPathComponent("imgName.png") 


    if !FileManager.default.fileExists(atPath: fileURL.path) { 
    do { 
    try UIImagePNGRepresentation(cahedimage!)!.write(to: fileURL) 
     print("Image Added Successfully") 
    } catch { 
     print(error) 
    } 
    } else { 
    print("Image Not Added") 
} 
+2

はい、これまでに何を試みましたか?何がうまくいかないの? – Scriptable

+0

私はkf.setImage()を使った完了ハンドラを使ってやりました。 – AKNinan

答えて

0

はこれを試してみてください。補完ハンドラは、画像がダウンロードされた後にどのようなタスクを行うのにも役立ちます。

loadImage(fromUrl: URL(string: imageUrl)!, imageView: imgView, fileName: image.png) 

func loadImage(fromUrl: URL, imageView: UIImageView, fileName: String) { 
imageView.kf.indicatorType = .activity 
imageView.kf.setImage(with:fromUrl, placeholder: nil, options: [.transition(.none)], progressBlock: nil, completionHandler: {(result) in 
    let documentsDirectoryURL = try! FileManager().url(for: 
     .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: 
     true) 

    let fileURL = 
     documentsDirectoryURL.appendingPathComponent(fileName) 


    if !FileManager.default.fileExists(atPath: fileURL.path) { 
     do { 
      try UIImagePNGRepresentation(imageView.image!)!.write(to: fileURL) 
      print("Image Added Successfully") 
     } catch { 
      print(error) 
     } 
    } else { 
     print("Image Not Added") 
    } 
}) 
} 
+0

このコードは、ドキュメントディレクトリに保存します。私の質問はそうではありませんでした。 – AKNinan

0

ロードイメージ - :

関連する問題