Selector de imagen de galeria o foto UIImagepicker imagePicker
AÑADIR PERMISOS A PLIST:
NSPhotoLibraryUsageDescription = "Access to the photo library";
NSCameraUsageDescription = "Access to the camera";
AÑADIR DELEGATE UIImagePickerControllerDelegate y UINavigationControllerDelegate
let picker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
}
PARA PHOTO LIBRARY:
@IBAction func photoFromLibrary(_ sender: UIBarButtonItem) {
picker.allowsEditing = false
picker.sourceType = .photoLibrary
picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
present(picker, animated: true, completion: nil)
}
PARA CAMARA:
@IBAction func shootPhoto(_ sender: UIBarButtonItem) {
if UIImagePickerController.isSourceTypeAvailable(.camera) {
picker.allowsEditing = false
picker.sourceType = UIImagePickerControllerSourceType.camera
picker.cameraCaptureMode = .photo
picker.modalPresentationStyle = .fullScreen
present(picker,animated: true,completion: nil)
}
}
//UIImagePickerControllerDelegate
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage //2
imgWord.image = chosenImage;
dismiss(animated:true, completion: nil) //5
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
1) Añadir delegates a .h
@interface LOAddDeviceViewController : TextViewController <UIActionSheetDelegate, UIImagePickerControllerDelegate>
2) Rutina que se asigna al tap de la imagen (mejor que boton) y que muestra el actionSheet
#pragma-mark Photo Selection
-(void) photoSelection{
[self.view endEditing:YES];
UIActionSheet *popup = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"chat_attach_msg",nil) delegate:self cancelButtonTitle:NSLocalizedString(@"lbl_cancel",nil) destructiveButtonTitle:nil otherButtonTitles:
NSLocalizedString(@"chat_attach_camera",nil),
NSLocalizedString(@"chat_attach_gallery",nil),
nil];
popup.tag = 1;
[popup showInView:self.view];
}
3) Delegate del ActionSheet
#pragma mark - Action Sheet Delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) { //select_gallery
[self showImagePicker:NO];
}
if (buttonIndex == 1) { //select_camera
[self showImagePicker:YES];
}
}
4) Delegate del imagePicker
#pragma mark - UIImagePickerController delegate
- (void)showImagePicker:(BOOL)fromAlbum {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.allowsEditing=NO;
imagePicker.delegate = self;
if (fromAlbum) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePicker animated:YES completion:nil];
}];
} else {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imagePicker animated:YES completion:nil];
}];
}
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *image = [info valueForKey: UIImagePickerControllerOriginalImage];
NSData *imageData = UIImageJPEGRepresentation(image, 0.1);
[self.imgPhotoSelector setImage:[UIImage imageWithData:imageData]];
[self.imgPhotoSelector setImage:[ViewsHelper roundImage:self.imgPhotoSelector].image];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}