13
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Posted at

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

13
13
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?