2011-10-01 58 views
0

因此,我有一个我为iPad编写的应用程序,并且希望能够允许用户通过从相册或图像中选择图像来将图像插入到他们的文档中相机。所有这一切都很好。由于用户可能会保留文档的时间长于保存相册中的图像的时间,因此我将其复制一份,缩小一下,并将其存储在仅用于此目的的核心数据表中。在核心数据记录中以uiwebview显示图像

我的店面形象是这样的:

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]) {... 

我子类NSURLCache,如建议在Cocoa With Love和ovverride cachedResponseForRequest正是如此:

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

我也确保应用程序使用的子-classed NSURLCache通过在didFinishLaunchingWithOptions中的应用代理中执行此操作:

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

从核心数据记录返回的图象数据的方法是这样的:

+(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; 
} 

要插入图像插入到网络视图中,我有一个HTML img标记,其中在src中的名称=“”涉及背到图像表中的名称。上面的NSURLCache代码的意义在于观察我们存储在映像表中的名称,截取它并发送所请求映像的实际图像数据。

当我运行这个,我看到在我的sub-classsed NSURLCache对象中获取请求的图像。它找到正确的记录,并按照它应该返回的数据。不过,我仍然得到没有找到图片图标在我的UIWebView:

Image for Image Not Found

所以马库斯(下)表明,我不是图像数据存储在核心数据表。所以我所做的更改为以适应:

存储图像:

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]; 

检索图像:

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

在调试模式下,我看到IDATA不会获取加载正确的图像数据。

而我仍然得到图像未找到的图像!显然,我在这里做错了什么。我只是不知道它是什么。

那么......我在这里做错了什么?我怎样才能使这个工作正常?

谢谢。

+0

你如何尝试将图像放入UIWebView?在你的代码中看不到。 – Tom

+0

@汤姆 - 我编辑过,包括有关的信息。基本上是一个img标签,其中src中的名称是图像文件中使用的名称。在html到达UIWebView之前,将img标签插入到html中。 – Chris

回答

0

我会强烈建议您不要将二进制数据存储在核心数据中。在Core Data中存储二进制数据,特别是在iOS设备上,会导致缓存严重的性能问题。

首选的方法是将实际的二进制数据存储在磁盘上的文件中,并引用存储在Core Data中的文件。从那里,将图像url更改为指向本地文件是一件简单的事情。

+0

我确实尝试过。我编辑了我的问题以包含代码细节。我猜我在某个地方做了一些愚蠢的事情,因为它仍然不起作用。谢谢。 – Chris

+0

我没有看到任何跳出来的东西。你有这个行为的测试项目吗?我发现一个测试非常出色,原因有几个:1)我们可以修改它,2)它隔离代码,你通常可以自己发现问题,3)如果它是Apple bug,你的雷达已经完成了:) –

0

所以事实证明,我是这样的方式overhinking。当我编写HTML时,我只是用图像标签来写入图像的路径。奇迹般有效。

但我很想知道为什么我在问题中提出的解决方案不起作用。

而且,我确实没有将图像存储在表格中。