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

store cgpoint in userdefaults iOS swift

libc++abi: terminating due to uncaught exception of type NSException iOS swift