簡體   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