UIIMagePickerController与Swift 3无法正常工作

时间:2016-09-21 14:59:14

标签: uiimagepickercontroller swift3

我对此感到疯狂。

迁移简单的UIIMagePicker实现时,我可以正常工作,没有问题,没有警告。 在模拟器和设备上运行时,PickerController不考虑新图像并且不会应用于UImageView。

这是我的代码:

@IBOutlet weak var profilePictureImageView: UIImageView!
let picker = UIImagePickerController()

override func viewDidLoad() {
    super.viewDidLoad()   
    picker.delegate = self
    }

//Opening Camera to take a new picture
@IBAction func takeNewPictureButtonDidTouch(_ sender: AnyObject) {
    if UIImagePickerController.isSourceTypeAvailable(.camera) {
        picker.allowsEditing = false
        picker.sourceType = UIImagePickerControllerSourceType.camera
        picker.cameraCaptureMode = .photo
        picker.modalPresentationStyle = .fullScreen
        present(picker,animated: true,completion: nil)
    } else {
        noCamera()
    }
}

//Opening the library to select an existing picture
@IBAction func openPicturesLibraryButtonDidTouch(_ sender: AnyObject) {
    picker.allowsEditing = false
    picker.sourceType = .photoLibrary
    picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
    picker.modalPresentationStyle = .popover
    present(picker, animated: true, completion: nil)
    }

//MARK: - Delegates
func imagePickerController(_ picker: UIImagePickerController,
                           didFinishPickingMediaWithInfo info: [String : AnyObject])
{
    var  chosenImage = UIImage()
    chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage //2
    profilePictureImageView.contentMode = .scaleAspectFill //3
    profilePictureImageView.image = chosenImage //4
    dismiss(animated:true, completion: nil) //5
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    dismiss(animated: true, completion: nil)
}

func noCamera(){
    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)
    present(
        alertVC,
        animated: true,
        completion: nil)
}   
}

1 个答案:

答案 0 :(得分:0)

在viewdidload中将委托设置为self