swift 實現(xiàn)拍照 選擇相冊

  • 點擊按鈕的方法
    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
    
      //顯示設(shè)置的照片
    imgView.image = image
    
    self.dismiss(animated: true, completion: nil)
    

    }

  • 第三方庫實現(xiàn) TZImagePickerController
    必須實現(xiàn)TZImagePickerControllerDelegate
     let imagePickerVc = TZImagePickerController(maxImagesCount: 1, delegate: self)
     imagePickerVc?.modalPresentationStyle = .custom
     self.present(imagePickerVc!, animated: true)

// 圖片選擇器的代理
// TZImagePickerControllerDelegate
func imagePickerController(_ picker: TZImagePickerController!, didFinishPickingPhotos photos: [UIImage]!, sourceAssets assets: [Any]!, isSelectOriginalPhoto: Bool) {
    if let image = photos.first {
        //顯示設(shè)置的照片
        imageview.image = image
                    
    }
    
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,422評論 4 61
  • 本文介紹如何將目標(biāo)字符串復(fù)制存儲到剪貼板上,代碼如下: 本文將持續(xù)更新,如有需要請關(guān)注!荊軻刺秦王!
    oc123閱讀 406評論 0 2
  • 來源:法律職業(yè)資格考試網(wǎng) 希律法考交流群:343325550 為了改革和完善司法考試制度,即將開始的國家統(tǒng)一法律職...
    Pe1za1閱讀 393評論 0 0
  • 2018.1.2 星期二 陰 今天兒子上學(xué)去了,又開始新的一年的一天,好好學(xué)習(xí)吧。下午帶二寶去了醫(yī)院一趟,...
    鑫隆媽媽閱讀 187評論 0 5

友情鏈接更多精彩內(nèi)容