【发布时间】:2020-02-26 18:57:23
【问题描述】:
我正在编写一段代码,逻辑如下:
- 每次点击按钮时,都会打开一个新视图,并允许用户从手机图库中选择一张图片。 - 所选图像应加载并显示在水平滚动视图中。我面临的问题是选择者在选择图像时拾取器擦除罚款,但是图像不加载在拾取器解雇的UICollectionView上(占位符映像保留并且未被所选图像替换)。我的控制台中没有发生崩溃或错误。感谢您的帮助!
这是我用于此功能的视图控制器:
public class ImagePickerViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate, UICollectionViewDelegate, UICollectionViewDataSource {
var mediaArray = [UIImage]()
@IBAction func galleryButtonTapped(_ sender: Any) {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
let actionSheet = UIAlertController(title: "Gallery image", message: "Choose an image from your gallery", preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { (action: UIAlertAction) in
imagePickerController.sourceType = .photoLibrary
self.present(imagePickerController,animated: true, completion: nil )
}))
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(actionSheet, animated: true,completion: nil)
}
public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
mediaArray.append(selectedImage)
picker.dismiss(animated: true, completion: nil)
}
public func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return mediaArray.count
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MediaCollectionViewCell", for: indexPath) as! MediaCollectionViewCell
cell.mediaContent.image = mediaArray[indexPath.row]
return cell
}
}
【问题讨论】:
标签: ios swift uicollectionview uiscrollview scrollview