0
votes

I am using the BSImagePicker so I can choose multiple images at once from the photo library. I am not sure how to save the images into an image array after I am finished selecting the photos. I will need to save the images and also be able to view them through my Firebase Database. I have chosen BSImagePicker for handling of choosing multiple images at once.

class CarsUpload: UIViewController {
    
    var imageArray = [UIImage]()
    
    fileprivate func saveToDatabaseWithImageUrl() {
        guard let uid = Auth.auth().currentUser?.uid else { return }
        let userPostRef = Database.database().reference().child("posts").child(uid)
        let ref = userPostRef.childByAutoId()
        
        let values = ["value": "valueTest", "findersFee": "findersTest", "itemTitle": "test", "itemDescription": "test", "confirmed": "test", "location": "test", "bePresent": "test", "verification": "test", "creationDate": Date().timeIntervalSince1970] as [String : Any]
        
        ref.updateChildValues(values) { (err, ref) in
            if let err = err {
                print("Failed to save post to DB", err)
                return
            }
            self.dismiss(animated: true, completion: nil)
        }
    }
    
    let nextButton: UIButton = {
        let button = UIButton(type: .system)
        button.addTarget(self, action: #selector(nextPressed), for: .touchUpInside)
        return button
    }()
 
    @objc func nextPressed(){
        showImagePicker(self.nextButton)
    }
 
    @IBAction func showImagePicker(_ sender: UIButton) {
        let imagePicker = ImagePickerController()
        imagePicker.settings.selection.max = 5
        imagePicker.settings.theme.selectionStyle = .numbered
        imagePicker.settings.fetch.assets.supportedMediaTypes = [.image, .video]
        imagePicker.settings.selection.unselectOnReachingMax = true

        let start = Date()
        self.presentImagePicker(imagePicker, select: { (asset) in
            print("Selected: \(asset)")
        }, deselect: { (asset) in
            print("Deselected: \(asset)")
        }, cancel: { (assets) in
            print("Canceled with selections: \(assets)")
        }, finish: { (assets) in
            print("Finished with selections: \(assets)")
        }, completion: {
            let finish = Date()
            print(finish.timeIntervalSince(start))
        })
    }
   
    @IBAction func showCustomImagePicker(_ sender: UIButton) {
        let imagePicker = ImagePickerController()
        imagePicker.settings.selection.max = 1
        imagePicker.settings.selection.unselectOnReachingMax = true
        imagePicker.settings.fetch.assets.supportedMediaTypes = [.image, .video]
        imagePicker.albumButton.tintColor = UIColor.green
        imagePicker.cancelButton.tintColor = UIColor.red
        imagePicker.doneButton.tintColor = UIColor.purple
        imagePicker.navigationBar.barTintColor = .black
        imagePicker.settings.theme.backgroundColor = .black
        imagePicker.settings.theme.selectionFillColor = UIColor.gray
        imagePicker.settings.theme.selectionStrokeColor = UIColor.yellow
        imagePicker.settings.theme.selectionShadowColor = UIColor.red
        imagePicker.settings.theme.previewTitleAttributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16),NSAttributedString.Key.foregroundColor: UIColor.white]
        imagePicker.settings.theme.previewSubtitleAttributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12),NSAttributedString.Key.foregroundColor: UIColor.white]
        imagePicker.settings.theme.albumTitleAttributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18),NSAttributedString.Key.foregroundColor: UIColor.white]
        imagePicker.settings.list.cellsPerRow = {(verticalSize: UIUserInterfaceSizeClass, horizontalSize: UIUserInterfaceSizeClass) -> Int in
            switch (verticalSize, horizontalSize) {
                case (.compact, .regular): // iPhone5-6 portrait
                    return 2
                case (.compact, .compact): // iPhone5-6 landscape
                    return 2
                case (.regular, .regular): // iPad portrait/landscape
                    return 3
                default:
                    return 2
            }
        }

        self.presentImagePicker(imagePicker, select: { (asset) in
            print("Selected: \(asset)")
        }, deselect: { (asset) in
            print("Deselected: \(asset)")
        }, cancel: { (assets) in
            print("Canceled with selections: \(assets)")
        }, finish: { (assets) in
            print("Finished with selections: \(assets)")
        })
    }
   
    @IBAction func showImagePickerWithSelectedAssets(_ sender: UIButton) {
        let allAssets = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: nil)
        var evenAssets = [PHAsset]()
        allAssets.enumerateObjects({ (asset, idx, stop) -> Void in
            if idx % 2 == 0 {
                evenAssets.append(asset)
            }
        })

        let imagePicker = ImagePickerController(selectedAssets: evenAssets)
        imagePicker.settings.fetch.assets.supportedMediaTypes = [.image]

        self.presentImagePicker(imagePicker, select: { (asset) in
            print("Selected: \(asset)")
        }, deselect: { (asset) in
            print("Deselected: \(asset)")
        }, cancel: { (assets) in
            print("Canceled with selections: \(assets)")
        }, finish: { (assets) in
            print("Finished with selections: \(assets)")
        })
    }
 
    override func viewDidLoad() {
        view.addSubview(nextButton)
        view.backgroundColor = .white
        nextButton.anchor(top: nil, left: nil, bottom: view.bottomAnchor, right: nil, paddingTop: 20, paddingLeft: 0, paddingBottom: 90, paddingRight: 0, width: 0, height: 0)
        nextButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        super.viewDidLoad()
    }
}
1
your self.presentImagePicker(imagePicker, select: provides a asset which will contain the selected images. in func showImagePickerWithSelectedAssets is an example how to iterate/enumerate thru an asset, do something with (asset).Ol Sen

1 Answers

0
votes

Try this :

 https://github.com/mikaoj/BSImagePicker