在一个视图控制器中使用不同的按钮上传多个图像(iOS、Xcode 9、Swift 4)

Posted

技术标签:

【中文标题】在一个视图控制器中使用不同的按钮上传多个图像(iOS、Xcode 9、Swift 4)【英文标题】:Multiple Image uploads in One View Controller with different Buttons (iOS, Xcode9, Swift4) 【发布时间】:2018-05-17 10:04:04 【问题描述】:

这里有 4 个 UIImageView,每个都有单独的 4 个按钮用于图像上传。当用户单击每个按钮时,我无法修复适当的图像必须上传。意味着当用户单击返回按钮时,必须上传适当的图像。我已经附上了我的快速代码,请帮助解决这个问题

 var imagePicker = UIImagePickerController()
 var imagePicked = 0

@IBOutlet weak var adImage1: UIImageView!
@IBOutlet weak var adImage2: UIImageView!
@IBOutlet weak var adImage3: UIImageView!
@IBOutlet weak var adImage4: UIImageView!

 @IBAction func uploadImage1(_ sender: Any) 
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) 
            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = .photoLibrary;
            imagePicker.allowsEditing = true
            self.present(imagePicker, animated: true, completion: nil)
        
    

    @IBAction func uploadImage2(_ sender: Any) 
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) 
            let imagePicker1 = UIImagePickerController()
            imagePicker1.delegate = self
            imagePicker1.sourceType = .photoLibrary;
            imagePicker1.allowsEditing = true
            self.present(imagePicker1, animated: true, completion: nil)
             
    @IBAction func uploadImage3(_ sender: Any) 
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) 
            let imagePicker2 = UIImagePickerController()
            imagePicker2.delegate = self
            imagePicker2.sourceType = .photoLibrary;
            imagePicker2.allowsEditing = true
            self.present(imagePicker2, animated: true, completion: nil)
          
    @IBAction func uploadImage4(_ sender: Any) 
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) 
            let imagePicker3 = UIImagePickerController()
            imagePicker3.delegate = self
            imagePicker3.sourceType = .photoLibrary;
            imagePicker3.allowsEditing = true
            self.present(imagePicker3, animated: true, completion: nil)
          


     internal func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
     
         let imagePicker = info[UIImagePickerControllerOriginalImage] as? UIImage
        if imagePicked == 1 
       adImage1.image = imagePicker
        
     else if imagePicked == 2
            
                adImage2.image = imagePicker
            
            else if imagePicked == 3

                
                    adImage3.image = imagePicker
                
              else if imagePicked == 4
                    
                        adImage4.image = imagePicker
                                     else
                    
                       // Error message

         self.dismiss(animated: true, completion: nil)
    
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) 
        dismiss(animated: true)
    

【问题讨论】:

【参考方案1】:

更改您的按钮 IBaction 代码以设置基于按钮单击选择的图像,以便您的代码像魅力一样工作

@IBAction func uploadImage1(_ sender: Any) 
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) 
            imagePicked = 1
            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = .photoLibrary;
            imagePicker.allowsEditing = true
            self.present(imagePicker, animated: true, completion: nil)
        
    

    @IBAction func uploadImage2(_ sender: Any) 
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) 
            imagePicked = 2
            let imagePicker1 = UIImagePickerController()
            imagePicker1.delegate = self
            imagePicker1.sourceType = .photoLibrary;
            imagePicker1.allowsEditing = true
            self.present(imagePicker1, animated: true, completion: nil)
        
    
    @IBAction func uploadImage3(_ sender: Any) 
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) 
            imagePicked = 3
            let imagePicker2 = UIImagePickerController()
            imagePicker2.delegate = self
            imagePicker2.sourceType = .photoLibrary;
            imagePicker2.allowsEditing = true
            self.present(imagePicker2, animated: true, completion: nil)
        
    
    @IBAction func uploadImage4(_ sender: Any) 
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) 
            imagePicked = 4
            let imagePicker3 = UIImagePickerController()
            imagePicker3.delegate = self
            imagePicker3.sourceType = .photoLibrary;
            imagePicker3.allowsEditing = true
            self.present(imagePicker3, animated: true, completion: nil)
        
    

【讨论】:

以上是关于在一个视图控制器中使用不同的按钮上传多个图像(iOS、Xcode 9、Swift 4)的主要内容,如果未能解决你的问题,请参考以下文章

如何在多个图像视图中设置多个图像

iOS - 需要为不同的 iPhone 调整按钮和图像的大小

如何在 ios swift 中使用故事板平均划分按钮图像?

分别上传多个图像并使用不同的按钮

在 iOS7 中将多个图像加载到单个视图控制器中的方向/分辨率问题

如何在 Swift iOS 中创建一个通用的弹出视图控制器