UIImageからNSDataに変換する方法です。
変換の形式ですが、JPEGとPNGを選択できます。
それぞれ以下のメソッドを使います。
UIImagePNGRepresentation UIImageJPEGRepresentation
実際に変換を試してみます。
最初にプロジェクトに画像を追加します。
それぞれ実際に書きだしてみます。
let documentsPath = NSSearchPathForDirectoriesInDomains( .CachesDirectory, .UserDomainMask, true).first ?? "" _ = try? UIImagePNGRepresentation(UIImage(named: "sample")!)?.writeToFile( documentsPath + "/sample.tes1", options: .DataWritingFileProtectionNone) _ = try? UIImageJPEGRepresentation(UIImage(named: "sample")!, 0.7)?.writeToFile( documentsPath + "/sample.tes2", options: .DataWritingFileProtectionNone)
確認したところ、それぞれのフォーマットで書き出す事ができていました。