0
- (IBAction)saveImage:(id)sender{

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];


    NSManagedObject *image = [NSEntityDescription insertNewObjectForEntityForName:@"Image" inManagedObjectContext:context];
                              images.image = image;

                              [image setValue:tmpphoto forKey:@"img"];

                               CGSize size = tmpphoto.size;
                               CGFloat ratio = 0;
                               if (size.width > size.height) {
                                   ratio = 90.0 / size.width;
                               } else {
                                   ratio = 90.0 / size.height;
                               }
                               CGRect rect = CGRectMake(0.0, 0.0, ratio * size.width, ratio * size.height);

                               UIGraphicsBeginImageContext(rect.size);
                               [tmpphoto drawInRect:rect];

                               images.imaget = UIGraphicsGetImageFromCurrentImageContext();

                               UIGraphicsEndImageContext();

}

我的实体名称是“图像”,属性为 img(transformable),imgcode(string) 我试图用核心数据将此图像保存到我的设备。但我有这个错误,可能是什么问题?先感谢您...

错误:'NSInvalidArgumentException',原因:'无法将模型与名为'Image'的两个不同实体合并'

4

3 回答 3

0

将图像属性的类型设置为NSData而不是可转换的,当您想要获取图像时,使用PNG表示来获取它。

看到这个

于 2012-12-06T08:55:54.117 回答
0

NSData *imageData = UIImagePNGRepresentation(tmpphoto); NSManagedObject *image = [NSEntityDescription insertNewObjectForEntityForName:@"Image" inManagedObjectContext:context]; 图像.图像 = 图像;

[image setValue:imageData forKey:@"img"];

CGSize size = tmpphoto.size;
CGFloat ratio = 0;
if (size.width > size.height) {
        ratio = 44.0 / size.width;
} else {
    ratio = 44.0 / size.height;
}
CGRect rect = CGRectMake(0.0, 0.0, ratio * size.width, ratio * size.height);

UIGraphicsBeginImageContext(rect.size);
[tmpphoto drawInRect:rect];
images.imaget = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

我已经改变了这样,我有同样的问题。而且在数据模型中,我们没有 NSData 选项。我们为图像提供的二进制数据和可转换选项

于 2012-12-06T09:31:10.963 回答
0
NSManagedObjectContext *context = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
    if (!currentPicture)
        self.currentPicture = (ImageList *)[NSEntityDescription insertNewObjectForEntityForName:@"ImageList" inManagedObjectContext:context];

    if (imageview.image)
    {
        float resize = 74.0;
        float actualWidth = imageview.image.size.width;
        float actualHeight = imageview.image.size.height;
        float divBy, newWidth, newHeight;
        if (actualWidth > actualHeight) {
            divBy = (actualWidth / resize);
            newWidth = resize;
            newHeight = (actualHeight / divBy);
        } else {
            divBy = (actualHeight / resize);
            newWidth = (actualWidth / divBy);
            newHeight = resize;
        }
        CGRect rect = CGRectMake(0.0, 0.0, newWidth, newHeight);
        UIGraphicsBeginImageContext(rect.size);
        [imageview.image drawInRect:rect];
        UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        NSData *smallImageData = UIImageJPEGRepresentation(smallImage, 1.0);
        [self.currentPicture setMyimage:smallImageData];
    }
    NSError *error;
    if (![context save:&error]){
        NSLog(@"Failed to add new picture with error: %@", [error domain]);}
    else
    {
        UIAlertView *AlertCampo = [[UIAlertView alloc] initWithTitle:@"
" message:@"Your image successfully saved into your device"
                                                            delegate:nil
                                                   cancelButtonTitle:@"OK"
                                                   otherButtonTitles:nil];
        [AlertCampo show];
    }

有了这个,我解决了我的问题。谢谢大家。

于 2012-12-11T07:56:36.803 回答