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で宣言した変数を宣言するのを忘れずに。