iOSアプリは、サンドボックスのみにアクセスできるため、ドキュメント、温度、ライブラリのフォルダにのみ書き込むことができます。 See the documentation。
ここでは、ドキュメントディレクトリにイメージを書き込んで読み込む方法を示します。
let documentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let path = "\(documentsDirectory)/image.png"
let image = UIImage(contentsOfFile: path)
そして今、保存する方法:
let image = ...
let documentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let path = "\(documentsDirectory)/image.png"
if let data = UIImagePNGRepresentation(image!) {
try? data.write(to: URL(fileURLWithPath: path))
}