I'm trying to choose an image from the camera roll in a view controller and then pass it/show that image in another view controller. Why doesn't the image view get passed?
When landing on FirstViewController i have a segue in the navigation bar to the SecondViewController.
//FirstViewController.h
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@end
//FirstViewController.m
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController () <SeconViewControllerDelegate>
@end
@implementation FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark - SecondViewControllerDelegate
- (void)didDismissWithImageView:(UIImageView *)imageView
{
self.imageView = imageView;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"addPhoto"]) {
SecondViewController *secondVC = segue.destinationViewController;
secondVC.delegate = self;
}
}
@end
//SecondViewController.h
#import <UIKit/UIKit.h>
#import <MobileCoreServices/MobileCoreServices.h>
@protocol PhotoPickerDelegate <NSObject>
- (void)didDismissWithImageView:(UIImageView *)imageView;
@end
@interface SecondViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (weak, nonatomic) id<PhotoPickerDelegate> delegate;
@property (strong, nonatomic) UIImageView *imageView;
- (IBAction)useCameraRoll:(id)sender;
@end
//SecondViewController.m
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (IBAction)useCameraRoll:(id)sender
{
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeSavedPhotosAlbum]){
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = @[(NSString *) kUTTypeImage];
imagePicker.allowsEditing = NO;
[self presentViewController:imagePicker animated:YES completion:nil];
}
}
#pragma mark - UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = info[UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:YES completion:nil];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image = info[UIImagePickerControllerOriginalImage];
_imageView.image = image;
}
if ([self.delegate respondsToSelector:@selector(didDismissWithImageView:)]) {
[self.delegate didDismissWithImageView:_imageView];
}
[self.navigationController popViewControllerAnimated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}