从coredata接收图像

时间:2015-11-21 11:11:46

标签: ios objective-c

我从camrea / gallery手机拍了一张照片,然后我将她存储在核心数据上。但我无法从核心数据中收回图像。

@property (strong) NSMutableArray *allPic;
@property (strong) NSManagedObject *Image;
@property(strong,nonatomic)NSData *dataImage;
@end

@implementation ViewController



- (NSManagedObjectContext *)managedObjectContext
{
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    // Fetch the devices from persistent data store
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Picture"];
   self.allPic = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

    [self loadDefaultImage];
}



- (void)viewDidLoad {
    [super viewDidLoad];

    // Creating a Circular Profile Image.
    self.img.layer.cornerRadius = self.img.frame.size.width / 2.0;
    self.img.clipsToBounds = YES;

    // Adding Border to image.
    self.img.layer.borderWidth = 6.0;
    self.img.layer.borderColor = [UIColor blackColor].CGColor;

    [self loadDefaultImage];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)takePic:(id)sender {

    // ALERT SHEET.
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

    //CAMERA
    UIAlertAction *openCamrea = [UIAlertAction actionWithTitle:@"צלם" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
    {
        // If device has no camera.
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            UIAlertController *alertNoCamera = [UIAlertController alertControllerWithTitle:@"Error" message:@"Device has no camera" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){}];
            [alertNoCamera addAction:ok];
            [self presentViewController:alertNoCamera animated:YES completion:nil];
        }
        else// if  have a camera.
        {
           UIImagePickerController *picker = [[UIImagePickerController alloc] init];
           picker.delegate = self;
           picker.allowsEditing = YES;
           picker.sourceType = UIImagePickerControllerSourceTypeCamera;


           [self presentViewController:picker animated:YES completion:NULL];
        }
    }];
    // GALLERY
    UIAlertAction *openGallery = [UIAlertAction actionWithTitle:@"גלריה" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

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

    [alert addAction:openCamrea];
    [alert addAction:openGallery];
    [self presentViewController:alert animated:YES completion:nil];
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *mediaType = info[UIImagePickerControllerMediaType];
    [self dismissViewControllerAnimated:YES completion:nil];

    if ([mediaType isEqualToString:(NSString *)kUTTypeImage])
    {
        UIImage *sampleimage = info[UIImagePickerControllerOriginalImage];

        //sampleimage = [UIImage imageNamed:@"image"];

        self.dataImage = UIImageJPEGRepresentation(sampleimage, 0.0);

        [self.allPic setValue:_dataImage forKey:@"image"]; // obj refers to NSManagedObject
    }

}

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

    [picker dismissViewControllerAnimated:YES completion:NULL];
    }
-(void)loadDefaultImage
{

    UIImage *img2 = [UIImage imageWithData:_dataImage];
    self.img.image=img2;

}

我更新了这个问题 我再次编辑了这个问题,我写了所有的代码, 我拍了一张照片,保存了,然后试着把它拿回去。

2 个答案:

答案 0 :(得分:1)

您可以尝试使用此功能从核心数据中获取图像。 从Core数据中获取数据,然后:

UIImage *img=[UIImage imageWithData:data];

您将数据保存为:

   [self.allPic setValue:_dataImage forKey:@"image"];

但是你已经将'self.allPic'声明为 NSArray 。这将是可用的,直到此对象存在于内存中,这就是为什么你没有得到它。

您必须使用NSMananaged类型来保存Core数据值。

查看这篇文章:Tuts

答案 1 :(得分:0)

目前,您尚未在Core Data中保存任何内容。供您参考:所有“托管对象”都封装在托管对象上下文$temp = get_option( 'tt_options' ); echo $temp['footer_text']; 中。然后,您需要使用NSManagedObjectContext在此上下文中保存对象。在此方法之后,您的对象将从内存移动到磁盘中的持久存储。