0

我在 UIImageView 中有问题。在我的项目中,我使用了两个UIImageViewmainview用于保存背景颜色和drawimage绘图),一个作为另一个的子视图:

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

问题是,每当我尝试将对象保存在 Core Data 中时,只会保存一张图像。这是我的代码:

 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];
4

1 回答 1

1

清理你的代码:

  • 从图像视图中获取图像
  • 将其转换为NSData
  • 将其保存在您的核心数据实体中
于 2012-08-28T11:25:29.187 回答