Get an image from it's name from Document directory iOS swift

Solution:

If you saved an image to document directory in your app you cannot have to save the full url path to your database or coredata.

Because every time document directory path will be modified.So you can save the image name in your database and fetch it later using the below code.


func getImage(name:String)->UIImage{
        let fileManager = FileManager.default
        let imagePAth = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent(name)
        if fileManager.fileExists(atPath: imagePAth){
            if UIImage(contentsOfFile: imagePAth) != nil{
                return UIImage(contentsOfFile: imagePAth)!
            } else{
                return  imageLiteral(resourceName: "default image you saved")
            }
        }
        else{
            return  imageLiteral(resourceName: "default image you saved")
        }
    } 

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift