Swift图像选择器无法正常工作

时间:2016-07-14 01:22:49

标签: ios swift image uiimagepickercontroller photo

我已经好几次看了这段代码,即使是现在一小时,但我发现错误。 :(

没有错误编译项目,根本没有显示错误,但是我无法选择图像,当我按下UIImage时,没有任何反复发生。

在" signUp View Controller"中使用类似的代码注册一切顺利时,您可以选择(选择)图像,并在Firebase上正确保存图像。

此外,在这个" ProfileTableViewController"中,一切都很好,Firebase上保存的图像被加载并显示在Profile View中,但是无法选择它(按下它)来选择它并进行更改。

其他所有内容都经过测试,更改和保存(用户名甚至是电子邮件),只有图片才能给我带来问题。你能看一下代码并告诉我我做错了吗?

感谢。

import UIKit

class ProfileTableViewController: UITableViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

@IBOutlet weak var profileImage: UIImageView!
@IBOutlet weak var username: UITextField!
@IBOutlet weak var email: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    self.title = "Edit Profile"
    let tap = UITapGestureRecognizer(target: self, action: #selector(ProfileTableViewController.selectPhoto(_:)))
    tap.numberOfTapsRequired = 1
    profileImage.addGestureRecognizer(tap)
    profileImage.layer.cornerRadius = profileImage.frame.size.height / 2
    profileImage.clipsToBounds = true

    if let user = DataService.dataService.currentUser {
        username.text = user.displayName
        email.text = user.email
        if user.photoURL != nil {
            if let data = NSData(contentsOfURL: user.photoURL!) {
                self.profileImage!.image = UIImage.init(data: data)
        }
    }
} else {
        // No user is signed in

    }
}
func selectPhoto(tap: UITapGestureRecognizer) {
    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.allowsEditing = true
    if UIImagePickerController.isSourceTypeAvailable(.Camera) {
        imagePicker.sourceType = .Camera
    } else {
        imagePicker.sourceType = .PhotoLibrary
    }
    self.presentViewController(imagePicker, animated: true, completion: nil)

}

// UIImagePicker Delegate

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
    profileImage.image = image
    dismissViewControllerAnimated(true, completion: nil)
}    

2 个答案:

答案 0 :(得分:0)

您需要在UIImageView上启用用户交互,在添加手势识别器后尝试此操作:

profileImage.userInteractionEnabled = true

答案 1 :(得分:0)

我是通过其他方式通过检查 User Interaction Enabled下的" Interaction"选项,见下图以供参考

enter image description here

希望它可以帮助任何人