如何在视图控制器之间传递UIImageView?

时间:2014-09-24 20:12:18

标签: ios objective-c ios7 uiimageview uiimagepickercontroller

我正在尝试从视图控制器中的相机胶卷中选择一个图像,然后将其传递/在另一个视图控制器中显示该图像。为什么图像视图没有通过?

登陆FirstViewController时,我在导航栏中有一个segue到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 个答案:

答案 0 :(得分:1)

如果它只是我们在这里讨论的图像,那么让我们为图像创建一个属性。

@property (nonatomic, strong) UIImage *imageIWant; 

一旦我们拥有了image属性,我们就可以通过调用[UIImage imageWithData:imageData];或从Plist中的文件设置它来设置所需的图像。我不建议保存imageView并在控制器之间传递该视图,因为在我们的MVC模型中,视图和模型是分开的。在一天结束时,它是模型中您不想要的视图元素的图像。