【发布时间】:2021-09-11 16:47:23
【问题描述】:
我正在尝试从导航栏上的 UIAlertController 将图像上传到 UICollectionView。集合视图位于另一个页面上。
我使用允许多张照片选择的 BSImagePicker 3rd 方库。
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
【问题讨论】:
-
您正在打印中新建一个 CustomFeedNavigationBar 实例
-
@Shadowrun 我只是将 CustomFeedNavigationBar 添加到打印中,只是为了访问 selectedImages 属性。有什么更好的方法?
-
不,您添加了“a”(新)CustomFeedNavigationBar,而不是“the”CustomFeedNavigationBar(您已经拥有),这就是数组为空的原因。
标签: ios arrays swift uicollectionview cocoapods