swift 实现拍照 选择相册

//点击按钮的方法

1 func photos()  {
2     
3   self.showBottomAlert()
4     
5 }

 

/// 屏幕底部弹出的Alert

 1 func showBottomAlert(){
 2     
 3     let alertController=UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
 4 
 5     let cancel=UIAlertAction(title:"取消", style: .cancel, handler: nil)
 6     let takingPictures=UIAlertAction(title:"拍照", style: .default)
 7     {
 8         action in
 9         self.goCamera()
10         
11     }
12     let localPhoto=UIAlertAction(title:"本地图片", style: .default)
13     {
14         action in
15         self.goImage()
16         
17     }
18     alertController.addAction(cancel)
19     alertController.addAction(takingPictures)
20     alertController.addAction(localPhoto)
21     self.present(alertController, animated:true, completion:nil)
22     
23 }

 

 //拍照与本地相册方法/ 

// 去拍照

 1 func goCamera(){
 2                 
 3     if UIImagePickerController.isSourceTypeAvailable(.camera){
 4         let  cameraPicker = UIImagePickerController()
 5         cameraPicker.delegate = self
 6         cameraPicker.allowsEditing = true
 7         cameraPicker.sourceType = .camera
 8         //在需要的地方present出来
 9         self.present(cameraPicker, animated: true, completion: nil)
10     } else {
11         
12         print("不支持拍照")
13         
14     }
15 
16 }

 

/// 去相册

 1 func goImage(){
 2 
 3     
 4     let photoPicker =  UIImagePickerController()
 5     photoPicker.delegate = self
 6     photoPicker.allowsEditing = true
 7     photoPicker.sourceType = .photoLibrary
 8     //在需要的地方present出来
 9     self.present(photoPicker, animated: true, completion: nil)
10     
11 }

 

//代理

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {


    print("获得照片============= \(info)")
    
    let image : UIImage = info[UIImagePickerController.InfoKey.editedImage] as! UIImage

    //显示设置的照片
    imgView.image = image

    self.dismiss(animated: true, completion: nil)
}

 



作者:江河_ios
链接:https://www.jianshu.com/p/fc0768999ef8
 

转载于:https://www.cnblogs.com/-yun/p/10654209.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值