Swift4.0 从相册中获取图片和拍照
2024-08-27 21:47:39
第一步
添加协议
UIImagePickerControllerDelegate,UINavigationControllerDelegate
第二步
添加选择方式
let sexActionSheet = UIAlertController.init(title: nil, message: nil, preferredStyle: .actionSheet) weak var weakSelf = self let sexNanAction = UIAlertAction(title: "从相册中选择", style: UIAlertActionStyle.default){ (action:UIAlertAction)in weakSelf?.initPhotoPicker()
//填写需要的响应方法 } let sexNvAction = UIAlertAction(title: "拍照", style: UIAlertActionStyle.default){ (action:UIAlertAction)in weakSelf?.initCameraPicker()
//填写需要的响应方法 } let sexSaceAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.cancel){ (action:UIAlertAction)in //填写需要的响应方法 } sexActionSheet.addAction(sexNanAction)
sexActionSheet.addAction(sexNvAction)
sexActionSheet.addAction(sexSaceAction) self.present(sexActionSheet, animated: true, completion: nil)
第三步
//MARK: - 相机 //从相册中选择
func initPhotoPicker(){
let photoPicker = UIImagePickerController()
photoPicker.delegate = self
photoPicker.allowsEditing = true
photoPicker.sourceType = .photoLibrary
//在需要的地方present出来
self.present(photoPicker, animated: true, completion: nil)
} //拍照
func initCameraPicker(){ if UIImagePickerController.isSourceTypeAvailable(.camera){
let cameraPicker = UIImagePickerController()
cameraPicker.delegate = self
cameraPicker.allowsEditing = true
cameraPicker.sourceType = .camera
//在需要的地方present出来
self.present(cameraPicker, animated: true, completion: nil)
} else { print("不支持拍照") } } func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { //获得照片
let image:UIImage = info[UIImagePickerControllerEditedImage] as! UIImage // 拍照
if picker.sourceType == .camera {
//保存相册
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(image:didFinishSavingWithError:contextInfo:)), nil)
} personImage.image = image self.dismiss(animated: true, completion: nil)
} @objc func image(image:UIImage,didFinishSavingWithError error:NSError?,contextInfo:AnyObject) { if error != nil { print("保存失败") } else { print("保存成功") }
}
最新文章
- Java的多线程机制系列:不得不提的volatile及指令重排序(happen-before)
- Git服务器搭建及配置
- VIJOS P1037搭建双塔[DP]
- Sharepoint 2010 创建栏 计算栏
- return false取消手机移动端的默认设置
- [系统集成] CI持续集成项目简介
- jq获取表单值与赋值代码
- 单元测试工具之Xunit
- Python 命令行参数解析
- fiddler Composer 构建请求
- meta的属性详解
- 基于Mysql 5.7 GTID 搭建双主Keepalived 高可用
- property与x.setter与x.deleter
- 问题:计算foldRight(1)(_-_) 与foldLeft(1)(_-_)值不一样
- Equal 路由类
- python变量类型&;字符串的内建函数使用
- Sublime Text 2 配置PHP调试环境(在windows环境下)
- 自己实现数据结构系列五---BinarySearchTree
- C# 调用线程并行上下文穿透-ILogicalThreadAffinative+CallContext
- 源码编译安装MySQL8.0
热门文章
- Spring Boot 最核心的 25 个注解,都是干货!
- Spring Data Jpa接口简介
- MySQL查询昨天的数据
- Mongodb的集合插入一个字段
- Mac下 .bash_profile 和 .zshrc 两者之间的区别
- ASP.NET Core WebAPI控制器返回类型的最佳选项
- QWebView加载网页
- Unable to build: the file dx.jar was not loaded from the SDK folder
- SpringBoot入门教程(八)配置logback日志
- Chapter 5 Blood Type——14