0

I've made an iPhone app where the user takes a picture. What would i need to put in my code so that when the user takes the photo it saves the photo to the users library?

Below is my code so far

import UIKit import AVFoundation

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

var picker = UIImagePickerController()

@IBOutlet var camera: UIButton!



@IBAction func camera(sender: UIButton) {


        if UIImagePickerController.availableCaptureModesForCameraDevice(.Rear) != nil {
            picker = UIImagePickerController() //make a clean controller
            picker.allowsEditing = false
            picker.sourceType = UIImagePickerControllerSourceType.Camera
            picker.cameraCaptureMode = .Photo
            picker.showsCameraControls = true

            //customView stuff
            let customViewController = CustomOverlayViewController(
                nibName:"CustomOverlayViewController",
                bundle: nil
            )


            let customView:CustomOverlayView = customViewController.view as! CustomOverlayView
            customView.frame = self.picker.view.frame

            customView.cameraLabel.text = "Hello Cute Camera"


            picker.modalPresentationStyle = .FullScreen
            presentViewController(picker,animated: true,completion: {
                    self.picker.cameraOverlayView = customView
                }
            )




        } else { //no camera found -- alert the user.
            let alertVC = UIAlertController(
                title: "No Camera",
                message: "Sorry, this device has no camera",
                preferredStyle: .Alert)
            let okAction = UIAlertAction(
                title: "OK",
                style:.Default,
                handler: nil)
            alertVC.addAction(okAction)
            presentViewController(
                alertVC,
                animated: true,
                completion: nil)
        }


    //MARK: Picker Delegates
    func imagePickerController(
        picker: UIImagePickerController,
        didFinishPickingMediaWithInfo info: [String : AnyObject])
    {
        let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage //2
        UIImageWriteToSavedPhotosAlbum(chosenImage, self,nil, nil)
    }
    //What to do if the image picker cancels.
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        dismissViewControllerAnimated(true,
            completion: nil)
    }




    }






override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

bigredfro
  • 27
  • 6

1 Answers1

0

This is what I do on my application this might help.

func saveImage(image: UIImage, funParam: (Bool) -> ()) ->Bool { //, metadata: NSDictionary) {
        if assetCollection == nil {
            return false                         // if there was an error upstream, skip the save
        }

        //self.validateStatus()

    //TODO: the return false is happening before the photo is save.
        PHPhotoLibrary.sharedPhotoLibrary().performChanges({
            let assetChangeRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(image)
            let assetPlaceHolder = assetChangeRequest.placeholderForCreatedAsset
            let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: self.assetCollection)
            albumChangeRequest!.addAssets([assetPlaceHolder!])                                                      
            }, completionHandler: { (success, error) -> Void in
                if success {
                    funParam(true)
                }
                else {
                    funParam(false)
                }

        })
        return true

}

The parameters are the following:

  1. image is the image that you want to safe.

  2. funParam is a function that will be completion handler if you want to control synchronously the saving of the image. I do this because the code that actually save the image runs asynchronously.

UPDATED message with a sample of the call:

Here is a example how I call this code:

I have a class called PhotoAlbum.swif and I call that function like this:

let validate = self.saveImage(imageView.image!, funParam: getSaveResponse)

getSaveResponse is a method to control what should happen after the image is safe.

JuValencia
  • 212
  • 1
  • 3
  • 17
  • Where would i put this code in my code? – bigredfro Mar 17 '16 at 23:23
  • you can have a button if you want to execute the code which i recommend or you can add it as part of the imagePickerController func (didFinishPickingMediaWithInfo) which you already have. If this answer satisfies your question please mark the question as answered – JuValencia Mar 17 '16 at 23:28