Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to take a picture and save it to the app locally with swift

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(sender: UIButton) {
let picker = UIImagePickerController()
    picker.delegate = self
    picker.sourceType = .Camera
    picker.allowsEditing = true
    self.presentViewController(picker, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
    picker.dismissViewControllerAnimated(true, completion: nil)

    //Save image
    let img = UIImage()
    let data = UIImagePNGRepresentation(img)
    NSUserDefaults.standardUserDefaults().setObject(data, forKey: "myImageKey")
    NSUserDefaults.standardUserDefaults().synchronize()
    NSLog("Image stored?")
    viewDidLoad()


}

From what I have above, my iPhone opens up its camera and I can take a picture. When I hit capture and the "use this image" option I don't think it the pictures are actually being stored because I haven't been able to recall the pictures.

So I'm asking for help in how to locally store a picture taken from the UIImagePickerController, possibly in the documents folder, how to recall it, and how to do it without NSUserDEfaults. Thanks.

like image 756
acday Avatar asked Jun 22 '16 22:06

acday


3 Answers

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!

like image 131
GJZ Avatar answered Nov 06 '22 19:11

GJZ


Use this Method: with your image argument as below:

UIImageWriteToSavedPhotosAlbum(imagePicked, nil, nil, nil)
like image 32
Sour LeangChhean Avatar answered Nov 06 '22 18:11

Sour LeangChhean


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.

like image 36
Asdrubal Avatar answered Nov 06 '22 18:11

Asdrubal