4

如何保存UIImagePickerController相机图像以便发送到CreateViewController场景UIImageView

HomeViewController(场景 1)有一个按钮,用于加载UIImagePickerController和返回来自相机的图像。CreateViewController(场景 2)有一个空的UIImageView.

HomeViewController.h

#import "CreateViewController.h"

@interface HomeViewController : UIViewController
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (strong, nonatomic) UIImagePickerController *imagePicker;
@property(nonatomic, retain) UIImage *myImage;

- (IBAction)cameraImage:(id)sender;

主视图控制器.m

@implementation HomeViewController
@synthesize imagePicker, myImage;

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ( [segue.identifier isEqualToString:@"create"]) {
    CreateViewController *cvc = [segue destinationViewController];
    UIImage *image = myImage;
    cvc.myImage = image;

}
}

//Camera button action
- (IBAction)cameraImage:(id)sender{
//UIImagePickerController space in memory
imagePicker = [[UIImagePickerController alloc]init];
//Set the delegate
imagePicker.delegate = self;
//Set the sourceType
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
//Show Image Picker UI
[self presentViewController:imagePicker animated:YES completion:^{}];
}

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:    (NSDictionary *)info {

self.myImage = [info objectForKey:UIImagePickerControllerOriginalImage];

[self dismissViewControllerAnimated:YES completion:^{}];

[self performSegueWithIdentifier:@"create" sender:self];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:^{}];
}

创建视图控制器.h

@property (strong, nonatomic) IBOutlet UIImageView *bgImage;
@property(nonatomic, retain) UIImage *myImage;

创建视图控制器.m

@implementation CreateViewController
@synthesize bgImage, myImage;

- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *image = myImage;
[bgImage setImage:image];
}

在此处下载示例代码;http://code-blind.com/ios6-camera-picture-to-another-scenes-uiimageview/

4

4 回答 4

5

如果您以编程方式实例化控制器,则不需要使用 segue。

实现这一目标的最简单方法是

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage * pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    DisplayViewController * controller = [DisplayViewController new];
    controller.imageView.image = pickedImage;
    [self.navigationController pushViewController:controller animated:YES];
}

如果你想使用 segue,你需要手动调用它

[self performSegueWithIdentifier:@"your-segue-name" sender:self];

然后您可以在 HomeViewController 中使用 ivar 来存储并将其pickedImage传递给您的destinationViewController.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {    
    DisplayViewController * controller = (DisplayViewController *)segue.destinationViewController;
    controller.imageView.image = _pickedImage;
}

_pickedImage选择后存储图像的 ivar 在哪里。

于 2012-12-11T00:28:10.427 回答
1

在你的第一眼

  - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    CreateViewController* sI = [self.storyboard instantiateViewControllerWithIdentifier:@"XXXXX"];
    sI.myImage = image;
}

XXXXXX 是视图的标识符。

于 2012-12-12T11:43:07.460 回答
0

首先,您必须确保调用了您的委托方法并且您拥有图像。接下来,将图像存储到属性中:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {


    self.takenImage = image; // assuming you have declared @property(nonatomic, strong) UIImage *takenImage;
    [picker dismissViewControllerAnimated:YES completion:^{

        [self performSegueWithIdentifier:@"my-segue" sender:self];   
    }];

}

之后,这个方法被调用:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // you need to set the picked image to the controller:

    DisplayViewController *controller; // get the controller
    controller.image = self.takenImage;
}
于 2012-12-11T00:10:16.680 回答
0

在这段代码中:

    - (void)imagePickerController:(UIImagePickerController *)picker     

      didFinishPickingMediaWithInfo:(NSDictionary *)info
     {
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];


 if( i ==1)
 {

     [self dismissViewControllerAnimated:YES completion:^
      {
          Second_ViewController*secondview=[[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"Second_ViewController"];
          secondview.bb_image=image;
          [[NSUserDefaults standardUserDefaults ]setInteger:0 forKey:@"123"];

          [[self navigationController] pushViewController:secondview animated:YES];

      }];
 }
于 2017-06-05T12:25:47.637 回答