Wont pass image to destination ViewController - ios

I'm using the library GKClass, its basically a custom imagePickerController class.
I'm having trouble passing data - the image to the next control view, while it was working with the simple imagePickerController its not working now, the ViewController doesn't get the data.
-(void)imagePicker:(GKImagePicker *)imagePicker pickedImage:(UIImage *)image
{
VCImageEditingViewController *ieVC = [[VCImageEditingViewController alloc]init];
ieVC.selectedIMG = image;
[self.imagePicker.imagePickerController dismissViewControllerAnimated:NO completion:nil];
[self performSegueWithIdentifier:#"imgrdy" sender:self];
}
and VCImageEditingViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
if (self.selectedIMG == nil) {
NSLog(#"no img set");
}
self.imageDisplayed.image = self.selectedIMG;
}
i tried it also in viewDidAppear and still nothing, the "picker" is working since iv tried :
-(void)imagePicker:(GKImagePicker *)imagePicker pickedImage:(UIImage *)image
{
[self.imagePicker.imagePickerController dismissViewControllerAnimated:NO completion:nil];
self.lastImageView.image = image;
}
It showed the image in the current view controller.
My guess is that the destination viewController is presented before the image is modified.
Would like some help here. Thank you.

Try this
-(void)imagePicker:(GKImagePicker *)imagePicker pickedImage:(UIImage *)image
{
[self.imagePicker.imagePickerController dismissViewControllerAnimated:NO completion:nil];
//pass image with peroformsegue
[self performSegueWithIdentifier:#"imgrdy" sender: image];
}
// In prepare segue method assign image to destination view controller
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"imgrdy"])
{
VCImageEditingViewController *ieVC=segue.destinationViewController;
ieVC.selectedIMG =(UIImage *) sender;
}
}

Related

Send Captured Image to another view with prepareforSgue

So when I press captured button, it's taking a photo and I can save the image in my camera roll. But when I want to see image in another view I can't see it.
This is my code :
- (IBAction)captureButton:(id)sender
{
[self.cameraViewController captureImageWithCompletionHander:^(id data) {
self.image = ([data isKindOfClass:[NSData class]]) ? [UIImage imageWithData:data] : data;
UIImageWriteToSavedPhotosAlbum(self.image, nil, nil, nil);
[self performSegueWithIdentifier:#"savingSegue" sender:self];
}];
}
And this is the prepare for segue method.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"savingSegue"]) {
PhotoSaveViewController *pvc = [[PhotoSaveViewController alloc] init];
[pvc.imageView setImage:self.image];
}
}
It sounds like your destination view controller is a UINavigationController. I'm guessing its top view controller is the one you want. You can access it like this (though you may wish to add type checking):
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"savingSegue"]) {
UINavigationController *navigationController = segue.destinationViewController;
PhotoSaveViewController *pvc = (PhotoSaveViewController *)navigationController.topViewController;
[pvc.imageView setImage:self.image];
}
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"savingSegue"])
{
YourNextViewController*ImageView=segue.destinationViewController;
UIImage*clickedImage=capturedPicImageView.image;
ImageView.passedImage=clickedImage;
// NSLog(#"%#",editView.editPhotoImgView.image);
}
}
(And in YourNextViewController create UIImage*clickedImage and give property to it. and dont forget to allocate in viewDidLoad. Hope this helps.

Showing a view controller from a parallel window (with the same parent)

I am trying to show a ViewController directly from its sibling window, the tree looks kinds like this: CalibrationVC<--- MainVC ---> Settings VC. The MainVC presents the other two modally, and over current context. Now what I want to do is click a button in SettingsVC, that would open the CalibrationVC for a specific device. I have managed to do so using unwind segues and a delegate from the SettingsVC, and it looks like this:
- (void)showViewForDeviceCalibration
{
[self performSegueWithIdentifier:#"showCalibrationViewFromSettings" sender:nil];
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"showCalibrationViewFromSettings"])
{
[_delegate calibrateDevice:deviceToConfigure];
}
}
The delegate implementation:
-(void)calibrateDevice:(Device *)device
{
dispatch_block_t autoinitService =
^{
deviceToCalibrateFromSettings = device;
[NSThread sleepForTimeInterval:0.2];
dispatch_async(dispatch_get_main_queue(), ^{
[self performSegueWithIdentifier:#"showCalibrationViewForDeviceFromSettings" sender:nil];
});
};
dispatch_async(dispatch_get_global_queue(QOS_CLASS_UTILITY, 0), autoinitService);
}
And the prepare for segue method:
- (void)prepareForSegue: (UIStoryboardSegue *)segue sender:(id) sender
{
if ([segue.identifier isEqualToString:#"showCalibrationViewForDeviceFromSettings"])
{
CalibrationViewController *destinationController = (CalibrationViewController*)segue.destinationViewController;
NSArray *devicesToCalibrate = [NSArray arrayWithObject:deviceToCalibrateFromSettings];
[destinationController setDevicesToCalibrate: devicesToCalibrate];
}
This works well, but is there a better way to do that? I am really concerned about the delegate implementation because it uses a separate thread only to wait for a moment and then use the main thread again. I had to do this because without it the CalibrationVC would not appear saying that the MainVC is already presenting. So to sum thing up, is there a better, more optimal/proper way to do this?
If you don't specifically need unwind segues, you could do something like this:
Settings VC:
- (void)showViewForDeviceCalibration
{
[_delegate calibrateDevice:deviceToConfigure];
}
Delegate:
-(void)calibrateDevice:(Device *)device
{
[self dismissViewControllerAnimated:YES completion:^{
[self performSegueWithIdentifier:#"showCalibrationViewForDeviceFromSettings" sender:nil];
}];
}

Trouble setting UIImageView image from another view controller

The first view controller calls this method that sends the image
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
...
[wallPostCreateViewController viewDidLoad];
[wallPostCreateViewController selectImage:wallPostCreateViewController.myImage];
NSLog(#"FROM PAWWallViewController %#",wallPostCreateViewController.myImage); //Image is there
}
I receive the image in the second view controller but it fails to put the image into its UIImageView.
- (void)selectImage:(UIImage *)img
{
if (self.condition)
{
NSLog(#"View loaded!"); //is loaded
[self selectImageNow:img];
self.condition = NO;
}
else
NSLog(#"View did NOT load!");
}
- (void)selectImageNow: (UIImage *)img
{
NSLog(#"Image passed %#", img); // This is the UIImage being passed.
self.imageView = [[UIImageView alloc] initWithImage:img];
self.imageView.image = img;
NSLog(#"Image in view %#", self.imageView.image); //has value
NSLog(#"Imageview %#", self.imageView); //has value
}
UPDATED:
The code above is updated now. All of the logs now have values but I still see no image or image view when I run the app
The issue seems like your imgView is nil. So make sure that your method should not get called, before your view has been loaded.
Send UIImage object from one to another ViewController then assign that UIImage to UIImageViewin ViewWillAppear OR ViewDidLoad method of SecondController.
I think you need to set the imageView of first view controller in prepareForSegue method that is in the second view controller, where you will have your destination to be the first view controller, do something like this in the second view controller:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"firstVwCtrl"]){
FirstViewController *frstVwCtrl = [segue destinationViewController];
[frstVwCtrl setImgGot:_photoView.image];
} }

Segue between view controllers not functioning

I have an app that allows the user to take a picture, and when the picture is taken (push action) should then navigate to another view controller to allow the user to discard, edit, or save the photo. I try to segue from the camera view controller in the didFinishPickingMediaWithInfo method, like so:
- (void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info {
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
UIImage *originalImage, *editedImage, *imageToSave;
// Handle a still image capture
if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo) {
editedImage = (UIImage *) [info objectForKey:
UIImagePickerControllerEditedImage];
originalImage = (UIImage *) [info objectForKey:
UIImagePickerControllerOriginalImage];
if (editedImage) {
imageToSave = editedImage;
} else {
imageToSave = originalImage;
}
// Save the new image to camera roll (will change later)
UIImageWriteToSavedPhotosAlbum (imageToSave, nil, nil , nil);
[self.capturedImages addObject: imageToSave];
_imageView.image = [self imageToSave:imageToSave];
}
CaptureViewController *capture = [[CaptureViewController alloc] init];
[self performSegueWithIdentifier:#"takePic" sender:self];
}
The didFinishPickingMediaWithInfo method is called when a button is pressed, prompting [_picker takePicture]; Here is my prepare for segue method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller
//this is just a temporary fix
if ([segue.identifier isEqualToString:#"takePic"]) {
CaptureViewController *destViewController = segue.destinationViewController;
}
}
The segue identifier is "takePic", so that's not the issue. That would be too simple. When I try to perform the segue, nothing happens. The camera still shows and the two buttons I have in the imagePickerController's cameraOverlayView are still there (although they are non-functional). Help is much appreciated!
I don't see where you're dismissing the image picker controller. UIImagePickerController is pretty picky about how you present it. So, make sure that you're presenting it the way it expects to be presented. When the image is captured or selected, you also need to dismiss the image picker controller. Since you don't see it going away, I strongly suspect that you're not dismissing it anywhere.
Add a line like:
[picker dismissViewControllerAnimated:YES completion:nil];
to your ... didFinishPickingMediaWithInfo: method.

Fails to load image from UIImagePickerController on next ViewController after segue

I am trying to load a picture selected on UIImagePickerController on the next view controller. i did some search on similar threads and found a few that helped me set it up, but the image does not really get 'transfered' to the next view controller.
this is my code for the didFinishPickingImage and prepareForSegue:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
self.imageChosen = image;
[picker dismissModalViewControllerAnimated:NO];
[self performSegueWithIdentifier:#"Edit" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"Edit"]){
EditViewController *editViewController =
(EditViewController *)segue.destinationViewController;
editViewController.imageView.image = self.imageChosen;
}
I am getting the segue running to the EditViewController, but the imageView there doesnt load the picture. I guess the assignment is wrong somehow but I fail to see how.
When you try to set image to EditViewController UIImageView it doesn't not exist because EditViewController not loaded yet. Instead of setting image to UIImageView directly create an instance variable in EditViewController that will hold image. And than assign this image to UIImageView in viewDidLoad:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"Edit"]){
EditViewController *editViewController =
(EditViewController *)segue.destinationViewController;
editViewController.image = self.imageChosen;
}
//EditViewController.h
...
#property (nonatomic, strong) UIImage *image;
...
//EditViewController.m
- (void)viewDidLoad {
...
self.imageView.image = self.image;
...
}

Resources