Skip to main content

Open Camera and Gallery In Swift 3

//Define this in your button action.
  @IBAction func profileBtnAct(_ sender: Any)
    {
        //Create the AlertController and add Its action like button in Actionsheet
        let actionSheetControllerIOS8: UIAlertController = UIAlertController(title: "Select Image", message: nil, preferredStyle: .actionSheet)
        
        let cancelActionButton = UIAlertAction(title: "Camera", style: .default) { _ in
            print("Camera")
            self.openCamera()
        }
        actionSheetControllerIOS8.addAction(cancelActionButton)
        
        let saveActionButton = UIAlertAction(title: "Gallery", style: .default)
        { _ in
            print("Gallery")
            self.openGallery()
        }
        actionSheetControllerIOS8.addAction(saveActionButton)
        
        let deleteActionButton = UIAlertAction(title: "Cancel", style: .destructive)
        { _ in
            print("Cancel")
        }
        actionSheetControllerIOS8.addAction(deleteActionButton)
        self.present(actionSheetControllerIOS8, animated: true, completion: nil)
   

    }

////Simply Add this any in you code;

func openGallery()
    {
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
            // print("Button capture")
            imagePicker.delegate = self
            imagePicker.sourceType =  UIImagePickerControllerSourceType.photoLibrary;
            imagePicker.allowsEditing = true
            self.present(imagePicker, animated: true, completion: nil)
        }
    }
    func openCamera()
    {
        if UIImagePickerController.isSourceTypeAvailable(.camera){
            // print("Button capture")
            
            imagePicker.delegate = self
            imagePicker.sourceType =  UIImagePickerControllerSourceType.camera;
            imagePicker.allowsEditing = true
            
            self.present(imagePicker, animated: true, completion: nil)
        }
        
    }
    func imagePickerController(_ picker: UIImagePickerController,
                               didFinishPickingMediaWithInfo info: [String : Any])
    {
        let choosenImage = info[UIImagePickerControllerEditedImage] as? UIImage
        profileimgOut.image = choosenImage
 self.dismiss(animated: true, completion: nil)
    }

At last add permission in your plist file

Key:- Privacy - Photo Library Usage Description
Description:- Allow app to use gallery

Happy Coding :)

Comments

Popular posts from this blog

post method using alamofire in swift 3

  func addSubscriptionApi(_completeUrl: String ) {         //HUD         Alamofire.request(_completeUrl, method:.post, parameters: nil , encoding: JSONEncoding. default , headers: nil ).responseJSON { response in             switch (response.result) {             case .success( _ ):                 if let data = response.result.value as ? NSDictionary                 {                     print(response.result.value as Any)                     print(data)                      print("SUCCESS")                  }                 break  ...

Set Placeholder in UiTextView in swift 3

//In viewdidLoad add this                DiscriptionTV . text = "Discription"         DiscriptionTV . textColor = UIColor . lightGray         DiscriptionTV . delegate = self          DiscriptionTV . layer . borderColor = UIColor . lightGray . cgColor ; then   //Mark:- textView Delegates          func textViewDidBeginEditing( _ textView: UITextView ) {         if DiscriptionTV . textColor == UIColor . lightGray {             DiscriptionTV . text = nil             DiscriptionTV . textColor = UIColor . white             DiscriptionLab . isHidden = false         }     }          func textViewDidEndEditing( _ textView: UITextView ) { ...