使用coredata将图像保存到设备

时间:2012-12-06 08:43:31

标签: ios core-data

- (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(可转换),imgcode(字符串)我试图将此图像保存到具有核心数据的设备。但是我有这个错误,可能是什么问题?提前谢谢你......

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

3 个答案:

答案 0 :(得分:0)

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

see this

答案 1 :(得分:0)

NSData * imageData = UIImagePNGRepresentation(tmpphoto);  NSManagedObject * image = [NSEntityDescription insertNewObjectForEntityForName:@“Image”inManagedObjectContext:context]; images.image = image;

[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();

我已经改变了这样,我遇到了同样的问题。而且在datamodel中我们没有NSData选项.Binarydata和Tranformable选项我们有图像

答案 2 :(得分: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];
    }

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