I am trying to pass UIImage from one Viewcontroller to another in the same way i have passed NSString but in case of image it is getting nil value in second controller.I have used imagepickercontroller to select image from gallery in first controller.
This is InfoViewcontroller.h-
#import <UIKit/UIKit.h>
#interface InfoViewController : UIViewController
#property(strong,nonatomic) UIImagePickerController *imagepicker;
#property(strong,nonatomic) UIImage *image1;
#end
This is InfoViewcontroller.m-
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSData *data=UIImageJPEGRepresentation([info objectForKey:#"UIImagePickerControllerOriginalImage"], 1);
image1=[[UIImage alloc]initWithData:data];
//[imgview1 setImage:image1];
[imagepicker dismissViewControllerAnimated:YES completion:nil];
[self performSegueWithIdentifier:#"tocrop" sender:self];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"toimage"]) {
ThirdViewController *destViewController = segue.destinationViewController;
destViewController.image2 =image1;}
}
This is ThirdViewController.h-
#import <UIKit/UIKit.h>
#interface ThirdViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIImageView *imgview2;
#property (strong,nonatomic) UIImage *image2;
#end
This is ThirdViewController.m-
-(void)viewDidLoad {
[super viewDidLoad];
//if(image2==nil) NSLog(#"abab");
[imgview2 setImage:image2];
}
Why am I getting nil?
In here
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSData *data=UIImageJPEGRepresentation([info objectForKey:#"UIImagePickerControllerOriginalImage"], 1);
image1=[[UIImage alloc]initWithData:data];
//[imgview1 setImage:image1];
[imagepicker dismissViewControllerAnimated:YES completion:nil];
[self performSegueWithIdentifier:#"tocrop" sender:self];
}
You are performing segue with "ToCrop" where as here you checking to toimage
if ([segue.identifier isEqualToString:#"toimage"])
I think it should be if ([segue.identifier isEqualToString:#"tocrop"]) and also use self.image1=[[UIImage alloc]initWithData:data];
My friend your segue identifiers are not the same and there for your code inside the if statement in the prepare for segue is not executed.
try something like this:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSData *data=UIImageJPEGRepresentation([info objectForKey:#"UIImagePickerControllerOriginalImage"], 1);
image1=[[UIImage alloc]initWithData:data];
//[imgview1 setImage:image1];
[imagepicker dismissViewControllerAnimated:YES completion:nil];
[self performSegueWithIdentifier:#"tocrop" sender:self];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"tocrop"]) {
ThirdViewController *destViewController = segue.destinationViewController;
destViewController.image2 =image1;}
}
Cheers
1.Make sure image1 is not nil after initWithData.
2.Check If ViewDidLoad execute before destViewController.image2 =image1(sometimes it will)
Related
OK, I am a NOOB with iPhone and have a simple question I assume. I have found a lot of help out there but am not sure what I am doing wrong. I simply want to take a photo with the camera then move to the next view controller after successful capture and place it in an image view. Got most of this code from here so thanks already but can not seem to get it to work, all different kinds of errors I do not understand. I think it must be that I am killing the view controller then trying to re-instantiate it but am a little lost Please Help.
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (IBAction)TakePhoto:(id)sender{
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:picker animated:YES completion:NULL];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
image = [info objectForKey:UIImagePickerControllerOriginalImage];
[imageView setImage:image];
[self dismissViewControllerAnimated:YES completion:NULL];
NSString *submit = #"viewControllerSubmit";
UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:submit bundle:nil];
UIViewController *vc = [mainstoryboard instantiateViewControllerWithIdentifier:submit];
[self presentViewController:vc animated:YES completion:nil];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
As you can see I have tried a few things. I have tried searching for the errors but tried other things to no avail. Any assistance would be great, thanks in advance.
EDITED VERSION and FULL CODE BELOW HERE
====================================================================
OK, I am posting my full code. I can get the form to go from one to the next as I was simply calling the storyboard name wrong. However I can not get the image I take from the camera to show up on the next form. All I get is an empty ImageView without any errors. What am I missing? Please help been working on this for days and have researched a ton.
First View Controller .M File
#import "ViewController.h"
#import "ViewControllerSubmit.h"
#interface ViewController ()
#end
#implementation ViewController{
UIImage *newImage;
}
- (IBAction)TakePhoto:(id)sender{
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:picker animated:YES completion:nil]; //nil from Null
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"ViewControllerSubmit"]){
ViewControllerSubmit *destViewController = segue.destinationViewController;
destViewController.theNewImage = newImage;
}
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
newImage = [info objectForKey:UIImagePickerControllerOriginalImage];
[self dismissViewControllerAnimated:YES completion:NULL];
NSString *submit = #"ViewControllerSubmit";
NSString *main = #"Main";
UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:main bundle:nil];
UIViewController *vc = [mainstoryboard instantiateViewControllerWithIdentifier:submit];
[self presentViewController:vc animated:YES completion:nil];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
First View Controller .H File
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>{
UIImagePickerController *picker;
}
#end
Second View Controller .M File
#import "ViewControllerSubmit.h"
#interface ViewControllerSubmit ()
#property (weak, nonatomic) IBOutlet UIImageView *_image;
#end
#implementation ViewControllerSubmit
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self._image.image = self.theNewImage;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Second View Controller .H File
#import "ViewController.h"
#interface ViewControllerSubmit : ViewController
#property (strong, nonatomic) UIImage *theNewImage;
#end
Again, no error, the camera comes up, it takes the picture and when I click 'Use Photo' the second View Controller pops up but the image view does not show the image captured.
What am I missing? Any help would be great and again please forgive the ignorance, I am new to iPhone.
You need to write navigation code in completion block...
Try below code,
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
image = [info objectForKey:UIImagePickerControllerOriginalImage];
[imageView setImage:image];
[picker dismissViewControllerAnimated:YES completion:^{
NSString *submit = #"viewControllerSubmit";
UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:submit bundle:nil];
UIViewController *vc = [mainstoryboard instantiateViewControllerWithIdentifier:submit];
[self presentViewController:vc animated:YES completion:nil];
}];
}
I'm trying to choose an image from the camera roll in a view controller and then pass it/show that image in another view controller. Why doesn't the image view get passed?
When landing on FirstViewController i have a segue in the navigation bar to the SecondViewController.
//FirstViewController.h
#import <UIKit/UIKit.h>
#interface FirstViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIImageView *imageView;
#end
//FirstViewController.m
#import "FirstViewController.h"
#import "SecondViewController.h"
#interface FirstViewController () <SeconViewControllerDelegate>
#end
#implementation FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark - SecondViewControllerDelegate
- (void)didDismissWithImageView:(UIImageView *)imageView
{
self.imageView = imageView;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"addPhoto"]) {
SecondViewController *secondVC = segue.destinationViewController;
secondVC.delegate = self;
}
}
#end
//SecondViewController.h
#import <UIKit/UIKit.h>
#import <MobileCoreServices/MobileCoreServices.h>
#protocol PhotoPickerDelegate <NSObject>
- (void)didDismissWithImageView:(UIImageView *)imageView;
#end
#interface SecondViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
#property (weak, nonatomic) id<PhotoPickerDelegate> delegate;
#property (strong, nonatomic) UIImageView *imageView;
- (IBAction)useCameraRoll:(id)sender;
#end
//SecondViewController.m
#import "SecondViewController.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (IBAction)useCameraRoll:(id)sender
{
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeSavedPhotosAlbum]){
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = #[(NSString *) kUTTypeImage];
imagePicker.allowsEditing = NO;
[self presentViewController:imagePicker animated:YES completion:nil];
}
}
#pragma mark - UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = info[UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:YES completion:nil];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image = info[UIImagePickerControllerOriginalImage];
_imageView.image = image;
}
if ([self.delegate respondsToSelector:#selector(didDismissWithImageView:)]) {
[self.delegate didDismissWithImageView:_imageView];
}
[self.navigationController popViewControllerAnimated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
If its just an image we are talking about here then lets make a property for the image.
#property (nonatomic, strong) UIImage *imageIWant;
Once we have our image property, we can set the image that you want by either calling [UIImage imageWithData:imageData]; or set it from a file within our Plist. I do not recommend saving an imageView and passing that view among controllers because within our MVC model, the view and the model are separate. At the end of the day, its an image within the model that you want not a view element.
I'm trying to write an extremely basic Xcode (ver. 5) project application for the iPhone5 that loads an image from the photo library into an UIImageView in the start up UIViewController and keeps that image in the UIImageView (or returns it) when switching between other view controller panels. The problem I am having is that when I return from other UIViewController windows to the main view controller with my UIImageView, the loaded image that was previously displayed is gone and I end up having to reload it into the UIImageView. How can I make this behave where the image remains in the UIImageView after I return from other UIViewController windows?
Implementation File...
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (IBAction)btnClick:(id)sender
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
modBuffer = info[UIImagePickerControllerEditedImage];
self.imageView.image = modBuffer;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#end
Header file...
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
UIImage* modBuffer;
}
#property (strong, nonatomic) IBOutlet UIImageView* imageView;
- (IBAction)btnClicked:(id)sender;
I think you need to make property for UIImage (nonatomic,retain) and then u should try to do it.
I used the code below in iOS to pick an image from the photo library. I need to display this image in different view. How should I proceed?
//delegate methode will be called after picking photo either from camera or library
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:NO completion:nil];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSArray *theViewControllers = [self.tabBarController viewControllers];
[imgview setImage:image]; //set in image view
[self performSegueWithIdentifier:#"NextView" sender:nil];
}
I tried the below code to display the image in next view but it is not working??
//delegate methode will be called after picking photo either from camera or library
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self dismissViewControllerAnimated:NO completion:nil];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSArray *theViewControllers = [self.tabBarController viewControllers];
//[imgview setImage:image]; //set in image view
ImageViewController *adjViewController = (ImageViewController *)[theViewControllers objectAtIndex:0];
[adjViewController.imageview1 setImage :image];
[self performSegueWithIdentifier:#"NextView" sender:nil];
}
You could make the UIImage *image (from your imagePickerController method) global to its view controller and then pass it prepareForSegue.
For example:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"NextView"]) {
ImageViewController *vc = [segue destinationViewController];
[vc setImage:image];
}
}
Then in your ImageViewController.h you just need something like:
#interface ImageViewController : UIViewController {
UIImage *image;
}
#property (nonatomic, retain) UIImage *image;
And synthesise it in the .m and set the image to your imageView in viewWillAppear. You can read the docs for prepareForSegue here.
How do I save a UIImagePickerController camera image so I can send to CreateViewController scene's UIImageView?
HomeViewController(scene 1) has a button that loads UIImagePickerController and returns with an image from the camera. CreateViewController(scene 2) has an empty 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;
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
#property (strong, nonatomic) IBOutlet UIImageView *bgImage;
#property(nonatomic, retain) UIImage *myImage;
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/
Using a segue is not mandatory if you are instanciating the controller programmatically.
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
[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.
- (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.
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.
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];
}];
}