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)
}
}
总结以上是内存溢出为你收集整理的swift3.0调用相机和相册 简单实用全部内容,希望文章能够帮你解决swift3.0调用相机和相册 简单实用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)