[英]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.