how to load image from local path ios swift (by path)

后端 未结 8 1730
长情又很酷
长情又很酷 2020-12-04 20:02

In my app I am storing an image in local storage and I am saving the path of that image in my database. How can I load the image from that path?

Here is the code I a

8条回答
  •  一个人的身影
    2020-12-04 20:40

    This code works for me

    func getImageFromDir(_ imageName: String) -> UIImage? {
    
        if let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
            let fileURL = documentsUrl.appendingPathComponent(imageName)
            do {
                let imageData = try Data(contentsOf: fileURL)
                return UIImage(data: imageData)
            } catch {
                print("Not able to load image")
            }
        }
        return nil
    }
    

提交回复
热议问题