Cómo guardar una UIImage de una UIImageView en Core Data

I have issue in UIImageView. In my project I have used two UIImageView(mainview for saving the background color and drawimage for drawing), one as subview of the other:

[self.view addSubview:mainview];
[self.mainview addSubview:drawImage];

The problem is that whenever I am trying to save the objects in Core Data only one image is saved. This is my code:

 if(checkedcore==YES)
                {

                 NSData *imageData = UIImageJPEGRepresentation(mainview.image,1.0);
                NSManagedObject *imagetblObj= [NSEntityDescription insertNewObjectForEntityForName:@"Imagedata" inManagedObjectContext:context];



                //[imagetblObj setValue:<#(id)#> forKey:<#(NSString *)#>]



                //[imagetblObj setValue:self.imageData forKey:@"imageData"];
                [imagetblObj setValue:imageData forKey:@"imageData"];
                [imagetblObj setValue:self.textfield.text forKey:@"date"];
                [imagetblObj setValue:@"table" forKey:@"tabletext"];

                [context deleteObject:imagemanaged];


                [context insertObject:imagetblObj];

                NSError *err;
                if (![context save:&err]) 
                {
                    NSLog(@"Couldn't save history item into coredata");
                }
                else
                {

                    NSLog(@"data saved");
                }

                UIAlertView *alertview=[[UIAlertView alloc]initWithTitle:@"" message:@"Image Updated" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alertview show];

                NSLog(@"data saved");


                NSLog(@"save clicked");

                /// [self getcoredata];

               // TextPlacedViewController *viewcontroller=[[TextPlacedViewController alloc]initWithNibName:@"TextPlacedViewController" bundle:nil];
                ///viewcontroller.tabledata=self.array;
                ////viewcontroller.myimage=drawImage.image;
                ///[self.view   addSubview:viewcontroller.view];
                   // [self.navigationController popViewControllerAnimated:YES];
                }
                }


            else
            {
                mainview.image=drawImage.image;
       NSData *imageData = UIImageJPEGRepresentation(mainview.image, 1.0);


            if(![imageData isKindOfClass:[NSNull class]]||imageData==nil)
            {

        NSManagedObjectContext *context = [appDelegate managedObjectContext];
        NSManagedObject *imagetblObj= [NSEntityDescription insertNewObjectForEntityForName:@"Imagedata" inManagedObjectContext:context];

        [imagetblObj setValue:imageData forKey:@"imageData"];

        [imagetblObj setValue:self.textfield.text forKey:@"date"];
       [imagetblObj setValue:@"table" forKey:@"tabletext"];
        NSError *err;
        if (![context save:&err]) 
        {
            NSLog(@"Couldn't save history item into coredata");
        }
        UIAlertView *alertview=[[UIAlertView alloc]initWithTitle:@"" message:@"Data Saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertview show];

        NSLog(@"data saved");


        NSLog(@"save clicked");

        /// [self getcoredata];

        saveimageviewcontroller *viewcontroller=[[saveimageviewcontroller alloc]initWithNibName:@"saveimageviewcontroller" bundle:nil];
        ///viewcontroller.tabledata=self.array;
        ////viewcontroller.myimage=drawImage.image;
        ///[self.view   addSubview:viewcontroller.view];
        [self.navigationController pushViewController:viewcontroller animated:YES];

preguntado el 28 de agosto de 12 a las 11:08

Pretend you're someone else. Now read the question again and try to understand it. -

Your code does not make sense, too many brackets without context. Could you just post the relevant code, please? -

Try to reduce you code sample to the essence, at least half of what you have now. Is the problem getting hold of two images? Is the problem storing them in a managed object context? -

1 Respuestas

Clean up your code:

  • get the image from the image view
  • convertirlo a NSData
  • save it in your Core Data entity

Respondido 28 ago 12, 12:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.