swift3.0调用相机和相册 简单实用

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)

}

}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...