如何保存CoreData中捕获的图像并将其显示在UICollectionView中?

时间:2018-06-05 08:38:34

标签: ios objective-c

我正在制作一个图库应用。目前我通过将其转换为NSData来保存CoreData中的捕获图像。但我不知道如何获取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

0 个答案:

没有答案
相关问题