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;
}
Related
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];
}];
}
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;
}
}
I have two images in one view controller which have been picked with UIImagePickerController:
ChoosePhotosViewController.h
#interface PickPhotosViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
#property (strong, nonatomic) IBOutlet UIImageView *pickedImage1; // image 1
#property (strong, nonatomic) IBOutlet UIImag8eView *pickedImage2; // image 2
- (IBAction)pickImage1:(id)sender; // button that picks up the first image
- (IBAction)pickImage2:(id)sender; // button that picks up the seconds image
- (IBAction)nextButton:(id)sender;
in implementation file - ChoosePhotosViewController.m
#implementation PickPhotosViewController
#synthesize pickedImage1, pickedImage2;
for each button I wrote a code which invokes action sheet with a list of actions:
-(IBAction)pickImage1:(id)sender {
[[NSUserDefaults standardUserDefaults] setInteger:CurrentImageCategoryImage1 forKey:#"currentImageCategory"];
[[NSUserDefaults standardUserDefaults] synchronize];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Choose image one" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Choose existing photo", #"Take new photo", nil];
[actionSheet showInView:self.view];
}
There are two actions - take photo from camera air upload from library:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
else if (buttonIndex == 1) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
}
The similar algorithm for the second button. Then in UIImagePickerController method I wrote;
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
if([[NSUserDefaults standardUserDefaults] integerForKey:#"currentImageCategory"] ==CurrentImageCategoryImage1)
{
self.pickedImage1.image = chosenImage;
}
else if([[NSUserDefaults standardUserDefaults] integerForKey:#"currentImageCategory"] ==CurrentImageCategoryImage2)
{
self.pickedImage2.image = chosenImage;
}
}
Then I need to display these two images in the next viewController when I click button “next”. I work with storyboards, that is why I wrote prepareForSegue method:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#“next"]) {
TypeTextViewController *transferedImage = [segue destinationViewController];
transferedImage.imageWithText1 = pickedImage1;
}
}
where imageWithText1 is a UIImageView and declared in TypeTextViewController.h, but When I click button images don’t appear in the TypeTextViewController.
Where is a problem?
You want to create NSMutableArray and then this two Images add into this Array.And then you have to pass this NSMutableArray to Another View Controller.
Hope this is helpfull.
I dont think you can pass a UIImageView from one VC to another VC. I think we can only pass UIImage and not UIImageView. Try this.. But im not sure.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#“next"])
{
TypeTextViewController *transferedImage = [segue destinationViewController];
transferedImage.imageWithText1.image = pickedImage1.image;
}
}
// pass Image one view to other view:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
[self dismissViewControllerAnimated:YES completion:^
{
color_ViewController*secondview=[[UIStoryboard storyboardWithName:#"Main" bundle:nil]instantiateViewControllerWithIdentifier:#"color_ViewController"];
secondview.b_image=image;
[[NSUserDefaults standardUserDefaults ]setInteger:1 forKey:#"123"];
[[self navigationController] pushViewController:secondview animated:YES];
}];
}
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];
}];
}