在didFinishPickingImage中显示两个图像 - iOS Swift 2.2

时间:2016-08-26 15:26:08

标签: ios image uiimageview swift2 uiimagepickercontroller

我有两个UIImageView个,每个都有两个按钮。一个按钮拍照,另一个按钮选择图库中的照片。两个按钮都正常工作。但是,当我通过第一张纸上的按钮选择图像时,它会显示在UIImageView中。我的问题是我们如何才能在相应的ImageView中显示图像?

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [NSObject : AnyObject]?) {
    print("Image Selected")
    self.dismissViewControllerAnimated(true, completion: nil)

    importedImage.image = image
    secondPhoto.image = image
}

// MARK: - Action Sheet


@IBAction func showActionSheet1(sender: AnyObject) {
    let image = UIImagePickerController()
    image.delegate = self
    let actionSheet = UIAlertController(title: "Action Sheet", message: "Choose Option", preferredStyle: .ActionSheet)
    let libButton = UIAlertAction(title: "Select from photo library", style: .Default, handler: { (libButton) -> Void in
        image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        image.allowsEditing = false

        self.presentViewController(image, animated: true, completion: nil)

    })
    let cameraButton = UIAlertAction(title: "Take picture", style: .Default, handler: { (cameraButton) -> Void in

        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){

        image.sourceType = UIImagePickerControllerSourceType.Camera
        image.allowsEditing = false
        self.presentViewController(image, animated: true, completion: nil)
        }
    })
    let cancelButton = UIAlertAction(title: "Cancel", style: .Cancel, handler: {(cancelButton) -> Void in
        print("Cancel selected")
    })
    actionSheet.addAction(libButton)
    actionSheet.addAction(cameraButton)
    actionSheet.addAction(cancelButton)


    self.presentViewController(actionSheet, animated: true, completion: nil)

}

@IBAction func showActionSheet2(sender: AnyObject) {

    let image = UIImagePickerController()
    image.delegate = self

    let actionSheet = UIAlertController(title: "Action Sheet", message: "Choose Option", preferredStyle: .ActionSheet)
    let libButton = UIAlertAction(title: "Select from photo library", style: .Default, handler: { (libButton) -> Void in
        image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        image.allowsEditing = false
        self.presentViewController(image, animated: true, completion: nil)

    })
    let cameraButton = UIAlertAction(title: "Take picture", style: .Default, handler: { (cameraButton) -> Void in

        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){

            image.sourceType = UIImagePickerControllerSourceType.Camera
            image.allowsEditing = false
            self.presentViewController(image, animated: true, completion: nil)
        }
    })
    let cancelButton = UIAlertAction(title: "Cancel", style: .Cancel, handler: {(cancelButton) -> Void in
        print("Cancel selected")
    })
    actionSheet.addAction(libButton)
    actionSheet.addAction(cameraButton)
    actionSheet.addAction(cancelButton)


    self.presentViewController(actionSheet, animated: true, completion: nil)
}

1 个答案:

答案 0 :(得分:0)

这种情况正在发生,因为您要在ImageView didFinishPickingImage内设置图片,以解决问题需要保持某些状态,例如点击Button设置Image为此,你可以创建一个Bool实例,并在Button的操作中分配它的值。

var isFromFirst: Bool = false

@IBAction func showActionSheet1(sender: AnyObject) {

    self.isFromFirst = true
    ...//your other code

}

@IBAction func showActionSheet2(sender: AnyObject) {

    self.isFromFirst = false
    ...//your other code

} 

之后检查didFinishPickingImage内的这个bool值,就像这样。

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [NSObject : AnyObject]?) {
    print("Image Selected")
    self.dismissViewControllerAnimated(true, completion: nil)
    if (self.isFromFirst) {
        importedImage.image = image
    }
    else {
        secondPhoto.image = image
    }
}