Reputation: 216
I am downloading an image in iOS in the following format:
"Content-Encoding" = gzip;
"Content-Type" = "text/html";
Date = "Thu, 31 Oct 2013 19:08:58 GMT";
Expires = "Thu, 01 Jan 1970 00:00:00 GMT";
"Set-Cookie" = "JSESSIONID=1mrh644zbpgutn1xk116n825u;Path=/";
"Transfer-Encoding" = Identity;
I am trying to use this: https://github.com/st3fan/cocoa-utils/blob/master/src/NSDataGZipAdditions.m to do the unarchiving... but it does not seem to be working.
Here is my current, unworking code:
NSString *authHeader = [NSString stringWithFormat:@"OAuth %@", credentials.accessToken];
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:myURL];
[request addValue:authHeader forHTTPHeaderField:@"Authorization"];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue new] completionHandler:^(NSURLResponse *response, NSData *data, NSError *err) {
NSLog(@"Response: %@", response);
if (err) {
NSLog(@"Error: %@", err);
}
//here create file from _data_
NSData *mydata = [NSData dataWithCompressedData:data];
self.propImg1.image = [UIImage imageWithData:mydata];
[self.propImg1 setNeedsLayout];
Does anyone know how to accomplish this?
Thanks
Upvotes: 1
Views: 544
Reputation: 437592
I'd suggest checking out the response's statusCode
and make sure it's 200
.
You say your server reported a Content-Encoding
of
"Content-Encoding" = gzip;
that doesn't mean your NSData
is gzip data. I believe that some web servers can transparently gzip their responses, and iOS transparently unzips that for you. You generally don't have to use a gzip library to unzip for most web server requests.
This is evidenced by your Content-Type
would suggest the response wasn't an image:
"Content-Type" = "text/html";
Admittedly, we should hesitate to draw too many conclusions from the Content-Type
(because some custom web services are sloppy about setting that), but it is not consistent with your assertion that the resulting NSData
is gzip data.
You've added a comment below, showing us the NSData
, and that is, in fact, a string script. It looks like it's probably HTML, not an image.
I'd NSLog
the data
(or set a breakpoint inside this block and issue the po data
command in the debugger) and see what it looks like, and eliminate the ambiguity here. If it's largely hex values between 20
and 7f
(plus the occasional 0a
and possibly even a 0d
), that suggests that the response is a string, which you could then log it as a string.
Perhaps you can update your question with the first few lines of the data
hex dump, and we can help you diagnose what's going on (as you can often look at the first few bytes and confirm if it's text, a gzip, or an image).
Or, now that you've confirmed it's a string response (that looks like it might be HTML), you should convert it to a NSString
and log it, and you'll see what's going on.
By the way, when you fix your request issue, since you're doing UI update in your completion block, either use [NSOperationQueue mainQueue]
for your completion block, or make sure to dispatch the UI update back to the main queue (as I've done in my example below).
Thus:
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue new] completionHandler:^(NSURLResponse *response, NSData *data, NSError *err) {
NSLog(@"Response: %@", response);
if (err) {
NSLog(@"Error: %@", err);
}
NSInteger statusCode = -1;
if ([response isKindOfClass:[NSHTTPURLResponse class]])
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
statusCode = httpResponse.statusCode;
}
NSLog(@"statusCode = %d", statusCode); // this should be 200
NSLog(@"data = %@", data); // if really text, you'll largely see values b/w 20 and 7f and the occasional 0a
// if it does look like largely 20-7f and a few 0a values, then try displaying it as a string:
//
// NSLog(@"data string = %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//here create file from _data_
NSData *mydata = [NSData dataWithCompressedData:data];
if (mydata) {
UIImage *image = [UIImage imageWithData:mydata];
if (image) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
self.propImg1.image = image;
[self.propImg1 setNeedsLayout];
}];
}
}
// ...
}];
Upvotes: 1