【发布时间】:2011-02-21 19:38:12
【问题描述】:
我们正在缓存从我们的服务器下载的图像。我们从 ASIHTTPRequest 回调中获取数据,如下所示:
#pragma mark ASIHTTPRequest callback
-(void)imageDownloadFinished:(ASIHTTPRequest*)aRequest
{
NSString* fileName = aRequest.url.path.lastPathComponent;
[self imageDidArrive:[aRequest responseData] forFileName:fileName];
}
我们将图像数据写入本地存储,如下所示:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0] ;
NSString* folder = [[documentsDirectory stringByAppendingPathComponent:@"flook.images"] retain];
NSString* fileName = [folder stringByAppendingFormat:@"/%@", aBaseFilename];
BOOL writeSuccess = [anImageData writeToFile:fileName atomically:NO];
下载的图像始终是预期大小,大约 45-85KB。
稍后,我们像这样从缓存中读取图像:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0] ;
NSString* folder = [[documentsDirectory stringByAppendingPathComponent:@"flook.images"] retain];
NSString* fileName = [folder stringByAppendingFormat:@"/%@", aBaseFilename];
image = [UIImage imageWithContentsOfFile:fileName];
有时,从缓存读取返回的图像要小得多,因为它们的压缩程度更高 - 大约 5-10KB。操作系统对我们这样做了吗?
编辑 - 原来我们正在下载小图像,所以问题不在 iPhone 上
【问题讨论】:
-
顺便说一句,我正在调试不是我自己的代码,所以很愿意相信我错过了什么!
-
“anImageData”是如何创建的?例如,您是否使用 UIImageJPEGRepresentation()?
-
它由 ASIHTTPRequest 回调提供,用于图像下载成功:#pragma mark ASIHTTPRequest callbacks - (void)imageDownloadFinished:(ASIHTTPRequest*)aRequest; { NSString* 文件名 = aRequest.url.path.lastPathComponent; [self imageDidArrive:[aRequest responseData] forFileName:fileName]; }
-
图片是JPG格式的。
标签: iphone