[英]Can not get proper firebase image save in document directory in swift 4 iOS
i'm saving firebase image in document directory !! 我将Firebase图片保存在文档目录中! For uniqueness my Document Directory name is firebase image name !
为了唯一起见,我的文档目录名称是firebase图像名称! i have check with a condition that if firebase image name is not exists in my document directory then save that image in Document Directory !!
我有一个条件检查,如果我的文档目录中不存在Firebase图像名称,则将该图像保存在文档目录中!
Issue is when i try to get image :- (1) For example :- Firebase Image name is 1.jpg . 问题是当我尝试获取图像时:-(1)例如:-Firebase图像名称为1.jpg。 (2) Document Directory save image with firebase name like 1.jpg .
(2)文档目录以Firebase名称保存图像,如1.jpg。 (3) now i change firebase image to other but it save with name 1.jpg .
(3)现在我将firebase图像更改为其他图像,但其名称为1.jpg。 (4) when i try to get image because already 1.jpg is in Document Directory that's why it not return me updated image it show me previous 1.jpg image !!
(4)当我尝试获取图像,因为文档目录中已经有1.jpg图像时,这就是为什么它不返回我更新的图像,而是显示以前的1.jpg图像的原因!
how can i solved this issue. 我该如何解决这个问题。 Thank You
谢谢
Save And Get Image Code :- 保存并获取图像代码:-
func saveImageDocumentDirectory(imageName: String){
let documentPath = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let imgURL = documentPath.appendingPathComponent(imageName, isDirectory: true)
if !FileManager.default.fileExists(atPath: imgURL.path){
do{
let image = UIImage(named: imgURL.path)
try image?.pngData()?.write(to: imgURL)
}catch let err{
print("error in save:\(err.localizedDescription)")
}
}
}
func getDocumentImage(imageName: String, firebaseImgURL: URL, imgView:UIImageView){
let documentPath = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let imgURL = documentPath.appendingPathComponent(imageName, isDirectory: true)
let image = UIImage(contentsOfFile: imgURL.path)
if image != nil{
imgView.image = image
}else{
imgView.kf.setImage(with: firebaseImgURL)
}
}
Please try with like below code 请尝试使用类似下面的代码
Save Data
let placeholderURL: URL =
getDocumentDirectoryPath()!.appendingPathComponent("\(imageName.JPG)/",
isDirectory: true)
let fileExists: Bool = FileManager.default.fileExists(atPath: placeholderURL.path )
if !fileExists {
let thumbnailImageURL = URL(string: firebaseURL)
var placeholderData: Data? = nil
if let url = url {
do {
placeholderData = try Data(contentsOf: (thumbnailImageURL ?? nil)!)
} catch {
print("Unable to load data: \(error)")
}
}
if urlData != nil {
do {
//saving is done on main thread
try placeholderData?.write(to: URL(fileURLWithPath: placeholderURL.path) , options: .atomic)
} catch {
print(error)
}
}
}
Retrive image 检索图像
let thumbnailURL: URL = getDocumentDirectoryPath()!.appendingPathComponent("\("imageName.JPG")/", isDirectory: true)
let fileExists: Bool = FileManager.default.fileExists(atPath: thumbnailURL.path)
var urlThumbnail:URL? = nil
if fileExists {
urlThumbnail = URL(fileURLWithPath: thumbnailURL.path)
} else {
urlThumbnail = URL(string: firebaseURL)
}
Sat image in image view 图像视图中的卫星图像
self.imgtemp.sd_setImage(with: urlThumbnail, placeholderImage: UIImage(named: "placeholder.png"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.