ios - 相机按钮不工作

标签 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)   
    }
}

最佳答案

请检查您是否在设置中为您的应用关闭了相机照片权限。 enter image description here

关于ios - 相机按钮不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32833827/

相关文章:

Swift - 将文本和 int 从 vc1 传递到 vc 2 (uitextfield -> vc2 中的标签)

ios - 在自定义类中呈现 UIDocumentInteractionController 的问题

ios - 免费为用户提供付费应用内购买

ios - 体系结构 x86_64 的 undefined symbol : "_OBJC_CLASS_$_RCTImageLoader"

ios - xcode cocoapods 库未找到错误

ios - 在 xcode 中启动时模拟器崩溃,问题出在 view.controller.init 中

ios - Xcode 6 iPhone模拟器应用支持位置

iOS ScrollView ,在非事件状态上显示灰度图像,在滚动上显示正常图像

ios - iOS 中来自 JSON 的响应不完整

ios - 在 AFNetworking 中设置用户代理