Send captured image to another UIViewController with prepareForSegue - ios

I'm trying to send the captured image to another UIViewController.
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];
}
}

If your second view is a PhotoSaveViewController then replace your prepareForSeque method with this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"savingSegue"]) {
PhotoSaveViewController *pvc = (PhotoSaveViewController *)segue.destinationViewController;
if (pvc)
[pvc.imageView setImage:self.image];
}
}
Indeed, it's not your job to instantiate the destination UIViewController, your UINavigationController have already done that.

I guess that imageView on PhotoSaveViewController it is outlet and it is not set at this moment. You should declare there some other property like
#property (nonatomic, retain) UIImage *inputImage;
and assign to that in prepareForSegue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"savingSegue"]) {
PhotoSaveViewController *pvc = segue.destinationViewController;
pvc.inputImage = self.image;
}
}
Then do the following in the PhotoSaveViewController:
- (void)viewDidLoad {
[super viewDidLoad];
self.imageView.image = self.inputImage;
}
Another approach you can create it from code totally. Assign Storyboard ID to the PhotoSaveViewController and then do the following:
- (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);
PhotoSaveViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:#"PhotoSaveViewControllerID"];
dvc.inputImage = self.image;
[self.navigationController pushViewController:dvc animated:YES];
}];
}

Related

UIImage and prepareforSegue not able to pass the image

Here my code :
#property (weak, nonatomic) IBOutlet UIImageView *chosedImage;
- (IBAction)cropImageTap:(id)sender {
[self dismissViewControllerAnimated:YES completion:^{
UIImage * pickedImage = self->_chosedImage;
//[UIImage imageNamed:#"helloWorld.jpg"];
[self performSegueWithIdentifier:#"gotomainimage" sender:pickedImage];
}];
}
And my perform segue is below :
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
PostVC *vc = (PostVC*)segue.destinationViewController;
UIImage *img = (UIImage*)sender;
if (img.images == nil) {
NSLog(#"null img");
vc.Sourcemage = img;
}
else {
NSLog(#"not null img");
vc.Sourcemage = img;
}
}
I am getting null image only.My image is not coming.What the issue i am doing here ?
Please help me out
UPDATED :
My updated code, work like - giving me console message like :
its null dataa - post vc
its not an null image - prepareForSegue method
Not able to find out why ist happening.
- (IBAction)cropImageTap:(id)sender {
UIImageView * pickedImage = _chosedImage;
[self dismissViewControllerAnimated:YES completion:^{
[self performSegueWithIdentifier:#"gotomainimage" sender:pickedImage.image];
}];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
PostVC *vc = (PostVC*)segue.destinationViewController;
UIImage *img = (UIImage*)sender;
if (img != nil) {
NSLog(#"its not an null image");
vc.Sourcemage = img;
} else {
NSLog(#"its an null image");
}
}
in my postvc :
#property(nonatomic) UIImage *Sourcemage;
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (self.Sourcemage != nil){
NSLog(#"not null dataa");
_PostImageView.image = _Sourcemage;
} else {
NSLog(#"not null dataa");
_PostImageView.image = [UIImage imageNamed:#"Travel.png"];
}
}
Is the variable Sourcemage in your destination viewcontroller(PostVC) is UIImage?
Please check whether the property is UIImage or not.
This is because you are passing UIImageView instead of UIImage
Update your code as below,
UIImageView * pickedImage = _chosedImage;
[self dismissViewControllerAnimated:YES completion:^{
[self performSegueWithIdentifier:#"gotomainimage" sender:pickedImage.image];
}];
in above code, you must pass UIImage not UIImageView itself and handle it like this,
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
PostVC *vc = (PostVC*)segue.destinationViewController;
UIImage *img = (UIImage*)sender;
vc.Sourcemage = img;
}
Why don't you just do like this?
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
PostVC *vc = (PostVC*)segue.destinationViewController;
vc.Sourcemage = _checkedImage;
}

No responed after pressing UICollectionViewCell in the UICollectionViewController

when I come the part of sending image from UICollectionViewController to another ViewController here is View A:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"Detail Segue"])
{
PhotoDetailViewController *PhotoDetailVC = segue.destinationViewController;
NSIndexPath *path = [[self.collectionView indexPathsForSelectedItems] lastObject];
Photo *selectedPhoto = self.photos[path.row];
PhotoDetailVC.photo = selectedPhoto;
}
}
and I receive it in the ViewController (View B) using:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.imageView.image= self.photo.image;
}
I think the problem is that you didn't call performSegueWithIdentifier:sender: in any place in code which actually responsible for triggering navigation not prepareForSegue:sender: which is called when preparing for the navigation
so I suggest to implement collectionView:didSelectItemAtIndexPath: as the following
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
Photo *selectedPhoto = self.photos[path.row];
// this line is the one that is responsible for navigation
[self performSegueWithIdentifier:#"Detail Segue" sender:selectedPhoto];
}
and modify prepareForSegue:sender: as the following
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"Detail Segue"]) {
PhotoDetailViewController *PhotoDetailVC = segue.destinationViewController;
PhotoDetailVC.photo = sender;
}
}

how to pass the image using storyboard in ios7

I cant able to pass the image from 1 view to another view in ios7.
in current view
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showRecipeDetail"]) {
BehindContributors *destViewController = segue.destinationViewController;
[destViewController setImage:#"1.jpg"];
}
in destination view
self.image1.image=self.image;
did you try imageNamed method on ÙIImage
like so;
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showRecipeDetail"]) {
BehindContributors *destViewController = segue.destinationViewController;
[destViewController setImage:[UIImage imageNamed:#"1.jpg"]];
}
What are you trying to pass image name(i.e. NSString object) or image itself(i.e. UIImage object).
If you are passing image name then use following
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showRecipeDetail"]) {
BehindContributors *destViewController = segue.destinationViewController;
[destViewController setImageName:#"1.jpg"];
}
}
Destination view should have property imageName of NSString type
then use it as
self.imageView.image = [UIImage imageName:imageName];
If you are passing image then use following
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showRecipeDetail"]) {
BehindContributors *destViewController = segue.destinationViewController;
[destViewController setImageData:[UIImage imageNamed:#"1.jpg"]];
}
}
Destination view should have property imageData of UIImage type
then use it as
self.imageView.image = imageData;

Unable to pass a NSString from One VC to Next VC

I have two VCs and i want to pass resourceName from HomeViewController to SingleWebViewController. But the resourceName is getting null.
HomeViewController.m
#import "HomeViewController.h"
#import "SingleWebViewController.h"
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
singleWebViewController = segue.destinationViewController;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedRow = indexPath.row;
switch (selectedRow)
{
case 0:
{
singleWebViewController.resourceName=#"intro";
NSLog(#"HtmlFileName:%#" , singleWebViewController.resourceName);
[self performSegueWithIdentifier:#"toSingleWebView" sender:self];
break;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
SingleWebViewController.h has the following line
#property (nonatomic,strong)NSString *resourceName;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:self.resourceName ofType:#"html" inDirectory:nil] ;
NSLog(#"%#se:" , self.resourceName);
NSURL *url = [NSURL fileURLWithPath:htmlFile ];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
_webView.delegate=(id)self;
}
i did notice that didSelectRowAtIndexPath is getting called before prepareForSegue. what's the cause for this. Please suggest.
In prepareForSegue, you're assigning something new to singleWebViewController. If this controller is different from the controller that's already assigned in tableView: didSelectRowAtIndexPath:, which I assume it is, then your property will be reset.
Instead, do this:
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
singleWebViewController = segue.destinationViewController;
singleWebViewController.resourceName = #"intro";
}
In your prepareForSegue you can set the property resourceName
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
SingleWebViewController *singleWebViewController = segue.destinationViewController;
[singleWebViewController setResourceName:#"Resource name"];
}
try this..pass value in prepareforsegue
Edit: if you want for different case to pass the different value then do this..i m assuming you want to segueway to same ViewController ie SingleWebViewController here.
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"toSingleWebView"]) {
SingleWebViewController *singleWebViewController = segue.destinationViewController;
NSIndexPath *indexPath = [self.tableView indexPathOfSelectedRow];
if(indexpath.row == 0){
singleWebViewController.resourceName=#"intro";
}
else if(indexPath.row == 1){
singleWebViewController.resourceName=#"some other value";
}
else{
singleWebViewController.resourceName=#"something else";
}
NSLog(#"HtmlFileName:%#" , singleWebViewController.resourceName);
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedRow = indexPath.row;
switch (selectedRow)
{
case 0:
{
[self performSegueWithIdentifier:#"toSingleWebView" sender:self];
break;
}
case 1:
{
[self performSegueWithIdentifier:#"toSingleWebView" sender:self];
break;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
You're setting resourceName after viewDidLoad has been called. Add a custom initialiser, initWithResourceName:(NSString *)resourceName, and set it there. Or move the code from viewDidLoad to viewDidAppear:.
Try with:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
HomeViewController *obj = segue.destinationViewController;
if([segue.identifier isEqualToString:#"toSingleWebView"]){
obj.singleWebViewController = singleWebViewController;
}
}

ios Storyboard - Push a title onto the View Controller

Is this piece of code suppose to set the title on the ViewController I am connecting to?
The 2 UIViewControllers are connected via a push segue - the first one is embedded in a NavigationController.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"settingsSegue"])
{
self.navigationItem.title = [[NSString alloc] initWithFormat:#"Custom Title"];
}
}
It does not work for me, but the syntax is right.
Advance Thanks:-)
The above answer works for me with one exception...
Change
self.title = myTitle;
to
self.navigationItem.title = myTitle;
Set the title in the viewDidLoad of the destination viewcontroller using a property in the destination VC:
if ([[segue identifier] isEqualToString:#"settingsSegue"]) {
MyDestinationViewController *mdvc = segue.destinationViewController;
mdvc.myTitle = [[NSString alloc] initWithFormat:#"Custom Title"];
}
Then in the viewDidLoad event in MyDestinationViewController.h:
#property (nonatomic,strong) NSString *myTitle;
In MyDestinationViewController.m:
#synthesize myTitle;
And finally in viewDidLoad:
self.title = myTitle;
You can set the title directly in the segue as well, there is no need to go through a property:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"settingsSegue"]) {
segue.destinationViewController.navigationItem.title = #"Custom Title";
}
}
Or, what I needed, to set the title of the pushed view controller to the title of the table cell that was clicked:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"settingsSegue"]) {
NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];
UITableViewCell *cell = [self tableView:self.tableView cellForRowAtIndexPath:myIndexPath];
segue.destinationViewController.navigationItem.title = cell.textLabel.text;
}
}

Resources