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)

    }    

}


  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值