So I have an app I've written for the iPad, and I'd like to be able to allow users to insert images into their documents by selecting an image from an album or the camera. All that works great. Because the user might keep the document longer than they keep the image in an album, I make a copy of it, scale it down a bit, and store it in a core data table that is just used for this purpose.

I store the image like this:

```
NSManagedObjectContext* moc=[(ActionNote3AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSString* imageName=[NSString stringWithFormat:@"img%lf.png",[NSDate  timeIntervalSinceReferenceDate]];
Image* anImage = [NSEntityDescription insertNewObjectForEntityForName:@"Image" inManagedObjectContext:moc];
anImage.imageName=imageName;
anImage.imageData=UIImagePNGRepresentation(theImage);
NSError* error=nil;
if(![moc save:&error]) {...

```

I sub-class NSURLCache, as suggested on [Cocoa With Love](http://cocoawithlove.com/2010/09/substituting-local-data-for-remote.html), and ovverride cachedResponseForRequest thusly:

```
- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request {
    NSString *pathString = [[[request URL] absoluteString]lastPathComponent];
    NSData* data = [Image dataForImage:pathString];
    if (!data) {
        return [super cachedResponseForRequest:request];
    }

    NSURLResponse *response =[[[NSURLResponse alloc] 
                               initWithURL:[request URL] 
                               MIMEType:[NSString stringWithString:@"image/png"] 
                               expectedContentLength:[data length] 
                               textEncodingName:nil] 
                              autorelease];
    NSCachedURLResponse* cachedResponse =[[[NSCachedURLResponse alloc] initWithResponse:response data:data] autorelease];

    return cachedResponse;
}

```

I also make sure the app uses the sub-classed NSURLCache by doing this in my app delegate in didFinishLaunchingWithOptions:

```
ANNSUrlCache* uCache=[[ANNSUrlCache alloc]init];
[NSURLCache setSharedURLCache:uCache];

```

The method that returns the image data from the core data record looks like this:

```
+(NSData*)dataForImage:(NSString *)name {
    NSData* retval=nil;
    NSManagedObjectContext* moc=[(ActionNote3AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];

    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Image" inManagedObjectContext:moc];
    NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:entityDescription];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"imageName==%@", name];
[request setPredicate:predicate];

    NSError* error=nil;
    NSArray *array = [moc executeFetchRequest:request error:&error];

    if ([array count]>0) {
        retval=((Image*)[array objectAtIndex:0]).imageData;
    }

    return retval;
}

```

To insert the image into the web view, I have an html img tag where the name in src="" relates back to the name in the image table. The point of the NSURLCache code above is to watch for a name we have stored in the image table, intercept it, and send the actual image data for the image requested.

When I run this, I see the image getting requested in my sub-classed NSURLCache object. It is finding the right record, and returning the data as it should. However, I'm still getting the image not found icon in my uiwebview:

![Image for Image Not Found](https://i.stack.imgur.com/Y43Zq.png)

So Marcus (below) suggested that I not store the image data in a core data table. So I made changes to accomodate for that:

Storing the image:

```
NSString* iName=[NSString stringWithFormat:@"img%lf.png",[NSDate timeIntervalSinceReferenceDate]];
NSData* iData=UIImagePNGRepresentation(theImage);

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:iName];
[iData writeToFile:fullPathToFile atomically:NO];

```

Retrieving the image:

```
- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request {
    NSString *pathString = [[[request URL] absoluteString]lastPathComponent];
    NSString* iPath = [Image pathForImage:pathString];
    if (!iPath) {
        return [super cachedResponseForRequest:request];
    }

    NSData* idata=[NSData dataWithContentsOfFile:iPath];

    NSURLResponse *response =[[[NSURLResponse alloc] 
                               initWithURL:[request URL] 
                               MIMEType:@"image/png" 
                               expectedContentLength:[idata length] 
                               textEncodingName:nil] 
                              autorelease];
    NSCachedURLResponse* cachedResponse =[[[NSCachedURLResponse alloc] initWithResponse:response data:idata] autorelease];

    return cachedResponse;
}

```

In debug mode, I see that idata does get loaded with the proper image data.

And I still get the image-not-found image! Obviously, I'm doing something wrong here. I just dont know what it is.

So... What am I doing wrong here? How can I get this to work properly?

Thank you.