ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • iOS ) 버튼에 카메라 찍기, 갤러리 접속하기 코드
    Programing Language/iOS(Swift) 2019. 11. 6. 18:02
    728x90
    반응형

    코드만 올립니다. 

    import MobileCoreServices
    import AVFoundation
    
    
    
    // 밑의 코드를 btn.addTarget 에서 action에 넣어줍니다.
    
    let camerabtn : UIButton =
        {
            let btn = UIButton()
            btn.translatesAutoresizingMaskIntoConstraints = false
            btn.setTitle("Camera", for: .normal)
            btn.setTitleColor(.blue, for: .normal)
            btn.backgroundColor = .darkGray
            btn.addTarget(self, action: #selector(cameraClick), for: .touchUpInside)
            return btn
        }()
        
        
        
        
    @objc func cameraClick()
        {
            //카메라 접근 퍼미션 받는 코드입니다.
            switch AVCaptureDevice.authorizationStatus(for: .video) {
                case .authorized: // The user has previously granted access to the camera.
                    //self.setupCaptureSession()
                    break
                case .notDetermined: // The user has not yet been asked for camera access.
                    AVCaptureDevice.requestAccess(for: .video) { granted in
                        if granted {
                            //self.setupCaptureSession()
                        }
                    }
                    break;
                
                case .denied: // The user has previously denied access.
                    return
    
                case .restricted: // The user can't grant access due to restrictions.
                    return
            }
            if(UIImagePickerController.isSourceTypeAvailable(.camera)){
                flagImageSave = true
                print("camera: click")
                imagePicker.sourceType = .camera
                imagePicker.mediaTypes = [kUTTypeImage as String]
                imagePicker.allowsEditing = false
                
                present(imagePicker, animated: true, completion: nil)
            } else  {
                print("실행안됌")
            }
        }
        
        @objc func albumClicked()
        {
            if (UIImagePickerController.isSourceTypeAvailable(.photoLibrary)) {
                flagImageSave = false
                print("album: click")
                imagePicker.delegate = self
                imagePicker.sourceType = .photoLibrary
                imagePicker.mediaTypes = [kUTTypeImage as String]
                imagePicker.allowsEditing = true
                
                present(imagePicker, animated: true, completion: nil)
            }
        }

     

    찍거나 이미지 갤러리에서 선택한 이미지 불러오기 코드

    extension ViewController : UIImagePickerControllerDelegate,
    UINavigationControllerDelegate {
        
        //사진 찍은 후, 앨범에서 사진을 가져온 후 실행되는 함수
               @available(iOS 2.0, *)
               public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]){
                   
                
                if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
                {
                    dragImageView.image = image
                    for a in info {
                        print(a)
                    }
                   //print(info)
                    
                }
                
                
    //            let mediaType = info[UIImagePickerController.InfoKey.mediaType] as! NSString
    //               print("mediaType: \(mediaType)")
    //
    //               if mediaType.isEqual(to: kUTTypeImage as NSString as String) {
    //                captureImage = info[UIImagePickerController.InfoKey.mediaType] as? UIImage
    //                print("captureImage: \(String(describing: captureImage))")
    //                   if flagImageSave {
    //                       UIImageWriteToSavedPhotosAlbum(captureImage, self, nil, nil)
    //                   }
    //
    //                   //imageView.image = captureImage
    //               }
                   self.dismiss(animated: true, completion: nil)
               }
    
               @available(iOS 2.0, *)
               public func imagePickerControllerDidCancel(_ picker: UIImagePickerController){
                   self.dismiss(animated: true, completion: nil)
               }
        
    }

    dragImagveView는 UIImageView 입니다.

    728x90
    반응형
Designed by Tistory.