Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.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
Iphone 如何使用Core Data和RestKit映射和存储图像?_Iphone_Core Data_Restkit_Nsimage - Fatal编程技术网

Iphone 如何使用Core Data和RestKit映射和存储图像?

Iphone 如何使用Core Data和RestKit映射和存储图像?,iphone,core-data,restkit,nsimage,Iphone,Core Data,Restkit,Nsimage,使用具有核心数据的图像不是问题。关于如何做到这一点,有很多例子: 我想知道如何使用RestKit下载图像并将其正确映射到核心数据。有一个RestKit示例说明如何上载图像,但不下载和检索图像 现在,我的实体只有一个带有图像url的属性,但我希望能够脱机访问图像。我想做一些简单的映射,比如下载一个图像并将其重命名为它所属对象的id,但在我重新创建这个轮子之前,我想知道是否有其他人知道实现这一点的最“正确”的方法 我使用JsonKit和ASIHTTPRequest,但同样的原则也适用——我将图像数据

使用具有核心数据的图像不是问题。关于如何做到这一点,有很多例子:

我想知道如何使用RestKit下载图像并将其正确映射到核心数据。有一个RestKit示例说明如何上载图像,但不下载和检索图像


现在,我的实体只有一个带有图像url的属性,但我希望能够脱机访问图像。我想做一些简单的映射,比如下载一个图像并将其重命名为它所属对象的id,但在我重新创建这个轮子之前,我想知道是否有其他人知道实现这一点的最“正确”的方法

我使用JsonKit和ASIHTTPRequest,但同样的原则也适用——我将图像数据存储为字符串。这是一种与平台和语言无关的方法,非常适合JSON标准


Cocoa with Love的Matt Gallagher写了一篇非常干净的

我最终只是用图像url请求对象,在收到对象后,我抓取图像作为url,并将其设置为“我的对象的图像”属性。简单的

NSURL*imageURL; UIImage*选择图像

    for (Employee *employee in _employees){

        imageURL = [NSURL URLWithString:employee.imageURL];
        selectedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
        // Delete any existing image.
        NSManagedObject *oldImage = employee.image;
        if (oldImage != nil) {
            [employee.managedObjectContext deleteObject:oldImage];
        }

        // Create an image object for the new image.
        NSManagedObject *image = [NSEntityDescription insertNewObjectForEntityForName:@"Image" inManagedObjectContext:employee.managedObjectContext];
        employee.image = image;

        // Set the image for the image managed object.
        [image setValue:selectedImage forKey:@"image"];

        // Create a thumbnail version of the image for the recipe object.
        CGSize size = selectedImage.size;
        CGFloat ratio = 0;
        if (size.width > size.height) {
            ratio = 100.0 / size.width;
        } else {
            ratio = 100.0 / size.height;
        }
        CGRect rect = CGRectMake(0.0, 0.0, ratio * size.width, ratio * size.height);

        UIGraphicsBeginImageContext(rect.size);
        [selectedImage drawInRect:rect];
        employee.thumbnailImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }

对于此项目,您的目标部署为10.7和iOS 5?iOS5,但对于4.0也会有所帮助。看起来您现在正在同步请求映像,而不使用Restkit。对吗?是否有一种方法也可以使用RestKit请求映像?这将帮助我,因为restkit正在处理身份验证。这是正确的。我不知道使用RestKit请求二进制资源的方法,但我会检查一下:我为RKRequest和RKResponse创建了子类,它们将请求一个文件(或图像),并将响应写入磁盘而不是内存。工作起来很有魅力。