将图像从Camera发送到另一个ViewController

时间:2015-02-26 13:31:58

标签: ios objective-c uiviewcontroller uiimagepickercontroller

下午好,

这是我在StackOverFlow中的第一篇文章,所以我希望你可以帮助我解决我的问题,因为我已经坚持了5天,我需要一些帮助才能使用我的第一个iOS应用程序。

我正在尝试使用CameraViewController(Apple的默认方法)拍照,拍照后我想按一个按钮,然后在UIImage中加载另一个带有该图像的ViewController。目前Segue正在使用ViewController,但我无法将图像从一个viewcontroller发送到另一个。

这是我的代码:

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

这就是我的“DestinationViewController”,名为“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

我还在StoryBoard中创建了名为“Signature”的Segue按钮。但也许我在代码中遗漏了某些东西或某些东西......

请帮帮我吗?

提前致谢。

2 个答案:

答案 0 :(得分:1)

你可以这样做

@interface SignViewController : ViewController

@property (strong, nonatomic) UIImage *image;

@end

发送UIImage个实例而不是UIImageView

答案 1 :(得分:1)

您已经在property中为imageView创建了SignViewController {。}}只在{。1}} .m文件中,您可以在准备segue时将图像分配给该imageview。请参阅以下摘录。

SignViewController.m文件中的

@synthesize imageViewStep 1

@synthesize imageView;:使用准备segue将Image分配给SignViewController的imageView。

Step 2