Avoiding image re-compression on cellular networks | Code | My Website

Avoiding image re-compression on cellular networks

One problem encountered when trying to upload images programmatically to an app over the cellular network is that some networks dynamically re-compress images uploaded over their networks. This means that the quality of JPEG images can be significantly degraded, with visible artefacts.

This problem is all the more subtle, as it will not be detected when developing an app in the simulator due to the fact that the development platform is unlikely to be connected to a cellular network.

Luckily, re-compression can be avoided by the inclusion of simple HTTP header parameters...

request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString: URL]
cachePolicy: NSURLRequestReloadRevalidatingCacheData
timeoutInterval: 60.0f];

// Add some header parameters to ensure that images are not compressed over a mobile network
[request addValue: @"no-cache"
forHTTPHeaderField: @"Pragma"];

[request addValue: @"no-cache"

NSData *imageData = [NSURLConnection sendSynchronousRequest: request
returningResponse: &response
error: &error];
// Convert data to an image (if we were successful)
if ((imageData != nil) && (error == nil))
originalImage = [[[UIImage alloc] initWithData: imageData] autorelease];

Please note that for the sake of brevity, error checking and garbage collection has been omitted in this example.
blog comments powered by Disqus