Xcode - 用户选择后,图像不会出现在UIImageView中

时间:2017-06-10 17:02:29

标签: ios swift uiimageview

我正在尝试创建一个可以“撰写”假推文的应用,但我的代码似乎没有将用户选择的图像添加到我为Twitter个人资料图片设置的UIImageView中。

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate{    
    @IBOutlet weak var twitterName: UILabel!    
    @IBOutlet weak var twitterUsername: UILabel!    
    @IBOutlet weak var twitterProfilePic: UIImageView!    
    @IBOutlet weak var twitterTweet: UILabel!    
    @IBOutlet weak var inputTwitterName: UITextField!    
    @IBOutlet weak var inputTwitterUsername: UITextField!    
    @IBOutlet weak var inputTweet: UITextField!    
    @IBAction func chooseTwitterImage(_ sender: Any) {        

    let profilePic = UIImagePickerController()
    profilePic.delegate = self        
    profilePic.sourceType = UIImagePickerControllerSourceType.photoLibrary        
    profilePic.allowsEditing = false        
    self.present(profilePic, animated: true)
}

@IBAction func makeTweetButton(_ sender: Any) {        
    twitterName.text = (inputTwitterName.text)!        
    twitterUsername.text = (inputTwitterUsername.text)!        
    twitterTweet.text = (inputTweet.text)!        

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let profilePic = info[UIImagePickerControllerOriginalImage] as? UIImage
        {
            twitterProfilePic.image = profilePic                
        }                
        else
        {                
            print("ERROR PICKING IMAGE")                
        }
    }        
}

它似乎对UIImageView ...帮助没有任何影响。

1 个答案:

答案 0 :(得分:1)

didFinishPickingMediaWithInfo嵌套方法移动为ViewController类可以解决问题。