专业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]()
      
    • UIImagePickerControllerDelegatedidFinishPickingImage 方法中将获取到的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
      }
      
    • HMPhotoSelectorCellimage: UIImage 属性时显示删除按钮

      var image: UIImage? {
        didSet {
            // 设置图片
            addButton.setImage(image, for: .normal)
      
            // 显示删除按钮
            deleteButton.isHidden = false
        }
      }
      

results matching ""

    No results matching ""