How to pass image from one page to another in ios - ios

i want to pass image from one page to another by selecting image from gallery or using camera.whe user click on display button image will be tranfered to next page
i have tried this. but not working in .m file
#interface ImagePicker () <UIImagePickerControllerDelegate>
{
IBOutlet UIImageView *imageview;
UIImageView *fordisplay;
}
#end
#implementation ImagePicker
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)clickOnDate:(id)sender
{
datePicker *date=[[datePicker alloc]initWithNibName:#"datePicker" bundle:nil];
[self.navigationController pushViewController:date animated:YES];
}
-(IBAction)selectPhoto:(id)sender
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
-(IBAction)takePhoto:(id)sender
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
DisplayImage *obj_display1=[[DisplayImage alloc]initWithNibName:#"DisplayImage" bundle:nil];
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
//self.imageview.image = chosenImage;
imageview.image=chosenImage;
obj_display1.imageDisplayy=chosenImage;**
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
-(IBAction)display:(id)sender
{
DisplayImage *obj_display=[[DisplayImage alloc]initWithNibName:#"DisplayImage" bundle:nil];
[self.navigationController pushViewController:obj_display animated:YES];
}
and in next page where want to display image
.h file
#property(nonatomic,strong) UIImageView *imageDisplayy;
.m file
#synthesize imageDisplayy;
- (void)viewDidLoad {
[super viewDidLoad];
obj_imageview.image=imageDisplayy;
}

You are creating an instance of DisplayImage in your this method
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
While performing navigation you are creating another new instance which doesn't have imageDisplayy property set for it.
-(IBAction)display:(id)sender
{
DisplayImage *obj_display=[[DisplayImage alloc]initWithNibName:#"DisplayImage" bundle:nil];
[self.navigationController pushViewController:obj_display animated:YES];
}
One solution for this problem is make DisplayImage *obj_display1 as property in your ImagePicker.
#property(nonatomic,strong) DisplayImage *obj_display1;
Instantiate it in viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.obj_display1=[[DisplayImage alloc]initWithNibName:#"DisplayImage" bundle:nil];
}
update code in this method as
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
//self.imageview.image = chosenImage;
imageview.image=chosenImage;
self.obj_display1.imageDisplayy=chosenImage;**
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Then perform navigation
-(IBAction)display:(id)sender
{
[self.navigationController pushViewController:self.obj_display1 animated:YES];
}

Your are directly passing image to imagview.Instead of that pass image from one view controller to anorther.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
DisplayImage *obj_display1=[[DisplayImage alloc]initWithNibName:#"DisplayImage" bundle:nil];
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
//self.imageview.image = chosenImage;
imageview.image=chosenImage;
obj_display1.imageDisplayy=chosenImage;**
[picker dismissViewControllerAnimated:YES completion:NULL];
}
next page where want to display image
.h file
#property(nonatomic,strong) UIImage *imageDisplayy;
.m file
#synthesize imageDisplayy;
- (void)viewDidLoad {
[super viewDidLoad];
obj_imageview.image=imageDisplayy;
}

try this :
//In .h file
// change your property to UIImage instead of imageview
#property(nonatomic,strong) UIImage *imageDisplayy;

Related

How to save the image captured using Camera and save in another ViewController?

How to save the image captured using Camera and save in another ViewController ?
I am making a Photo Gallery App. But i dont know how to save the Captured image and show it in collection view in another viewcontroller. Currently i am using UIImagePickerController to access iphone camera and library .Can anyone help me to do that.
Here is my code.
Thanks in advance
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (IBAction)takePhoto:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
- (IBAction)selectPhoto:(UIButton *)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 {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)viewDidLoad {
[super viewDidLoad];
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Device has no camera"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[myAlertView show];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Try This
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
// Save image
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
[picker release];
}
you can achieve it by adding NSNotificationcenter.
Class A - Where you select the image from camera
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
[[NSNotificationCenter defaultCenter] postNotificationName: #"GotNewImage" object: chosenImage];
}
Class B - Where you want to pass image after selection and show on collection view. Add Nsnotificationcenter for this class in Viewdidload
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(GotNewImage:) name:#"GotNewImage" object:nil];
//To get the image on Class B
-(void)GotNewImage:(NSNotification*)notification
{
UIImage * newImage = (UIImage*) notification.object;
//Now do whatever you want to do with your image...
//Add in collection view and reload the collection view
}

UIImagePickerController delegate not called when sourceType is Photo Library

I have a problem where the delegate of UIImagePickerController not being called when presented with sourceType = UIImagePickerControllerSourceTypePhotoLibrary.
Oddly enough, it works perfectly fine in camera mode.
Interface declaration:
#interface ChatVC : UIViewController <UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
And in implementation:
- (void)takePhotoAction {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController* imgPickerController = [[UIImagePickerController alloc] init];
imgPickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imgPickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
imgPickerController.allowsEditing = YES;
imgPickerController.delegate = self;
[self presentViewController:imgPickerController animated:YES completion:nil];
}
}
- (void)choosePhotoFromLibraryAction {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController* imgPickerController = [[UIImagePickerController alloc] init];
imgPickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imgPickerController.allowsEditing = YES;
imgPickerController.delegate = self;
[self presentViewController:imgPickerController animated:YES completion:nil];
}
}
And here's the delegate method implementation in the same view controller:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary<NSString *,id> *)editingInfo {
NSLog(#"Image Picker Controller Picked Image - Deprecated");
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(#"Image Picker Controller Picked Image");
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
NSLog(#"Image Picker Controller Cancelled");
[picker dismissViewControllerAnimated:YES completion:nil];
}
When opened in camera mode, everything works just fine, and I can see the appropriate delegate methods being called.
But when opened in photo library mode and I choose a photo, nothing happens and no delegate method is called. However, I can see -imagePickerControllerDidCancel: delegate method being called when I tap on Cancel button.
I even suspected memory warning somehow invalidates the delegate, but it was not the case here.
Please someone help me figure this out.
Thanks in advance!

Pass image from camera to view from storyboard

Something strange is happening with my code.
Scenario: Open camera->Take picture->Pass the image taken to a viewcontroller(from a storyboard.
The thing is that my UIimage variable from the destination view is not available, not recognized!
My code
postviewController.h
#import <UIKit/UIKit.h>
#interface postAlertViewController : UIViewController
#property (nonatomic, retain) UIImage *cameraImage;
#end
postviewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *aImageview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 60, self.view.frame.size.width,150 )];
aImageview.image=cameraImage;
[self.view addSubview:amageview];
}
Take picture and pass it to the view controller above
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:NULL];
ViewController *postView = [self.storyboard instantiateViewControllerWithIdentifier:#"postview"];
postView.cameraImage=(UIImage *)[info objectForKey:UIImagePickerControllerEditedImage];
[self.navigationController pushViewController:postView animated:YES];
}
The stroryboard ID is set to "postview".
What i am getting is:
Property 'cameraImage' not found on object of type 'ViewController *'
Why cameraImage is not available from the origin view although is declared in destination's .h file?
In another situation where i needed to pass a string between views, in same manner as above
[yourViewController setValue:mysc.title forKey:#"sendAlertTitle"];
worked ok.
Any help?
Thank you.
try this
postAlertViewController *postView = [self.storyboard instantiateViewControllerWithIdentifier:#"postview"];
and always use class and interface name start with capital
you can use this one for more ease.
- (IBAction)captureImage:(id)sender
{
if (! [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *deviceNotFoundAlert = [[UIAlertView alloc] initWithTitle:#"No Device" message:#"Camera is not available"
delegate:nil
cancelButtonTitle:#"Okay"
otherButtonTitles:nil];
[deviceNotFoundAlert show];
} else {
UIImagePickerController *cameraPicker = [[UIImagePickerController alloc] init];
cameraPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraPicker.delegate =self;
// Show image picker
[self presentViewController:cameraPicker animated:YES completion:nil];
}
}
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
UIImage *selectedImage =[info objectForKey:UIImagePickerControllerOriginalImage];
ViewControllerName *Vc=[self.storyboard instantiateViewControllerWithIdentifier:#"captioEdit"];
Vc.postImgStr=[self scaleAndRotateImage:selectedImage];
[picker dismissViewControllerAnimated:YES completion:nil];
[self.navigationController pushViewController:captioViewEdit animated:YES];
}
//Delegate method of UIImagePickerController for image picker model cancel
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}
Thanks

allowsediting move and scale doesn't work

This is my code for.m
- (IBAction)takePhoto:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
for.h
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIImageView *imageView;
- (IBAction)takePhoto: (UIButton *)sender;
However when I try to move to scale it bounces back to the center so i can't even crop anything.

dismissViewControllerAnimated issue with navigation

This is my application code , which i used for selecting the image picker ,when image picks i want to dismiss the current view controller, Since its not dismissing from current view controller at the dismissal time of image picker itself ,
-(IBAction)selectExitingPicture
{
self.imagePicker = [[GKImagePicker alloc] init];
self.imagePicker.cropSize = CGSizeMake(296, 300);
self.imagePicker.delegate = self;
self.imagePicker.resizeableCropArea = YES;
[self presentViewController:self.imagePicker.imagePickerController animated:YES completion:nil];
}
-(void)imagePicker:(GKImagePicker *)imagePicker pickedImage:(UIImage *)image
{
appDel.im=image;
[self hideImagePicker];
}
- (void)hideImagePicker
{
[self.imagePicker.imagePickerController dismissViewControllerAnimated:YES completion:^{
[self.navigationController pushViewController:viewCont animated:YES ];
}];
}
Please help me to solve this issue, What code should i want add further
You should use this line to dismiss the PickerController :
[self dismissViewControllerAnimated:YES completion:nil];
You may call this line in both delegate methods :
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
and
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
.h file
int flg;
.m file
viewDidLoad
flg=0;
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if(flg==1) {
[self dismissViewControllerAnimated:YES completion:nil];
flg=0;
}
}
- (void)hideImagePicker
{
[self.imagePicker.imagePickerController dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
self.imgView.image = image;
flg=1;
[picker dismissViewControllerAnimated:YES completion:nil];
}

Resources