1.准备工作
在info.list
Privacy - Camera Usage Description//访问相机
Privacy - Photo Library Usage Description//访问相册
Localized resources can be mixed 设为YES //显示系统文字(要不然为英文)
2.代码
//遵守两个协议
class DataViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate{
///相机,相册
var cameraPicker: UIImagePickerController!
var photoPicker: UIImagePickerController!
override func viewDidLoad() {
super.viewDidLoad()
self.initPhotoPicker()
self.initCameraPicker()
}
//MARK: - Method
func initCameraPicker(){
cameraPicker = UIImagePickerController()
cameraPicker.delegate = self
cameraPicker.sourceType = .camera
//在需要的地方present出来
//self.present(cameraPicker, animated: true, completion: nil)
}
func initPhotoPicker(){
photoPicker = UIImagePickerController()
photoPicker.delegate = self
photoPicker.sourceType = .photoLibrary
//在需要的地方present出来
//self.present(photoPicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
self.dismiss(animated: true, completion: nil)
//获得照片
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
if photoArray.count < 6 {
photoArray.insert(image, at: 0)
}else {
//最多上传6张
}
}
}

本文介绍如何在iOS应用中集成相机与相册功能,包括必要的准备工作、代码实现细节及相册多选功能的实现。文章通过示例代码展示了如何创建UIImagePickerController实例,并设置其代理方法来处理用户选择的照片。
634

被折叠的 条评论
为什么被折叠?



