从UIImagePicker设置多个按钮图像

时间:2014-09-05 22:52:56

标签: ios uiimagepickercontroller

在我看来,我有三个4个按钮。当您单击按钮时,它会弹出UIPickerView,以便您可以从库中选择一个图像作为配置文件图像。但是,当调用didFinishPickingMediaWithInfo时,它会持续存储同一按钮的不同照片。 单击按钮1,选择图像,从按钮1设置图像 单击按钮2,选择图像,再次为按钮1设置图像,而不是按钮2。 所有4个按钮都是一样的。一直为第一个按钮设置图像。

func promptForCamera() {

    let controller = UIImagePickerController()
    controller.sourceType = UIImagePickerControllerSourceType.Camera
    controller.delegate = self
    self.presentViewController(controller, animated: true, completion: nil)
}

func promptForPhotoRoll() {

    let controller = UIImagePickerController()

    controller.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    controller.delegate = self
    self.presentViewController(controller, animated: true, completion: nil)
}

func promptForSource() {
    let actionSheet = UIActionSheet(title: "Image Source", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: nil, otherButtonTitles: "Camera", "Photo Roll")
    actionSheet.showInView(view)

}

func actionSheet(actionSheet: UIActionSheet!, clickedButtonAtIndex buttonIndex: Int) {
    if buttonIndex != actionSheet.firstOtherButtonIndex{
        if buttonIndex != actionSheet.firstOtherButtonIndex{
            promptForCamera()
        }
        else{
           promptForPhotoRoll()
        }
    }
}

 func imagePickerControllerDidCancel(picker: UIImagePickerController!) {
    dismissViewControllerAnimated(true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]!) {
    let image = info[UIImagePickerControllerOriginalImage] as UIImage

    self.p1Image = image


    self.dismissViewControllerAnimated(true , completion: nil)

    p1ImageButton.setImage(self.p1Image, forState: UIControlState.Normal)


}



@IBAction func p1PhotoTapped(sender: AnyObject) {


    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){

        promptForSource()


    }
    else{

        promptForPhotoRoll()


    }

}

显然,我知道在这个didFinishPickingMedia函数中,我正在为p1ImageButton设置Image。但是,如何更改此功能以便为拾取的相关按钮设置图像?我需要4种不同的didFinishPickingWithMedia功能(甚至可能)吗?每次选择一个按钮时,它都会调用didFinish方法。我需要传递某种参数吗?希望这一切都有意义。

1 个答案:

答案 0 :(得分:1)

将4个ImagePickerControllers保存在变量中并在imagePickerController中进行确定(picker:UIImagePickerController!,didFinishPickingMediaWithInfo info:[NSObject:AnyObject]!)方法。

if (picker == pickerBla)
{

}
else if (picker == ...)
{

}