在 Swift 中使用 imagePickerController 在同一个视图控制器中选择两个不同的图像

Picking two different images in the same view controller using imagePickerController in Swift(在 Swift 中使用 imagePickerController 在同一个视图控制器中选择两个不同的图像)
本文介绍了在 Swift 中使用 imagePickerController 在同一个视图控制器中选择两个不同的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I am making an app in which there are two UIImageViews. In each image view the user needs to be able to input a different image. Here is the code I have so far.

    var imagePicker = UIImagePickerController()
    @IBAction func chooseImage1(sender: AnyObject) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
        println("Button capture")


        imagePicker.delegate = self
        imagePicker.sourceType = .SavedPhotosAlbum
        imagePicker.allowsEditing = false

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

@IBAction func chooseImage2(sender: AnyObject) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
        println("Button capture")


        imagePicker2.delegate = self
        imagePicker2.sourceType = .SavedPhotosAlbum
        imagePicker2.allowsEditing = false

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

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

    let pickedImage2 = info[UIImagePickerControllerOriginalImage] as? UIImage
    chooseImage2.image = pickedImage2



    dismissViewControllerAnimated(true, completion: nil)
}



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

This ends up picking the same image for each different image view. I would like to be able to choose two individual photos, one for each view. Thank you for your help.

解决方案

You only need one UIImagePickerController. You can keep a reference of the tapped view and when the user finish picking the image, you just need to cast the selected view as UIImageView and set its image property:

update: Xcode 11.5 • Swift 5.2 or later

import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate ,UINavigationControllerDelegate {
    
    @IBOutlet weak var imageView1: UIImageView!
    @IBOutlet weak var imageView2: UIImageView!

    var imagePicker = UIImagePickerController()
    var selectedVew: UIView!
 
    override func viewDidLoad() {
        super.viewDidLoad()
        
        imagePicker.delegate = self
        imagePicker.sourceType = .savedPhotosAlbum
        imagePicker.allowsEditing = false
        
        [imageView1,imageView2].forEach {
            $0?.isUserInteractionEnabled = true
            $0?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(chooseImage)))
        }
    }
    
    @objc func chooseImage(_ gesture: UITapGestureRecognizer) {
        if UIImagePickerController.isSourceTypeAvailable(.savedPhotosAlbum) {
            selectedVew = gesture.view
            present(imagePicker, animated: true)
        }
    }
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        (selectedVew as? UIImageView)?.image = info[.originalImage] as? UIImage
        dismiss(animated: true)
    }
    
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true)
    }
}

这篇关于在 Swift 中使用 imagePickerController 在同一个视图控制器中选择两个不同的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)