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];
}];
}
Related
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)
I'll post my code first and then I'll explain the problem
RootViewController.h its called SpViewController.h
#import <UIKit/UIKit.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import "secondViewController.h"
#interface spViewController : UIViewController
<UIImagePickerControllerDelegate,
UINavigationControllerDelegate>
#property BOOL newMedia;
#property (strong, nonatomic) IBOutlet UIImageView *imageView;
#property (strong, nonatomic) spViewController *secondViewController;
#property (strong, nonatomic) UIImageView *image;
#property (strong, nonatomic) UIImage *myImage;
- (IBAction)useCamera:(id)sender;
- (IBAction)useCameraRoll:(id)sender;
#end
spViewController.m
#import "spViewController.h"
#import "secondViewController.h"
#interface spViewController ()
#end
#implementation spViewController
- (IBAction)backtohome:(UIStoryboardSegue *)unwindSegue
{
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void) viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)useCamera:(id)sender {
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = #[(NSString *) kUTTypeImage];
imagePicker.allowsEditing = NO;
[self presentViewController:imagePicker
animated:YES completion:nil];
_newMedia = YES;
}
}
- (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];
_newMedia = NO;
}
}
//image picker delegate
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *chosenImage = info[UIImagePickerControllerEditedImage]; self.myImage = chosenImage;
NSString *mediaType = info[UIImagePickerControllerMediaType];
[self performSelector:#selector(myMethod:) withObject:info afterDelay:0.1];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image = info[UIImagePickerControllerOriginalImage];
_imageView.image = image;
if (_newMedia)
UIImageWriteToSavedPhotosAlbum(image,
self,
#selector(image:finishedSavingWithError:contextInfo:),
nil);
}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
}
}
-(void)myMethod:(NSDictionary *)info {
[self dismissViewControllerAnimated:YES completion:^{
NSLog(#"Perform segue");
[self performSegueWithIdentifier:#"Picture Unwind Segue" sender:self];
}];
}
-(void)image:(UIImage *)image
finishedSavingWithError:(NSError *)error
contextInfo:(void *)contextInfo
{
if (error) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: #"Save failed"
message: #"Failed to save image"
delegate: nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
//cancel delegate
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier]isEqualToString:#"Picture Unwind Segue"]) {
secondViewController *destinationViewController = [segue destinationViewController];
destinationViewController.imageView.image = self.myImage;
}
}
#end
secondViewController.h
#import <UIKit/UIKit.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import "spViewController.h"
#interface secondViewController : UIViewController
<UIImagePickerControllerDelegate,
UINavigationControllerDelegate>
#property (strong, nonatomic) IBOutlet UIImageView *imageView;
#property (strong, nonatomic) UIImage *myImage;
#property (strong, nonatomic) UIImageView *image;
#end
secondViewController.m
#import "secondViewController.h"
#import "spViewController.h"
#interface secondViewController ()
#end
#implementation secondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
self.imageView.image = self.myImage;
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#end
So the problem I'm having is, I can't get the image that has just been selected in the UIImagePickerController to be displayed in the UIImageView, my ImagePickerController was initiated by a button in my previous view controller "spViewController" (rootViewController) I am using Storyboards, I thought if I put in the UIImageView then connected it using an outlet reference it would display there, this doesn't seem to be the case though. If I put the UIImageView in the first view controller and do the same it will display the image but thats not where I want it to go.
Thank you for any help
To fix my problem i decided to save the image and call on it later heres the code i put into my secondViewController.m
{NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filepathJPG = [docsPath stringByAppendingPathComponent:#"imagefile.jpg"];
UIImage *img = [UIImage imageWithContentsOfFile: filepathJPG];
if (img != nil) {
// assign the image to the imageview,
_imageView.image = img;
// Optionally adjust the size
BOOL adjustToSmallSize = YES;
CGRect smallSize = (CGRect){0,0,100,100};
if (adjustToSmallSize) {
_imageView.bounds = smallSize;
}
}
I hope this can help others that have the same problem.
So according to your code , you are setting the property of myImage which is an instance of UIImage from the RootViewController.
so your just passing the data of the image , But your not setting your imageView.image property to the passed image value.
So a possible fix would be to set
imageView.image = myImage in viewDidLoad
or
you can directly access the imageView property from the perform prepare for segue method in the RootViewController and set its image property.
I hope this helps .
Cheers
You can declare a property in AppDelegate class and access it across all controllers using appdelegate shared instance
I am trying to have a button that is meant to open the camera and a button that is meant to open camera roll. Here is my code:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (IBAction)getCameraPicture:(id)sender{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.allowsEditing = NO;
picker.sourceType = (sender == takePictureButton) ?
UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
- (IBAction)selectExistingPicture {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.allowsEditing = NO;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Sorry" message:#"Device does not support photo library" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alert show];
}
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
imageView.image = image;
}
- (void)viewDidLoad
{
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
takePictureButton.hidden = YES;
}
[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
Any ideas on why it's not working? Everything is linked but the buttons do nothing
Thanks
EDIT
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UIImagePickerControllerDelegate> {
IBOutlet UIButton *takePictureButton;
IBOutlet UIImageView *imageView;
}
#property (nonatomic, retain) IBOutlet UIImageView *imageView;
#property (nonatomic, retain) IBOutlet UIButton *takePictureButton;
- (IBAction)getCameraPicture:(id)sender;
- (IBAction)selectExistingPicture;
#end
Just add :
[self presentViewController:picker animated:YES completion:nil];
at the end of your 'getCameraPicture:' and in your 'selectExistingPicture:'.
And you also might want to add the 'UIImagePickerControllerDelegate' to your view controller's interface so the 'didFinishPickingImage' gets called.
Edit :
You should replace your :
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo;
with
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
beccause that method is long deprecated.
Use it like this :
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Code from http://www.appcoda.com/ios-programming-camera-iphone-app/.
-(IBAction)TakePhoto {
picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentedViewController:picker animated:YES completion:nil];
}
- (IBAction)ChooseExisting {
picker2 = [[UIImagePickerController alloc]init];
picker2.delegate = self;
[picker2 setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentedViewController:picker2 animated:YES completion:nil];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
image = [info objectForKey:UIImagePickerControllerOriginalImage];
[imageview setImage:image];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:NULL];
When I try to run this app I the
[self presentedViewController:picker animated:YES completion:nil];
Gets highlighted in red saying No visible #interface for ViewController Declares the sector presentedviewcontroller:animated :completion :
My .h file is
#interface ViewController : UIViewController <UIImagePickerControllerDelegate,
UINavigationControllerDelegate> {
UIImagePickerController *picker;
UIImagePickerController *picker2;
UIImage *image;
IBOutlet UIImageView *imageview;
}
- (IBAction)TakePhoto;
- (IBAction)ChooseExisting;
You have a typo: presentedViewController instead of presentViewController on the line
[self presentedViewController:picker animated:YES completion:nil];
(in more than one place)
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.