一个控制器中有多个图像选择器 [英] Multiple Image Pickers in one Controller

查看:40
本文介绍了一个控制器中有多个图像选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的应用程序启动流程.我想允许用户选择一个化身,在图像视图中显示该化身,然后选择背景照片并在另一个图像视图中显示.目前,我已对所有内容进行了编码,但是由于某种原因,当我显示选择器控制器时,它默认为化身的控制器.我希望头像按钮仅控制头像按钮背景图像,而背景按钮仅控制标题图像背景图像.这是我的代码:

I'm working on an onboarding flow for my app. I'm wanting to allow users to pick an avatar, display that avatar in an image view, and then select a background photo and display that in another image view. Currently, I have everything coded up, but for some reason when I present the picker controller, it defaults to controller for the avatar. I want the avatar button to only control the avatar button background image and the background button to only control the header image background image. Here's my code:

 @IBAction func getBackground(sender: AnyObject) {
    let backgroundPickerController = UIImagePickerController()
    backgroundPickerController.delegate = self
    backgroundPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    backgroundPickerController.allowsEditing = true
    self.presentViewController(backgroundPickerController, animated: true, completion: nil)
}

@IBAction func selectAvatar(sender: AnyObject) {
    let imagePickerController = UIImagePickerController()
    imagePickerController.delegate = self
    imagePickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    imagePickerController.allowsEditing = true
    self.presentViewController(imagePickerController, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    // set image for button
    let image = info[UIImagePickerControllerOriginalImage] as? UIImage
    self.addAvatar.setImage(image, forState: .Normal)

    // Dismiss controller
    self.dismissViewControllerAnimated(false, completion: nil)
}

func backgroundPickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
  // set image for button
    let image = info[UIImagePickerControllerOriginalImage] as? UIImage
    self.headerImage.image = image
     self.dismissViewControllerAnimated(false, completion: nil)  
}

推荐答案

委托函数 imagePickerController(picker:UIImagePickerController,didFinishPickingImage image:UIImage,editingInfo:[String:AnyObject]?)也返回 UIImagePickerController 完成选择图像.

The delegate function imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) also returns the UIImagePickerController that finished picking an image.

因此,您可以使用 if 语句检查完成的选择器是pickerOne还是pickerTwo.然后,根据结果执行不同的行为.

So you can use an if statement to check if the picker that finished is pickerOne or pickerTwo. Then you implement different behaviour according to the result of that.

也许可以在选择器完成清理工作后将其设置为 nil .

Maybe set the pickers to nil after they have finished to clean up some memory.

class multiPickerVC : UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {


    var pickerOne : UIImagePickerController?
    var pickerTwo : UIImagePickerController?


    override func viewDidLoad() {
        //
    }


    @IBAction func getBackground(sender: AnyObject) {
        pickerTwo = UIImagePickerController()
        pickerTwo!.delegate = self
        pickerTwo!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        pickerTwo!.allowsEditing = true
        self.presentViewController(pickerTwo!, animated: true, completion: nil)
    }

    @IBAction func selectAvatar(sender: AnyObject) {
        pickerOne = UIImagePickerController()
        pickerOne!.delegate = self
        pickerOne!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        pickerOne!.allowsEditing = true
        self.presentViewController(pickerOne!, animated: true, completion: nil)
    }


    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {

        if picker == pickerOne {

            // set image for button
            let image = info[UIImagePickerControllerOriginalImage] as? UIImage
            self.addAvatar.setImage(image, forState: .Normal)

        } else if picker == pickerTwo {

            // set image for button
            let image = info[UIImagePickerControllerOriginalImage] as? UIImage

            self.headerImage.image = image
        }

        self.dismissViewControllerAnimated(true, completion: nil)

    }
}

这篇关于一个控制器中有多个图像选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆