因此,我有一个我为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:
所以马库斯(下)表明,我不是图像数据存储在核心数据表。所以我所做的更改为以适应:
存储图像:
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不会获取加载正确的图像数据。
而我仍然得到图像未找到的图像!显然,我在这里做错了什么。我只是不知道它是什么。
那么......我在这里做错了什么?我怎样才能使这个工作正常?
谢谢。
你如何尝试将图像放入UIWebView?在你的代码中看不到。 – Tom
@汤姆 - 我编辑过,包括有关的信息。基本上是一个img标签,其中src中的名称是图像文件中使用的名称。在html到达UIWebView之前,将img标签插入到html中。 – Chris