繁体   English   中英

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

[英]Send image from Camera to another ViewController

下午好,

这是我在StackOverFlow上的第一篇文章,所以我希望您能为我的问题提供帮助,因为我坚持了5天,而我的第一个iOS App需要一些帮助。

我正在尝试使用CameraViewController(Apple的默认方法)拍照,拍照后,我想按一个按钮,然后在UIImage中加载带有该图像的另一个ViewController。 目前,Segue正在使用ViewController,但是我无法将图像从一个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

我还在故事板中创建了名为“签名”的Segue按钮。 但是也许我的代码中缺少某些东西或某些地方有问题...

你能帮我吗?

提前致谢。

你可以这样

@interface SignViewController : ViewController

@property (strong, nonatomic) UIImage *image;

@end

发送一个UIImage实例而不是一个UIImageView

您已经创建了一个propertyimageViewSignViewController只是@synthesize imageView它.m文件,你可以在你准备SEGUE分配图像到ImageView的。 请参阅以下片段。

Step 1@synthesize imageView; 在SignViewController.m文件中。

Step 2 :通过准备设置将图像分配给SignViewController的imageView。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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