Как сохранить изображение, снятое с помощью Camera, и сохранить в другом ViewController? - PullRequest
0 голосов
/ 01 июня 2018

Как сохранить изображение, снятое с помощью камеры, и сохранить его в другом ViewController?

Я создаю приложение Photo Gallery.Но я не знаю, как сохранить захваченное изображение и показать его в виде коллекции в другом viewcontroller.В настоящее время я использую UIImagePickerController для доступа к камере и библиотеке iphone. Может ли кто-нибудь помочь мне сделать это.

Вот мой код.

Заранее спасибо

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController



- (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)viewDidLoad {
    [super viewDidLoad];
    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.
}


@end

Ответы [ 2 ]

0 голосов
/ 01 июня 2018

Вы можете достичь этого, добавив NSNotificationcenter.

Класс A - Где вы выбираете изображение с камеры

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
     UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
     self.imageView.image = chosenImage;
     [picker dismissViewControllerAnimated:YES completion:NULL];
    [[NSNotificationCenter defaultCenter] postNotificationName: @"GotNewImage" object: chosenImage];
}

Класс B - Куда вы хотите передать изображение после выделения и показать в виде коллекции.Добавьте Nsnotificationcenter для этого класса в Viewdidload

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(GotNewImage:) name:@"GotNewImage" object:nil];

// Чтобы получить изображение для класса B

-(void)GotNewImage:(NSNotification*)notification
 {
     UIImage * newImage = (UIImage*) notification.object;
     //Now do whatever you want to do with your image...
     //Add in collection view and reload the collection view
 }
0 голосов
/ 01 июня 2018

Попробуйте

1002 *
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...