[英]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. 这是我在StackOverFlow上的第一篇文章,所以我希望您能为我的问题提供帮助,因为我坚持了5天,而我的第一个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. 我正在尝试使用CameraViewController(Apple的默认方法)拍照,拍照后,我想按一个按钮,然后在UIImage中加载带有该图像的另一个ViewController。 At the moment the Segue is going to the ViewController, but I'm not able to send the image from one viewcontroller to another. 目前,Segue正在使用ViewController,但是我无法将图像从一个ViewController发送到另一个ViewController。
Here is my code: 这是我的代码:
CamViewController.h 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 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": 那就是我的“ DestinationViewController”,叫做“ SignViewController”:
SignViewController.h SignViewController.h
#import "ViewController.h"
@interface SignViewController : ViewController
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@end
SignViewController.m 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. 我还在故事板中创建了名为“签名”的Segue按钮。 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
发送一个UIImage
实例而不是一个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. 您已经创建了一个property
为imageView
在SignViewController
只是@synthesize imageView
它.m文件,你可以在你准备SEGUE分配图像到ImageView的。 refer following snippets. 请参阅以下片段。
Step 1
: @synthesize imageView;
Step 1
: @synthesize imageView;
in SignViewController.m file. 在SignViewController.m文件中。
Step 2
: Assign Image to imageView of SignViewController with preparing segue. 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.