调用媒体库的方法

@objc private func addPhotoClick() {
        //判断数据源是否可用
        if !UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
            return
        }
        
        //创建照片选择控制器
        let ipc = UIImagePickerController()
        
        //设置照片源
        ipc.sourceType = .photoLibrary
        
        //设置代理
        ipc.delegate = self
        
        //弹出选择照片的控制器
        present(ipc, animated: true, completion: nil)
        
    }
}

//MARK:-UIImagePickerController的代理方法
extension ComposeViewController : UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        //获取选中的照片
        let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
        
        //展示照片
        images.append(image)
        
        //将数组赋值给collectionView,让其展示数据
        picPickerView.images = images
     
        //退出选中照片控制器
        picker.dismiss(animated: true, completion: nil)
    }
}

你可能感兴趣的:(调用媒体库的方法)