UIImagePicker 未设置 Swift UIButton 背景图像

标签 swift uibutton uiimagepickercontroller

我可以从 PhotoLibrary 中选择一个图像,但我的 UIButton 的背景图像没有改变。 didFinishPickingImage 和 imagePickerControllerDidCancel 中的 println 没有显示在控制台中,所以我认为没有调用这些函数。

class AddTeamTableViewController: UITableViewController, UITextFieldDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIAlertViewDelegate {
var picker:UIImagePickerController? = UIImagePickerController()
@IBOutlet var teamNumber: UITextField!
@IBOutlet var schoolName: UITextField!
@IBOutlet var teamImage: UIButton!
@IBAction func cancelButton(sender: AnyObject) {
    dismissViewControllerAnimated(true, completion: nil)
}
@IBAction func addTeam(sender: AnyObject) {
    var newTeam = Team()
    var onlineTeam = PFObject(className: "Team")
    // add new team to the list
    newTeam.name = teamNumber.text
    newTeam.schoolName = schoolName.text
    teamList.append(newTeam)
    // add online
    onlineTeam["name"] = newTeam.name
    onlineTeam["fromUser"] = PFUser.currentUser()
    onlineTeam["schoolName"] = newTeam.schoolName
    onlineTeam.save()
    //close the view
    dismissViewControllerAnimated(true, completion: nil)
}
override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 1
}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    self.view.endEditing(true)
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true;
}
@IBAction func pickImage(sender: AnyObject) {
    var image = UIImagePickerController()
    image.delegate = self
    var alert:UIAlertController = UIAlertController(title: "Choose Image", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
    var cameraAction = UIAlertAction(title: "Camera", style: UIAlertActionStyle.Default) {
        UIAlertAction in
        self.openCamera()
    }
    var galleryAction = UIAlertAction(title: "Gallery", style: UIAlertActionStyle.Default) {
        UIAlertAction in
        self.openGallery()
    }       
    var cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) {
        UIAlertAction in
    }
    alert.addAction(cameraAction)
    alert.addAction(galleryAction)
    alert.addAction(cancelAction)
    self.presentViewController(alert, animated: true, completion: nil)
    /*
    alert.delegate = self
    alert.message = "Choose Image Source"
    alert.addButtonWithTitle("Camera")
    alert.addButtonWithTitle("Photo Library")
    alert.show()
    */
    //image.sourceType = UIImagePickerControllerSourceType.Camera
    //image.allowsEditing = false
    //self.presentViewController(image, animated: true, completion: nil)
}
func openCamera() {
    if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)) {
        picker!.sourceType = UIImagePickerControllerSourceType.Camera
        self.presentViewController(picker!, animated: true, completion: nil)
    } else {
        openGallery()
    }
}
func openGallery() {
    picker!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    self.presentViewController(picker!, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
    println("Image selected")
    self.dismissViewControllerAnimated(true, completion: nil)
    teamImage.setBackgroundImage(image, forState: UIControlState.Normal)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController!) {
    println("picker cancel")
}
}

最佳答案

按钮似乎没有初始化

let teamImage: UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 380, height: 300))
let imageTest = UIImage(named: "Hypnotoad")

teamImage.setTitle("HypnotoadTitle", forState: .Normal)
teamImage.setBackgroundImage(imageTest, forState: .Normal)

在 Playground 中对此进行了测试并且工作正常。

Playground test of UIButton with backgroundImage

关于UIImagePicker 未设置 Swift UIButton 背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27337598/

相关文章:

Swift L-系统藻类

xcode - 从 WebView Xcode 共享 PDF 文件

swift - uinavigationcontroller 的异常行为点击 UIButton 抛出异常

iphone - UIImagePicker 和 AVCaptureSession 同时访问相机

ios - UIImagePickerController 委托(delegate)未调用 Swift 3

generics - 在 Swift 中获取泛型类型的名称(字符串)

ios - 如何在 iOS 中查找 Couchbase Mobile DB 文件?

ios - 按钮不会出现在表格单元格中

swift - 将参数附加到 Swift 中的 button.addTarget 操作

iphone - 使用默认相机拍照