要求用户使用 swift2 从照片库或相机中选择照片

Ask user to choose photo from photo library or camera with swift2

我希望用户从他的照片库或相机中选择一张照片。我找不到任何例子。我想用 UIAlertView 之类的东西提示用户。

我的代码适用于照片库。

@IBAction func selectLeftPhoto(sender: AnyObject) {

        flag = 1

        let myPickerController = UIImagePickerController()
        myPickerController.delegate = self;
        myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary

        self.presentViewController(myPickerController, animated: true, completion: nil)

    }

    @IBAction func selectRightButton(sender: AnyObject) {

        flag = 2

        let myPickerController = UIImagePickerController()
        myPickerController.delegate = self;
        myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary

        self.presentViewController(myPickerController, animated: true, completion: nil)

    }



    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
    {

        let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage

        if flag == 1 {
            leftImage.image = pickedImage
            let imageData = UIImageJPEGRepresentation(pickedImage!, 0.5)
            let base64String = imageData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
            photo1 = base64String

        }else if flag == 2 {
            rightImage.image = pickedImage
            let imageData = UIImageJPEGRepresentation(pickedImage!, 0.5)
            let base64String = imageData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
            photo2 = base64String
        }

        dismissViewControllerAnimated(true, completion: nil)



    }

我的用户界面:

我想在点击 select 照片按钮后提示用户从照片库或相机中选择。

        let alert:UIAlertController=UIAlertController(title: "Choose Image", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
        let cameraAction = UIAlertAction(title: "Camera", style: UIAlertActionStyle.Default)
        {
            UIAlertAction in
            self.openCamera()

        }
        let gallaryAction = UIAlertAction(title: "Gallary", style: UIAlertActionStyle.Default)
        {
            UIAlertAction in
            self.openGallary()
        }
        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel)
        {
            UIAlertAction in

        }
        // Add the actions
        picker?.delegate = self
        alert.addAction(cameraAction)
        alert.addAction(gallaryAction)
        alert.addAction(cancelAction)
        // Present the controller
        if UIDevice.currentDevice().userInterfaceIdiom == .Phone
        {
            self.presentViewController(alert, animated: true, completion: nil)
        }
        else
        {
            popover=UIPopoverController(contentViewController: alert)
            popover!.presentPopoverFromRect(btnClickMe.frame, inView: self.view, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
        }
 let optionMenu = UIAlertController(title: nil, message: "Choose Option for Image", preferredStyle: .ActionSheet)

// 2
let cameraAction = UIAlertAction(title: "Camera", style: .Default, handler: {
  (alert: UIAlertAction!) -> Void in
 flag = 1

    let myPickerController = UIImagePickerController()
    myPickerController.delegate = self;
    myPickerController.sourceType = UIImagePickerControllerSourceType.Camera

    self.presentViewController(myPickerController, animated: true, completion: nil)
})
let photoGalleryAction = UIAlertAction(title: "Photo Li", style: .Default, handler: {
  (alert: UIAlertAction!) -> Void in

 flag = 2

    let myPickerController = UIImagePickerController()
    myPickerController.delegate = self;
    myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary

    self.presentViewController(myPickerController, animated: true, completion: nil)


})

//
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: {
  (alert: UIAlertAction!) -> Void in
  println("Cancelled")
})