Swiftswift学习

swift 实现拍照 选择相册

2019-01-07  本文已影响0人  江河_ios

//点击按钮的方法

func photos()  {
    
  self.showBottomAlert()
    
}

/// 屏幕底部弹出的Alert

func showBottomAlert(){
    
    let alertController=UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

    let cancel=UIAlertAction(title:"取消", style: .cancel, handler: nil)
    let takingPictures=UIAlertAction(title:"拍照", style: .default)
    {
        action in
        self.goCamera()
        
    }
    let localPhoto=UIAlertAction(title:"本地图片", style: .default)
    {
        action in
        self.goImage()
        
    }
    alertController.addAction(cancel)
    alertController.addAction(takingPictures)
    alertController.addAction(localPhoto)
    self.present(alertController, animated:true, completion:nil)
    
}

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

// 去拍照

func goCamera(){
                
    if UIImagePickerController.isSourceTypeAvailable(.camera){
        let  cameraPicker = UIImagePickerController()
        cameraPicker.delegate = self
        cameraPicker.allowsEditing = true
        cameraPicker.sourceType = .camera
        //在需要的地方present出来
        self.present(cameraPicker, animated: true, completion: nil)
    } else {
        
        print("不支持拍照")
        
    }

}

/// 去相册

func goImage(){

    
    let photoPicker =  UIImagePickerController()
    photoPicker.delegate = self
    photoPicker.allowsEditing = true
    photoPicker.sourceType = .photoLibrary
    //在需要的地方present出来
    self.present(photoPicker, animated: true, completion: nil)
    
}

//代理

    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)
}
上一篇 下一篇

猜你喜欢

热点阅读