【问题标题】:Display two images in didFinishPickingImage - iOS Swift 2.2 [duplicate]在 didFinishPickingImage 中显示两个图像 - iOS Swift 2.2 [重复]
【发布时间】:2017-01-03 07:54:01
【问题描述】:

我有两个UIImageViews,每个都有两个按钮。一个按钮拍照,另一个按钮从图库中选择一张照片。两个按钮都能正常工作。但是当我通过第一张表中的按钮选择图像时,它同时显示在UIImageViews 中。我的问题是我们如何才能仅在相应的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)
}

【问题讨论】:

  • 使用全局引用你想改变的imageView;在操作表函数中,将该变量设置为所需的 imageView。
  • 感谢您的快速回复。问题解决了。我添加了 var imageInput: UIImageView!。在 presentViewController 之前的操作表中,我设置了“self.imageInput = self.importedImage”。在 didFinishPickingImage 方法中,我改为“input.image = image”

标签: ios image uiimageview swift2 uiimagepickercontroller


【解决方案1】:

发生这种情况是因为您在ImageViewdidFinishPickingImage 中都设置了图像,为了解决这个问题需要保持一些状态,例如单击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中的这个布尔值。

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
    }
}

【讨论】:

    猜你喜欢
    • 2019-10-08
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    • 2016-03-24
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多