开发者

Does the iPhone compress images saved within my app's documents directory?

开发者 https://www.devze.com 2022-12-24 09:49 出处:网络
We are caching images downloaded from our server. We get the data from an ASIHTTPRequest callback like this:

We are caching images downloaded from our server. We get the data from an ASIHTTPRequest callback like this:

#pragma mark ASIHTTPRequest callback
-(void)imageDownloadFinished:(ASIHTTPRequest*)aRequest
{
    NSString* fileName = aRequest.url.path.lastPathComponent; 
    [self imageDidArrive:[aRequest responseData] forFileName:fileName];
}

We write the image data to our local storage like this:

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

The downloaded images are always the expected size, around 45-85KB.

Later, we read images from our cache like this:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths obje开发者_运维技巧ctAtIndex:0] ; 
 NSString* folder = [[documentsDirectory stringByAppendingPathComponent:@"flook.images"] retain];
        NSString* fileName = [folder stringByAppendingFormat:@"/%@", aBaseFilename];

 image = [UIImage imageWithContentsOfFile:fileName];

Occasionally, the images returned from this cache read are much smaller because they are much more compressed - around 5-10KB. Has the OS done this to us?

Edit - it turns out that we are downloading the small images, so the issue isn't on the iPhone


If I'm reading your code correctly, you're using the NSData method writeToFile:atomically: to write to the file. That does an exact byte-for-byte write of the contents of the NSData object.

It appears that the NSData object is created directly from the contents of the HTTP response, so the answer is "no", there should not be any compression taking place.


We have the solution. When the phone is running on the 3G network, O2 kindly steps in and applies extra JPG compression to our images, so that they look extra horrible.

See this post on the UK 3G forum.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号