简体   繁体   English

我想使用两个按钮选择图像,并想在两个不同的图像视图中快速显示这些图像。我该怎么做?

[英]I want to select image using two buttons and want to display those images on two different image view in swift..how could I do this.?

I want to select image using two buttons and want to display those images on two different image view in swift 我想使用两个按钮选择图像,并想在两个不同的图像视图中快速显示这些图像

 func pickimage(){
            if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){

                let controller = UIImagePickerController()
                controller.delegate = self
                controller.sourceType = .photoLibrary
                controller.allowsEditing = false
                present(controller, animated: true, completion: nil)
            }

        }

Image Picker Delegates 图像选择器代表

extension AdnewDOC : UIImagePickerControllerDelegate,UINavigationControllerDelegate {

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true, completion: nil)
    }


    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {


            let image = info[UIImagePickerControllerOriginalImage] as! UIImage
            frontview.image = image
            frontview.contentMode = .scaleAspectFill

         dismiss(animated: true, completion: nil)


    }


}

You can use objective-c associated types objc_setAssociatedObject and objc_getAssociatedObject like this: . 您可以像这样使用与objective-c相关的类型objc_setAssociatedObjectobjc_getAssociatedObject

I have updated your method, Please pass your image view obejct (which image view you want to show image) from button action method. 我已经更新了您的方法,请通过按钮操作方法传递您的图像视图对象(您要显示图像的图像视图)。 Like a demo action method for buttons: 就像按钮的演示动作方法一样:

import ObjectiveC

var myKey = "myObjectKey"


@IBAction func firstButtonAction(_ sender: Any) {
            pickimage(imageView: firstImageView)
    }

@IBAction func secondButtonAction(_ sender: Any) {
            pickimage(imageView: secondImageView)
    }


func pickimage(imageView: UIImageView){
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
            let controller = UIImagePickerController()
            objc_setAssociatedObject(controller, &myKey, imageView, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            controller.delegate = self
            controller.sourceType = .photoLibrary
            controller.allowsEditing = false
            present(controller, animated: true, completion: nil)
        }
    }

And get the image object in delegate method like this: 并在委托方法中获取图像对象,如下所示:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

        let imageView = objc_getAssociatedObject(picker, &myKey) as! UIImageView

        let image = info[UIImagePickerControllerOriginalImage] as! UIImage
        imageView.image = image
        imageView.contentMode = .scaleAspectFill
        dismiss(animated: true, completion: nil)
}

For more understanding how to work objc_setAssociatedObject and objc_getAssociatedObject please check below links: 为了进一步了解objc_setAssociatedObjectobjc_getAssociatedObject工作objc_setAssociatedObject ,请检查以下链接:

http://blog.corywiles.com/objective-c-runtime-with-swfit http://blog.corywiles.com/objective-c-runtime-with-swfit

http://en.swifter.tips/associated-object/ http://en.swifter.tips/associated-object/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想使用2个按钮在同一ViewController上显示两个图像 - I want to display two images at the same ViewController using 2 buttons 我想在iOS中使用for循环基于多行两列的图像数量对齐图像视图 - I want to align image view based on number of images multiple rows and two columns using for loop in ios 如何使用Swift显示图像的选择像素? - How do I Display Select Pixels of an Image Using Swift? 如何计算两个图像的相似百分比,如下所示Image我想在我的项目中实现它 - How to calculate the similarity percentage of two Images , Same as like below Image I want to implement it in my Project 如何在Swift中制作包含两个图像的合成图像? - How can I make a composite image consisting of two images in Swift? 您可以在一个视图控制器上使用两个不同的相机按钮来显示两个不同的图像吗? (快速3) - Can you have two different camera buttons for two different image views on one view controller? (Swift 3) 迅速-如何在自定义视图单元格中显示两个不同的图像 - swift - how to display two different images in a custom made view cell 我想使用swift添加标签或文本,如下图所示 - I want to add label or text as shown in image below using swift 我想使用 alamofire swift 5 将图像上传到多部分表单数据 - I want to upload image to multipart form data using alamofire swift 5 如何在图片视图中添加背景图片以及要在背景图片ios上显示的图片? - how can I add a background image in the image view and the image I want it to show over the background image ios?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM