swift3.0调用相机和相册 简单实用
2016-09-23 16:56
183 查看
4000
1、首先,swift3.0中调用相机和相册会导致崩溃,需要在info.plist文件中加入两个键值对,如下:
Privacy - Photo Library Usage Description 和 Privacy - Camera Usage Description ,都是String类型,内容任意的字符串即可。
2、废话少说,上代码!
class MyController:UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate
{
var uploadAlertController:UIAlertController!
var imagePickerController:UIImagePickerController!
@IBOutletweakvar headImg:UIImageView!
overridefunc viewDidLoad() {
super.viewDidLoad()
self.initAlertController()
self.initImagePickerController()
}
func initAlertController()
{
weakvar blockSelf =self
self.uploadAlertController =UIAlertController(title:nil,
message: nil, preferredStyle:UIAlertControllerStyle.actionSheet)
self.uploadAlertController.view.tintColor
= DeepMainColor
let takePhoto =UIAlertAction(title:"拍照",
style:UIAlertActionStyle.default) { (action:UIAlertAction)in
blockSelf?.actionAction(action: action)
}
let photoLib =UIAlertAction(title:"从相册选择",
style:UIAlertActionStyle.default) { (action:UIAlertAction)in
blockSelf?.actionAction(action: action)
}
let cancel =UIAlertAction(title:"取消",
style:UIAlertActionStyle.cancel) { (action:UIAlertAction)in
blockSelf?.actionAction(action: action)
}
self.uploadAlertController?.addAction(takePhoto)
self.uploadAlertController?.addAction(photoLib)
self.uploadAlertController?.addAction(cancel)
}
func initImagePickerController()
{
self.imagePickerController =UIImagePickerController()
self.imagePickerController.delegate
= self
// 设置是否可以管理已经存在的图片或者视频
self.imagePickerController.allowsEditing
= true
}
func actionAction(action:UIAlertAction)
{
if action.title =="拍照"
{
self.getImageFromPhotoLib(type: .camera)
}elseif action.title =="从相册选择"||
action.title =="更换头像"
{
self.getImageFromPhotoLib(type: .photoLibrary)
}elseif action.title =="删除照片"
{
self.headImg.image =UIImage(named:"head")
}
}
func getImageFromPhotoLib(type:UIImagePickerControllerSourceType)
{
self.imagePickerController.sourceType
= type
//判断是否支持相册
ifUIImagePickerController.isSourceTypeAvailable(.photoLibrary)
{
self.present(self.imagePickerController,
animated: true, completion:nil)
}
}
//MARK:- UIImagePickerControllerDelegate
func imagePickerController(_ picker:UIImagePickerController, didFinishPickingMediaWithInfo
info: [String :Any]){
let type:String = (info[UIImagePickerControllerMediaType]as!String)
//当选择的类型是图片
if type=="public.image"
{
let img = info[UIImagePickerControllerOriginalImage]as?UIImage
self.headImg.image =cropToBounds(image:
img!)
let imgData =UIImageJPEGRepresentation(self.headImg.image!,0.5)
picker.dismiss(animated:true, completion:nil)
}
}
func imagePickerControllerDidCancel(_ picker:UIImagePickerController){
picker.dismiss(animated:true, completion:nil)
}
@IBActionfunc headImgTapGesture(_ sender:AnyObject)
{
present(self.uploadAlertController, animated:true,
completion: nil)
}
}
1、首先,swift3.0中调用相机和相册会导致崩溃,需要在info.plist文件中加入两个键值对,如下:
Privacy - Photo Library Usage Description 和 Privacy - Camera Usage Description ,都是String类型,内容任意的字符串即可。
2、废话少说,上代码!
class MyController:UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate
{
var uploadAlertController:UIAlertController!
var imagePickerController:UIImagePickerController!
@IBOutletweakvar headImg:UIImageView!
overridefunc viewDidLoad() {
super.viewDidLoad()
self.initAlertController()
self.initImagePickerController()
}
func initAlertController()
{
weakvar blockSelf =self
self.uploadAlertController =UIAlertController(title:nil,
message: nil, preferredStyle:UIAlertControllerStyle.actionSheet)
self.uploadAlertController.view.tintColor
= DeepMainColor
let takePhoto =UIAlertAction(title:"拍照",
style:UIAlertActionStyle.default) { (action:UIAlertAction)in
blockSelf?.actionAction(action: action)
}
let photoLib =UIAlertAction(title:"从相册选择",
style:UIAlertActionStyle.default) { (action:UIAlertAction)in
blockSelf?.actionAction(action: action)
}
let cancel =UIAlertAction(title:"取消",
style:UIAlertActionStyle.cancel) { (action:UIAlertAction)in
blockSelf?.actionAction(action: action)
}
self.uploadAlertController?.addAction(takePhoto)
self.uploadAlertController?.addAction(photoLib)
self.uploadAlertController?.addAction(cancel)
}
func initImagePickerController()
{
self.imagePickerController =UIImagePickerController()
self.imagePickerController.delegate
= self
// 设置是否可以管理已经存在的图片或者视频
self.imagePickerController.allowsEditing
= true
}
func actionAction(action:UIAlertAction)
{
if action.title =="拍照"
{
self.getImageFromPhotoLib(type: .camera)
}elseif action.title =="从相册选择"||
action.title =="更换头像"
{
self.getImageFromPhotoLib(type: .photoLibrary)
}elseif action.title =="删除照片"
{
self.headImg.image =UIImage(named:"head")
}
}
func getImageFromPhotoLib(type:UIImagePickerControllerSourceType)
{
self.imagePickerController.sourceType
= type
//判断是否支持相册
ifUIImagePickerController.isSourceTypeAvailable(.photoLibrary)
{
self.present(self.imagePickerController,
animated: true, completion:nil)
}
}
//MARK:- UIImagePickerControllerDelegate
func imagePickerController(_ picker:UIImagePickerController, didFinishPickingMediaWithInfo
info: [String :Any]){
let type:String = (info[UIImagePickerControllerMediaType]as!String)
//当选择的类型是图片
if type=="public.image"
{
let img = info[UIImagePickerControllerOriginalImage]as?UIImage
self.headImg.image =cropToBounds(image:
img!)
let imgData =UIImageJPEGRepresentation(self.headImg.image!,0.5)
picker.dismiss(animated:true, completion:nil)
}
}
func imagePickerControllerDidCancel(_ picker:UIImagePickerController){
picker.dismiss(animated:true, completion:nil)
}
@IBActionfunc headImgTapGesture(_ sender:AnyObject)
{
present(self.uploadAlertController, animated:true,
completion: nil)
}
}
相关文章推荐
- [iOS 相机相册调用] UIImagePickerController 简单实用 [转]
- 上传头像 调用相机拍照 ,及简单的调用相册的照片来上传头像
- 【iOS】一个完整的简单的调用系统相机和相册设置头像
- 关于iOS中简单实现调用系统相机及相册功能
- 关于iOS中简单实现调用系统相机及相册功能
- 关于iOS中简单实现调用系统相机及相册功能
- 关于iOS中简单实现调用系统相机及相册功能
- IOS相机 相册简单实用+图片正方形剪裁
- Android简单的调用系统相机和相册
- 调用相机拍照并得到图片-------得到系统相册中一张图片
- ios调用相册或相机上传图片
- [Android] 调用相机、打开相册、裁剪图片
- iOS调用系统相册、相机 显示中文标题
- android --相机调用方式(简单的转载复制粘贴)
- Android 调用相机、打开相册、裁剪图片
- ecshop 在任何页面调用商品属性 超简单实用方法
- 系统相册的调用,和相机的调用
- iOS调用相册、相机、上传等问题
- Android 调用系统相机拍照保存以及调用系统相册的方法
- Android调用系统相机拍照,并且模仿实现水印相机简单功能