会出现程序崩溃问题,需要在info.plist中添加key
除了相册的权限,iOS10之后如下的权限请求也是需要我们填写请求描述的,在这里也给大家提醒一下:
Privacy - Microphone Usage Description //麦克风权限
Privacy - Contacts Usage Description //通讯录权限
Privacy - Camera Usage Description //摄像头权限
Privacy - NSSiriUsageDescription //Siri的权限
Privacy - Bluetooth Peripheral Usage Description //蓝牙
Privacy - Reminders Usage Description //提醒事项
Privacy - Motion Usage Description //运动与健康
Privacy - Media Libaray Usage Description //媒体资源库
Privacy - Calendars Usage Description //日历
在storyboard中添加一个image控件
拖一个手势控件到image上
关联
添加代码
代码如下
需要添加如下代码
主函数
import UIKit
import MediaPlayer
import MobileCoreServices
class RenZhengViewController: UIViewController{
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
imageView.isUserInteractionEnabled = true // 开启控件的交互操作, 否则轻触事件无法传递
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func backClicked(_ sender: UIButton) {
self.dismiss(animated: true, completion: nil)
}
// ----------------------------------------
}
继承函数
//MARK: 轻触 图片控件
extension RenZhengViewController {
// MARK: 用于弹出选择的对话框界面
var selectorController: UIAlertController {
let controller = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
controller.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil)) // 取消按钮
controller.addAction(UIAlertAction(title: "拍照选择", style: .default) { action in
self.selectorSourceType(.camera)
}) // 拍照选择
controller.addAction(UIAlertAction(title: "相册选择", style: .default) { action in
self.selectorSourceType(.photoLibrary)
}) // 相册选择
return controller
}
// MARK: 轻触手势事件的回调
@IBAction func onTapImageView(_ sender: UITapGestureRecognizer) {
present(selectorController, animated: true, completion: nil)
}
func selectorSourceType(_ type: UIImagePickerControllerSourceType) {
imagePickerController.sourceType = type
// 打开图片选择器
present(imagePickerController, animated: true, completion: nil)
}
}
//MARK: 扩展图片选择和结果返回
extension RenZhengViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// MARK: 图片选择器界面
var imagePickerController: UIImagePickerController {
get {
let imagePicket = UIImagePickerController()
imagePicket.delegate = self
imagePicket.sourceType = .photoLibrary
return imagePicket
}
}
// MARK: 当图片选择器选择了一张图片之后回调
private func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: AnyObject]) {
//dismissViewControllerAnimated(true, completion: nil)
dismiss(animated: true, completion: nil) // 选中图片, 关闭选择器...这里你也可以 picker.dismissViewControllerAnimated 这样调用...但是效果都是一样的...
imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage // 显示图片
imageView.contentMode = .scaleToFill // 缩放显示, 便于查看全部的图片
}
// MARK: 当点击图片选择器中的取消按钮时回调
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil) // 效果一样的...
}
}
使用摄像头暂时未解决。。。。。。。。
扫描二维码关注公众号,回复:
930835 查看本文章