专业java、php、iOS、C++、网页设计、平面设计、网络营销、游戏开发、前端与移动开发培训机构
照片选择器添加照片
在
photoSelectorCellDidClickAddButton
代理方法里面弹出系统相册
选择相片/// 加号按钮点击了 extension HMPhotoSelectorViewController: HMPhotoSelectorCellDelegate { func photoSelectorCellDidClickDeleteButton(cell: HMPhotoSelectorCell) { } func photoSelectorCellDidClickAddButton(cell: HMPhotoSelectorCell) { let pickerVC = UIImagePickerController() // 设置代理 pickerVC.delegate = self present(pickerVC, animated: true, completion: nil) } }
HMPhotoSelectorViewController
实现UIImagePickerControllerDelegate
UINavigationControllerDelegate
协议.获得选取的图片
// MARK: - HMPhotoSelectorViewController 实现 extension HMPhotoSelectorViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate { func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { print("info: \(info)") let image = info[UIImagePickerControllerOriginalImage] print("image111: \(image)") picker.dismiss(animated: true, completion: nil) } }
照片选择器
需要显示选择的图片
,需要将选择的图片保存起来
让collecitonView
去显示- 在
HMPhotoSelectorViewController
定义photos
属性,保存选中的图片/// 选择的图片 private var photos = [UIImage]()
在
UIImagePickerControllerDelegate
的didFinishPickingImage
方法中将获取到的image
添加到photos
数组里面extension HMPhotoSelectorViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate { func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { let image = info[UIImagePickerControllerOriginalImage] as! UIImage // 将选中的图片添加到图片数组 photos.append(image) // collectionView 刷新数据 collectionView?.reloadData() picker.dismiss(animated: true, completion: nil) } }
- 设置
collectionView
的代理方法numberOfItemsInSection
返回数量override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { // 1.photos.count < maxPhotoCount 显示 photos.count + 1 // 2.photos.count = maxPhotoCount 显示 photos.count return photos.count < maxPhotoCount ? photos.count + 1 : photos.count }
设置
collectionView
的代理方法cellForItemAtIndexPath
返回cell
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! HMPhotoSelectorCell cell.backgroundColor = UIColor.brown cell.delegate = self // 显示photos里面对应的图片 if indexPath.item < photos.count { cell.image = photos[indexPath.item] } else { // 需要显示加号按钮 } return cell }
- 在
HMPhotoSelectorCell
添加image
属性var image: UIImage? { didSet { addButton.setImage(image, for: .normal) } }
运行,确实可以添加图片,但是加号按钮不需要显示删除按钮,在
HMPhotoSelectorCell
添加设置加号按钮方法setupAddButton
/// 设置加号按钮 func setupAddButton() { self.addButton.setImage(UIImage(named: "compose_pic_add"), for: UIControlState()) // 删除按钮隐藏掉 self.deleteButton.isHidden = true }
在
cellForItemAtIndexPath
方法设置加号按钮override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! HMPhotoSelectorCell cell.backgroundColor = UIColor.brown cell.delegate = self // 显示photos里面对应的图片 if indexPath.item < photos.count { cell.image = photos[indexPath.item] } else { // 需要显示加号按钮 cell.setupAddButton() } return cell }
在
HMPhotoSelectorCell
的image: UIImage
属性时显示删除按钮var image: UIImage? { didSet { // 设置图片 addButton.setImage(image, for: .normal) // 显示删除按钮 deleteButton.isHidden = false } }
- 在