I\'m working on an app where in the current stage when a user takes a picture the picture will be stored locally within the app.
@IBAction func CameraAction(se
Use this Method: with your image argument as below:
UIImageWriteToSavedPhotosAlbum(imagePicked, nil, nil, nil)
This is how you would store an image in NSUserDefaults:
As I mentioned in my comment, this is not recommended and, if used for more than one image, may result in serious performance loss and even crashes.
let image = UIImage(named: "myImage")
let pngImage = UIImagePNGRepresentation(image)
NSUserDefaults.standardUserDefaults().setObject(pngImage, forKey: "image")
and to retrieve the image:
var retrievedImage = NSUserDefaults.standardUserDefaults().objectForKey("image") as! AnyObject
Then, to display the image:
imageView.image = UIImage(data: retrievedImage as! NSData)
This is how to store an image to a file (much better):
func saveImage (image: UIImage, path: String ) -> Bool{
let pngImageData = UIImagePNGRepresentation(image)
//let jpgImageData = UIImageJPEGRepresentation(image, 1.0) // if you want to save as JPEG
let result = pngImageData!.writeToFile(path, atomically: true)
return result
}
You would call it like this:
saveImage(image, path: imagePath)
Then, to retrieve the image, use this function:
func loadImageFromPath(path: String) -> UIImage? {
let image = UIImage(contentsOfFile: path)
if image == nil {
print("missing image at: \(path)")
}
print("Loading image from path: \(path)") // this is just for you to see the path in case you want to go to the directory, using Finder.
return image
}
You can call it like this:
image = loadImageFromPath(imagePath)
imageView.image = image
For more details, take a look at http://helpmecodeswift.com/image-manipulation/saving-loading-images
Also, you don't actually use the image that gets selected in your example, as Frederik pointed out.
You save an empty variable 'img' that has not been assigned to. You need to save 'image', the variable you get back after the picker has finished.
Here is how you get the image:
This function returns the variable 'image'
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
picker.dismissViewControllerAnimated(true, completion: nil)
You can then go ahead and just use it e.g.
imageView.image = image
or maybe
saveImage(image, path: imagePath)
Good luck!
let fileDirectory : NSURL = {
return try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory , inDomain: .UserDomainMask , appropriateForURL: nil, create: true)
}()
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
picker.dismissViewControllerAnimated(true, completion: nil)
//save to album
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
//or to documents
//If you want to use png you have to change it later in saveImageToDocumentMethod in the guard statement
saveImageToDocuments(image, filenameWithExtension: "test.jpg")
}
func saveImageToDocuments(image: UIImage, fileNameWithExtension: String){
let imagePath = fileDirectory.URLByAppendingPathComponent("\(fileNameWithExtension)")
guard let imageData = UIImageJPEGRepresentation(image, 1) else {
// handle failed conversion
print("jpg error")
return
}
imageData.writeToFile((imagePath.path)!, atomically: true)
}
When you want to use that image all you need to do is use the imagePath again and convert it into a UIImage.