简体   繁体   English

如何将图像从uiimagepickercontroller传递到另一个场景的uiimageview

[英]How to pass image, from uiimagepickercontroller, into another scene's uiimageview

How do I save a UIImagePickerController camera image so I can send to CreateViewController scene's UIImageView ? 如何保存UIImagePickerController摄像机图像,以便可以发送到CreateViewController场景的UIImageView

HomeViewController (scene 1) has a button that loads UIImagePickerController and returns with an image from the camera. HomeViewController (场景1)具有一个按钮,该按钮加载UIImagePickerController并返回相机中的图像。 CreateViewController (scene 2) has an empty UIImageView . CreateViewController (场景2)的UIImageView为空。

HomeViewController.h HomeViewController.h

#import "CreateViewController.h"

@interface HomeViewController : UIViewController
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

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

- (IBAction)cameraImage:(id)sender;

HomeViewController.m HomeViewController.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:^{}];
}

CreateViewController.h CreateViewController.h

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

CreateViewController.m CreateViewController.m

@implementation CreateViewController
@synthesize bgImage, myImage;

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

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

Using a segue is not mandatory if you are instanciating the controller programmatically. 如果以编程方式实例化控制器,则不必强制使用segue。

The easiest way to achieve that is something like 最简单的方法是

-(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];
}

If you want to use a segue you need to manually invoke it by doing 如果要使用segue,则需要通过以下方式手动调用它

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

then you can use an ivar in your HomeViewController to store the pickedImage and pass it to your destinationViewController . 那么您可以在HomeViewController中使用一个ivar来存储pickedImage并将其传递给destinationViewController

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

where _pickedImage is the ivar where you stored the image after it has been picked. 其中_pickedImage是在选择图像后将图像存储在其中的ivar。

On ur first view 在您的第一眼

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

XXXXX is identifier of view. XXXXX是视图的标识符。

First you have to be sure that your delegate method is called and that you have the image. 首先,您必须确保调用了您的委托方法,并且您具有图像。 Next, store the image to a property: 接下来,将图像存储到属性:

- (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];   
    }];

}

After that, this method's being called: 之后,此方法被调用:

- (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;
}

in this code: 在此代码中:

    - (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];

      }];
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据从UIImagePickerController传递到UIImageView - How to pass data from a UIImagePickerController to a UIImageView 将图像从UIImagePickerController传递到另一个ViewController - Pass image from UIImagePickerController to another ViewController 如何将UIImage从UIImagePickerController传递到另一个UIViewController? - How to pass an UIImage from UIImagePickerController to another UIViewController? UIImageView不显示来自UIImagePickerController的图像 - UIImageView does not show Image from UIImagePickerController 如何从另一个视图设置UIImageView的图像 - How to set UIImageView's image from another View UIImagePickerController不将图像保存到UIImageView - UIImagePickerController not saving image to UIImageView UIImagePickerController没有将图像分配给uiimageview - UIImagePickerController not assigning image to uiimageview 从另一个ViewController中的&#39;UIImagePickerController&#39;更改UIImageView Image会崩溃,并显示&#39;致命错误:意外发现nil&#39; - Changing UIImageView Image from a 'UIImagePickerController' in another ViewController crashes with 'fatal error: unexpectedly found nil' 如何使用Tap Gesture从UIImageView呈现UIImagePickerController - How to present UIImagePickerController from UIImageView with Tap Gesture 如何将从UIImagePickerController拾取的两个图像传递到另一个ViewController - How to pass two images picked from UIImagePickerController to another ViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM