附加元素后的空数组

Empty array after appended element

我正在尝试从导航栏上的 UIAlertController 将图像上传到 UICollectionView。集合视图位于另一页。

我使用 BSImagePicker 第 3 方库,它允许选择多张照片。

selectImages() 函数实际上将照片附加到 selectedImages 数组并在调试器上打印它们

import Photos
import BSImagePicker

class CustomFeedNavigationBar: UIView {

    static let height: CGFloat = 48
    var collectionView = NewPostImagesViewController().collectionView
    var selectedImages: [UIImage] = []

    var rootVC = UIApplication.shared.keyWindow?.rootViewController
    unowned var navigationController: UINavigationController

    init(navigationController: UINavigationController) {
        self.navigationController = navigationController
        super.init(frame: .zero)

        translatesAutoresizingMaskIntoConstraints = false

        addSubview(addPostButton)
    }

    lazy var addPostButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "addPost"), for: .normal)
        button.imageView?.contentMode = .scaleAspectFit
        button.addTarget(self, action: #selector(handleAdd), for: .touchUpInside)
        button.translatesAutoresizingMaskIntoConstraints = false
        return button
    }()

    @objc func handleAdd() {
        let actionSheet = UIAlertController(title: "Add post", message: nil, preferredStyle: .actionSheet)

        actionSheet.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler: { action in
            print("tap dismiss")
        }))
        actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { action in
            print("tap camera")
        }))
        actionSheet.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { action in

            self.selectImages()

            let viewController = NewPostImagesViewController()
            viewController.hidesBottomBarWhenPushed = true
            self.navigationController.pushViewController(viewController, animated: true)
        }))

        rootVC?.present(actionSheet, animated: true, completion: nil)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension CustomFeedNavigationBar {

    func selectImages() {
        let imagePicker = ImagePickerController()
        rootVC?.presentImagePicker(imagePicker, select: { (asset) in
        }, deselect: { (asset) in

        }, cancel: { (assets) in

        }, finish: { (assets) in

            self.selectedImages = []
            let options: PHImageRequestOptions = PHImageRequestOptions()
            options.deliveryMode = .highQualityFormat

            for asset in assets {
                PHImageManager.default().requestImage(for: asset, targetSize: PHImageManagerMaximumSize,
                                                      contentMode: .aspectFit, options: options) { (image, info) in
                    self.selectedImages.append(image!)
                    print("images appended? \(self.selectedImages) YES") // selectedImages array appended elements
                    self.collectionView.reloadData()
                }
            }
        })
    }
}

但是当我点击下一步进入下一页时,数组是空的!它在调试器上什么也没显示

class NewPostImagesViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // ...
        // Loaded collection view and etc.

        print("images loaded? \(CustomFeedNavigationBar(navigationController: navigationController!).selectedImages)") // selectedImages array is empty again
    }
}

这是调试器输出:

images not appended []
images appended? [<UIImage:0x6000030cad00 anonymous {1668, 2500}>] YES
images appended? [<UIImage:0x6000030cad00 anonymous {1668, 2500}>, <UIImage:0x6000030cdf80 anonymous {4288, 2848}>] YES

这里

print("images loaded? \(CustomFeedNavigationBar(navigationController: navigationController!).selectedImages)") // selectedImages array is empty again

您创建了一个新实例而不是具有图像的实例,因此请尝试:

  1. class NewPostImagesViewController: UIViewController {
        var selectedImages = [UIImage]()
    
  2. actionSheet.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { action in
    
        self.selectImages()
    
    }))
    
  3. let g = DispatchGroup()
    for asset in assets {
        g.enter()
        PHImageManager.default().requestImage(for: asset, targetSize: PHImageManagerMaximumSize,
                                              contentMode: .aspectFit, options: options) { (image, info) in
            self.selectedImages.append(image!)
            print("images appended? \(self.selectedImages) YES") // selectedImages array appended elements
            self.collectionView.reloadData() 
            g.leave()
        }
    }
    
    g.notify(queue:.main) {
       let viewController = NewPostImagesViewController()
       viewController.hidesBottomBarWhenPushed = true
       viewController.selectedImages = selectedImages 
    self.navigationController.pushViewController(viewController, animated: true)
    }