Help us understand the problem. What is going on with this article?

UIImagePickerで、取得した画像を別Viewで表示する。

More than 5 years have passed since last update.

UIImagePickerで画像を取得した後に、新しいViewで画像を表示させます。
以下、FirstViewControllerでUIImagePickerで画像を取得し、SecondViewControllerで画像を表示させます。

FirstViewController.h
//
//    FirstViewController.h
//

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@end

UIImagePickerを使用するためのdelegate宣言として、UIImagePickerControllerDelegateとUINavigationControllerDelegateを宣言します。

FirstViewController.m
//
//    SecondViewController.m
//
#import "FirstViewController.h"
#import "SecondViewController.h"

@interface FirstViewController ()

@end


@implementation FirstViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (IBAction)touchSelectImage:(id)sender {
        UIImagePickerController *imagePickerVC = [[UIImagePickerController alloc] init];
        imagePickerVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        imagePickerVC.delegate = self;
        [self presentViewController:imagePickerVC animated:YES completion:nil];
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    secondVC.selectImage = image;
    [self.view addSubview:secondVC.self.view];
}

didFinishPickingMediaWithInfoで、取得した画像オブジェクト(UIImagePickerControllerEditedImage)を、secondViewで定義するUIImageに代入します。
その後、[self.view addSubview:secondVC.self.view]で、secondVCを表示します。

SecondViewController.h
//
//    SecondViewController.h
//

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIImage *selectImage;
@property (weak, nonatomic) IBOutlet UIImageView *selectImageView;

@end


FirstViewControllerから取得してくるUIImageをselectImageとして定義します。

SecondViewController.m
//
//    SecondViewController.m
//

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController
{
    __weak IBOutlet UIImage *selectImage;
}

@synthesize selectImage, 
    selectImageView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    selectImageView.image = selectImage;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
@end

@synthsize@propartyで宣言した変数を宣言するのを忘れずに。

watt1006
mixi
全ての人に心地よいつながりを
http://mixi.co.jp
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away