//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
Post a Comment