mimpami
mimpami

Reputation: 83

Receive image back from coredata

i took a picture from the camrea/gallery phone and then i stored her on core data. but i can't receive the image back from core data.

@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;

}

I updated the question I edited the question again, I wrote all the code, I take a picture, saves it, and then try to bring it back.?

Upvotes: 1

Views: 49

Answers (2)

Rahul
Rahul

Reputation: 5834

You can Try this to get Image From Core Data. Get You data from Core data and then:

UIImage *img=[UIImage imageWithData:data];

You are saving your data as:

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

But you have declared `self.allPic' as NSArray. This will be available until this object lives in memory thats why you are not getting it back.

You must use NSMananaged type to save Core data values.

Have a look into this article: Tuts

Upvotes: 1

Kevin Wu
Kevin Wu

Reputation: 1477

Currently, you have not saved anything in Core Data. For your information: All "Managed Objects" are encapsulated in a Managed Object Context NSManagedObjectContext. You then need to save your objects in this context using [context save: yourobject]. After this method, your objects will be moved from memory to persistent storage in disk.

Upvotes: 0

Related Questions