gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:49:46 26 4
gpt4 key购买 nike

下午好

这是我在 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 按钮。但也许我遗漏了一些东西或者我的代码中有什么问题......

你能帮帮我吗?

提前致谢。

最佳答案

你可以这样做

@interface SignViewController : ViewController

@property (strong, nonatomic) UIImage *image;

@end

发送 UIImage 实例而不是 UIImageView

关于ios - 将图像从 Camera 发送到另一个 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28743839/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com