简体   繁体   中英

Send image from Camera to another ViewController

Good afternoon,

This is my first post in StackOverFlow, so I hope you can help me with my problem because I'm stuck with this for 5 days and I need some help with my first iOS App.

I'm trying to take a picture using the CameraViewController (the default method by Apple) and after taking the picture I would like to press a button and then load another ViewController with that image in a UIImage. At the moment the Segue is going to the ViewController, but I'm not able to send the image from one viewcontroller to another.

Here is my code:

CamViewController.h

#import <UIKit/UIKit.h>

@interface CamViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (strong, nonatomic) IBOutlet UIImageView *imageView;
- (IBAction)takePhoto:  (UIButton *)sender;
- (IBAction)selectPhoto:(UIButton *)sender;
- (IBAction)uploadPhoto:(UIButton *)sender;

@end

CamViewController.m

#import "CamViewController.h"

#import "SignViewController.h"

#import <Security/Security.h>

#import "SSKeychain.h"

@interface NSURLRequest (DummyInterface)

+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;

+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;

@end

@interface CamViewController ()

@end

@implementation CamViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];

    picker.delegate = self;

    picker.allowsEditing = YES;

    picker.sourceType = UIImagePickerControllerSourceTypeCamera;

    [self presentViewController:picker animated:YES completion:NULL];

    [self.view setBackgroundColor: [self colorWithHexString:@"404041"]];

    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.

}



- (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)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

    if ([[segue identifier] isEqualToString:@"Signature"])

    {

        SignViewController *imagen = [segue destinationViewController];

        imagen.imageView = _imageView;
    }
}

@end

And that's my "DestinationViewController", called "SignViewController":

SignViewController.h

#import "ViewController.h"

@interface SignViewController : ViewController

@property (strong, nonatomic) IBOutlet UIImageView *imageView;

@end

SignViewController.m

#import "SignViewController.h"

#import "CamViewController.h"

@interface SignViewController ()

@end

@implementation SignViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    NSURL *url = [NSURL URLWithString:_imageView];

    NSData *data = [NSData dataWithContentsOfURL:url];

    UIImage *img = [[UIImage alloc] initWithData:data];

    self.imageView.image = img;

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

I have also created the Segue button called "Signature" in the StoryBoard. But maybe I'm missing something or something is wrong in my code...

Can you help me, please?

Thanks in advance.

you can do like this

@interface SignViewController : ViewController

@property (strong, nonatomic) UIImage *image;

@end

Send a UIImage instance not an UIImageView

You have already created a property for imageView in SignViewController just @synthesize imageView it in .m file and you can assign image to that imageview while you are preparing segue. refer following snippets.

Step 1 : @synthesize imageView; in SignViewController.m file.

Step 2 : Assign Image to imageView of SignViewController with preparing segue.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"Signature"])
    {
        SignViewController *imagen = [segue destinationViewController];

        imagen.imageView = self.imageView.image;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM