选择多个图像并在图像视图中显示它们

时间:2018-03-12 07:35:35

标签: ios swift image uiimageview

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

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

2 个答案:

答案 0 :(得分:0)

您正在使用UIImagePickerController,但不允许多种图片选择。

如果您想一次选择多个图像,则需要创建自己的自定义图像选择器控制器。

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

答案 1 :(得分:0)

有很多第三方库,但是BSImagePicker帮助我解决了我的问题,并且我已经使用了一段时间了。

这是链接-> https://github.com/mikaoj/BSImagePicker

希望它也对您有帮助!