Corrupted PNG files from ImageFetch


Thanks for the great book, I just left an Amazon review for it.

I have a question on ImageFetch. I was able to get the program to run and felt like I understood the concepts. The file google.png appears in /tmp and looks to be a reasonable size, about 12k. But when trying to use Preview to see it, I get “file could not be opened. It may damaged or use a file format that Preview does not recognize”.

I’m thinking I mistyped something, but I can’t see it.

[code]#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])

@autoreleasepool {
    NSURL *url = [NSURL URLWithString:@""];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSError *error = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request
    if (!data) {
        NSLog(@"fetch failed: %@", [error localizedDescription]);
        return 1;
    NSLog(@"The file is %lu bytes", [data length]);
   BOOL written = [data writeToFile:@"/tmp/google.png"
    if (!written) {
        NSLog(@"write failed: %@", [error localizedDescription]);
        return 1;
    NSData *readData = [NSData dataWithContentsOfFile:@"/tmp/google.png"];
    NSLog(@"The file is %lu bytes", [readData length]);
return 0;




If you open that file in a text editor, you’ll see that it is a HTTP 404 error page.

It should be

Thanks for the good review! It makes a big difference.