Save image to document directory iOS swift

 Solution:

// MARK: - Save Image to Document Directory

func saveImageDocumentDirectory(savingImage:UIImage)->String{

    let fileManager = FileManager.default

    let diceRoll = Int(arc4random_uniform(99999999))

    let name = "\(diceRoll).png"

    let urls = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: app.appGroup)

    let paths = urls?.appendingPathComponent(name)

    let imageData = savingImage.jpegData(compressionQuality: 0.5)

    fileManager.createFile(atPath: paths?.path ?? "", contents: imageData, attributes: nil)

    return name

}


The above function will save the image and return the image name.

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