Swift 4 - 选择多个图像并在图像视图中显示它们 [英] Swift 4 - Select multiple images and display them in image views

查看:84
本文介绍了Swift 4 - 选择多个图像并在图像视图中显示它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试选择并显示多张图像(约5张图像),但出于某种原因,我一次只能选择并显示单张图像。我试图实现以下代码来显示多个图像。请帮帮我们提前致谢。

I am trying to choose and display multiple images (approx. 5 images) but for some reason I am only able to select and display a single image at a time. I have tried to implement the below code for displaying multiple images. Please help guys. Thanks in advance.

import UIKit
import Alamofire

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate  {

    let imagesData = [UIImage]()

    @IBOutlet weak var myImageView: UIImageView!
    @IBOutlet weak var myImageView1: UIImageView!
    @IBOutlet weak var myImageView2: UIImageView!
    @IBOutlet weak var myImageView3: UIImageView!
    @IBOutlet weak var myImageView4: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        myImageView.backgroundColor =  #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1)
        myImageView1.backgroundColor = #colorLiteral(red: 0.1764705926, green: 0.4980392158, blue: 0.7568627596, alpha: 1)
        myImageView2.backgroundColor = #colorLiteral(red: 0.06274510175, green: 0, blue: 0.1921568662, alpha: 1)
        myImageView3.backgroundColor = #colorLiteral(red: 0.5791940689, green: 0.1280144453, blue: 0.5726861358, alpha: 1)
        myImageView4.backgroundColor = #colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1)
    }

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

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        let image = info[UIImagePickerControllerOriginalImage] as! UIImage

        myImageView.image = image
        myImageView.contentMode = .scaleAspectFill
        myImageView1.image = image
        myImageView1.contentMode = .scaleAspectFill
        myImageView2.image = image
        myImageView2.contentMode = .scaleAspectFill
        myImageView3.image = image
        myImageView3.contentMode = .scaleAspectFill
        myImageView4.image = image
        myImageView4.contentMode = .scaleAspectFill
        dismiss(animated: true, completion: nil)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let controller = UIImagePickerController()
        controller.delegate = self
        controller.sourceType = .photoLibrary
        present(controller, animated: true, completion: nil)
    }
}


推荐答案

您正在使用 UIImagePickerControlle r 并且它不允许多个图像选择。

You are using UIImagePickerController and it don't allow multiple selection of images.

如果你想一次选择多个图像,那么你需要创建你的拥有自定义图像选择器控制器。

If you want to select multiple images at a time then you need to create your own custom image picker controller.

另一种方法是,当用户选择图像时,在 imageview1 中设置该图像并再次显示图像选取器控制器以选择第二张图像,依此类推......直到您的图像数量达到5。

Another way is that, when user select an image the set that image in imageview1 and present image picker controller again to select 2nd image, and so on...until your image count reaches 5.

这篇关于Swift 4 - 选择多个图像并在图像视图中显示它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆