【问题标题】:Does the iPhone compress images saved within my app's documents directory?iPhone 会压缩保存在我的应用程序文档目录中的图像吗?
【发布时间】: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


【解决方案1】:

如果我正确阅读了您的代码,则您正在使用 NSData 方法 writeToFile:atomically: 写入文件。这会对 NSData 对象的内容进行精确的逐字节写入。

看来 NSData 对象是直接从 HTTP 响应的内容创建的,所以答案是“否”,不应该发生任何压缩。

【讨论】:

  • 肯定发生了什么事!但也许它在我尚未发现的代码的某个角落。
【解决方案2】:

我们有解决方案。当手机在 3G 网络上运行时,O2 会介入并对我们的图像进行额外的 JPG 压缩,让它们看起来更可怕。

看到这个post on the UK 3G forum

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    • 2015-07-19
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 2015-02-19
    相关资源
    最近更新 更多