Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用coredata将图像保存到设备_Ios_Core Data - Fatal编程技术网

Ios 使用coredata将图像保存到设备

Ios 使用coredata将图像保存到设备,ios,core-data,Ios,Core Data,我的实体名为“Image”,属性为img(可转换),imgcode(字符串),我正试图将此图像与核心数据一起保存到我的设备中。但我有此错误,可能是什么问题?提前谢谢你 错误:“NSInvalidArgumentException”,原因:“无法合并名为“图像”的两个不同实体的模型。将图像属性的类型设置为NSData,而不是可转换的。如果要获取图像,请使用PNG表示法获取 NSData*imageData=UIImagePNGRepresentation(tmpphoto); NSManagedO

我的实体名为“Image”,属性为img(可转换),imgcode(字符串),我正试图将此图像与核心数据一起保存到我的设备中。但我有此错误,可能是什么问题?提前谢谢你


错误:“NSInvalidArgumentException”,原因:“无法合并名为“图像”的两个不同实体的模型。

将图像属性的类型设置为
NSData
,而不是可转换的。如果要获取图像,请使用
PNG
表示法获取


NSData*imageData=UIImagePNGRepresentation(tmpphoto); NSManagedObject*image=[NSEntityDescription insertNewObjectForEntityForName:@“image”在ManagedObjectContext:context中]; 图像。图像=图像

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

}
我已经像这样改变了,我也有同样的问题。而且在数据模型中,我们没有NSData选项。对于图像,我们有Binarydata和Transformable选项

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

通过这一点,我已经解决了我的问题。谢谢大家。

我不能对你问题的核心数据方面发表评论,但有两点意见:1。您可以使用或
uiimagejpegresentation
来检索图像,而不是使用图形上下文方法;2.图像(除非非常小,小于100kb)不适合存储在CoreData中。而且,当我注释这两行时,AppDelegate*AppDelegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];NSManagedObjectContext*上下文=[appDelegate managedObjectContext];而不是NSManagedObject*image=[NSEntityDescription insertNewObjectForEntityForName:@“image”inManagedObjectContext:self.managedObjectContext];正在执行此操作我的错误正在更改为entityForName:nil不是合法的NSManagedObjectContext参数搜索实体名称“Image”???
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];
    }