0
votes

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];
}
1
Why are you passing image views? Why not just pass the image data?atreat
Oh, because i suck... ^^ Stupid me! :PXT_Nova

1 Answers

1
votes

If its just an image we are talking about here then lets make a property for the image.

@property (nonatomic, strong) UIImage *imageIWant; 

Once we have our image property, we can set the image that you want by either calling [UIImage imageWithData:imageData]; or set it from a file within our Plist. I do not recommend saving an imageView and passing that view among controllers because within our MVC model, the view and the model are separate. At the end of the day, its an image within the model that you want not a view element.