I have seen this code in other post, for save pictures:
// Create path.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSU
Use this category to save any image on document directory
import Foundation
import UIKit
import ImageIO
import MobileCoreServices
extension UIImage {
func writeAtPath(path:String) -> Bool {
let result = CGImageWriteToFile(self.CGImage!, filePath: path)
return result
}
private func CGImageWriteToFile(image:CGImageRef, filePath:String) -> Bool {
let imageURL:CFURLRef = NSURL(fileURLWithPath: filePath)
var destination:CGImageDestinationRef? = nil
let ext = (filePath as NSString).pathExtension.uppercaseString
if ext == "JPG" || ext == "JPEG" {
destination = CGImageDestinationCreateWithURL(imageURL, kUTTypeJPEG, 1, nil)
} else if ext == "PNG" || ext == "PNGF" {
destination = CGImageDestinationCreateWithURL(imageURL, kUTTypePNG, 1, nil)
} else if ext == "TIFF" || ext == "TIF" {
destination = CGImageDestinationCreateWithURL(imageURL, kUTTypeTIFF, 1, nil)
} else if ext == "GIFF" || ext == "GIF" {
destination = CGImageDestinationCreateWithURL(imageURL, kUTTypeGIF, 1, nil)
} else if ext == "PICT" || ext == "PIC" || ext == "PCT" || ext == "X-PICT" || ext == "X-MACPICT" {
destination = CGImageDestinationCreateWithURL(imageURL, kUTTypePICT, 1, nil)
} else if ext == "JP2" {
destination = CGImageDestinationCreateWithURL(imageURL, kUTTypeJPEG2000, 1, nil)
} else if ext == "QTIF" || ext == "QIF" {
destination = CGImageDestinationCreateWithURL(imageURL, kUTTypeQuickTimeImage, 1, nil)
} else if ext == "ICNS" {
destination = CGImageDestinationCreateWithURL(imageURL, kUTTypeAppleICNS, 1, nil)
} else if ext == "BMPF" || ext == "BMP" {
destination = CGImageDestinationCreateWithURL(imageURL, kUTTypeBMP, 1, nil)
} else if ext == "ICO" {
destination = CGImageDestinationCreateWithURL(imageURL, kUTTypeICO, 1, nil)
} else {
fatalError("Did not find any matching path extension to store the image")
}
if (destination == nil) {
fatalError("Did not find any matching path extension to store the image")
return false
} else {
CGImageDestinationAddImage(destination!, image, nil)
if CGImageDestinationFinalize(destination!) {
return false
}
return true
}
}
}
// This is how to use this category in your application.
func testImageWrite() -> Bool {
let img = UIImage(named: "test")
var path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0]
path = (path as NSString).stringByAppendingPathComponent("Test.png")
let result = img?.writeAtPath(path)
return result!
}