相机按钮不起作用

时间:2015-09-29 00:29:49

标签: ios xcode swift

我有一个应用程序,允许用户自拍并将其上传到Facebook,但我创建的selfieTapped按钮不起作用。它会出现黑屏。它不允许用户从他们的库中挑选照片,也不允许用户拍照。

import UIKit
import Social

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

    @IBOutlet weak var myImageView: UIImageView!

    @IBAction func selfieTapped(sender: AnyObject) {

        //this method will be called when the user has taken a photo or has selected a photo from the photo library
        func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) {

            //takes the image parameter and displays it inside myImageView
            myImageView.image = image

            //hides the imagePicker and animates the transition
            self.dismissViewControllerAnimated(true, completion: nil)

        }

        //creates a new UIImagePickerController and sets it to the imagePicker variable
        let imagePicker = UIImagePickerController()

        //self the imagePicker's delegate property to the current view controller
        imagePicker.delegate = self

        //sets the imagePicker's sourceType property to .Camera
        if UIImagePickerController.isSourceTypeAvailable(.Camera) {

            //checking if a front camera is available
            imagePicker.sourceType = .Camera

            //if so, set it to the front camera
            if (UIImagePickerController.isCameraDeviceAvailable(.Front)) {

                //setting it to the front camera
                imagePicker.cameraDevice = .Front

            } else {

                //setting it to the rear camera if the front is not available
                imagePicker.cameraDevice = .Rear
            }

        } else {

            //if the camera is not available, the photo library will be shown
            imagePicker.sourceType = .PhotoLibrary
        }

        //presents the imagePicker modally, sliding it up from the bottom of the screen and it animates the transition
        self.presentViewController(imagePicker, animated: true, completion: nil)

    }

    @IBAction func shareTapped(sender: AnyObject) {

        //sets the serviceType property to Facebook
        let facebookSocial = SLComposeViewController(forServiceType: SLServiceTypeFacebook)

        //myImageView image is added to the Facebook post
        facebookSocial.addImage(myImageView.image)

        //it is displayed and uses animation
        self.presentViewController(facebookSocial, animated: true, completion: nil)   
    }
}

2 个答案:

答案 0 :(得分:1)

请检查您是否已关闭Camera& Photos中对您的应用的Setting权限。 enter image description here

答案 1 :(得分:1)

这可能不是造成麻烦的原因,但以后肯定会有麻烦:你把一个功能放在另一个功能中:

@IBAction func selfieTapped(sender: AnyObject) {
    func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) {

不要那样做。他们都需要处于同一级别,作为你班级的方法。否则,永远不会调用didFinishPickingImage