由于" ViewController"的价值类型,无法访问相册和相机没有会员" showAlbum"

时间:2016-11-04 15:22:33

标签: ios swift3 xcode8

Screenshot

我试图访问相册和相机,但我不能,因为我无法调用showCamera和showAlbum函数,是否有任何人对这个问题有所了解?

  

showAlbum和ShowCamera无法调用

导入UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var imageView: UIImageView!
    var result : UIImage?

    let cameraPicker = UIImagePickerController()
    var pickedImage = UIImage()

    override func viewDidLoad() {
        super.viewDidLoad()


        cameraPicker.delegate = self


    }


    //For Catch Button   


    @IBAction func CatchButton(sender: AnyObject) {


        let actionSheet = UIAlertController(title: "Catch", message: nil, preferredStyle:.actionSheet)

        actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler:{
            action in
            self.showCamera()
        }))


        actionSheet.addAction(UIAlertAction(title: "Album", style: .default, handler: {
            action in
            self.showAlbum()
        }))


        actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil ))


        self.present(actionSheet, animated: true, completion: nil )


        func showCamera(){
            cameraPicker.sourceType = .camera

            present(cameraPicker, animated: true, completion: nil)

        }

        func showAlbum(){

            cameraPicker.sourceType = .savedPhotosAlbum

            present(cameraPicker, animated: true, completion: nil)

        }

        func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo: [String : AnyObject]){
            dismiss(animated: true, completion: nil)
            pickedImage = didFinishPickingMediaWithInfo [UIImagePickerControllerOriginalImage] as! UIImage
            imageView.image = pickedImage

        }

        func imagePickerControllerDidCancel(picker: UIImagePickerController){

            dismiss(animated: true, completion: nil)
        }

    }    

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


}

2 个答案:

答案 0 :(得分:0)

问题是因为您错误地嵌套了CatchButton函数中的所有这些函数。使它们成为所有正常的实例方法。

答案 1 :(得分:0)

CatchButton

中取出您的功能
 class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet weak var imageView: UIImageView!
var result : UIImage?

let cameraPicker = UIImagePickerController()
var pickedImage = UIImage()

override func viewDidLoad() {
    super.viewDidLoad()


    cameraPicker.delegate = self


}


//For Catch Button   


@IBAction func CatchButton(sender: AnyObject) {


    let actionSheet = UIAlertController(title: "Catch", message: nil, preferredStyle:.actionSheet)

    actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler:{
        action in
        self.showCamera()
    }))


    actionSheet.addAction(UIAlertAction(title: "Album", style: .default, handler: {
        action in
        self.showAlbum()
    }))


    actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil ))


    self.present(actionSheet, animated: true, completion: nil )



}    
func showCamera(){
        cameraPicker.sourceType = .camera

        present(cameraPicker, animated: true, completion: nil)

    }

    func showAlbum(){

        cameraPicker.sourceType = .savedPhotosAlbum

        present(cameraPicker, animated: true, completion: nil)

    }

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo: [String : AnyObject]){
        dismiss(animated: true, completion: nil)
        pickedImage = didFinishPickingMediaWithInfo [UIImagePickerControllerOriginalImage] as! UIImage
        imageView.image = pickedImage

    }

    func imagePickerControllerDidCancel(picker: UIImagePickerController){

        dismiss(animated: true, completion: nil)
    }


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

}