Как сохранить изображение, захваченное в CoreData, и отобразить его в UICollectionView? - PullRequest
0 голосов
/ 05 июня 2018

Я делаю приложение для галереи.В настоящее время я сохраняю изображение захвата в CoreData, преобразовывая его в NSData.Но я не знаю, как получить изображения для захвата, которые находятся в CoreData, и как отобразить их в UICollectionView.пожалуйста, помогите мне с этим.Здесь я делюсь своим кодом.помогите мне обновить его.

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

#import "ViewController.h"
#import "AppDelegate.h"

@interface ViewController (){
    AppDelegate *appdelegate;
    NSManagedObjectContext *context;

}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    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)camera:(UIButton *)sender {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;

    [self presentViewController:picker animated:YES completion:NULL];
}

- (IBAction)album:(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];
    NSData *imageData = UIImagePNGRepresentation(chosenImage);

    appdelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    context = appdelegate.persistentContainer.viewContext;



    //save coredata
    NSManagedObject *entity = [NSEntityDescription insertNewObjectForEntityForName:@"Images" inManagedObjectContext:context];

    [entity setValue:imageData forKey:@"firstimage"];

    [appdelegate saveContext];

    NSError *error = nil;
    if (![context save:&error]) {
        NSLog(@"Can't Save");
    }else{
        NSLog(@"hifi");
    }




    NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];

    NSEntityDescription *entity1 = [NSEntityDescription entityForName:@"Images" inManagedObjectContext:context];

    [fetchRequest setEntity:entity1];


    [picker dismissViewControllerAnimated:YES completion:NULL];

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

    [picker dismissViewControllerAnimated:YES completion:NULL];

}


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