Corrupted PNG files from ImageFetch


#1

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:@"http://www.google.com/images/logos/ps_logos2.png"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSError *error = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request
                                         returningResponse:NULL
                                                     error:&error];
    if (!data) {
        NSLog(@"fetch failed: %@", [error localizedDescription]);
        return 1;
    }
    
    NSLog(@"The file is %lu bytes", [data length]);
    
   BOOL written = [data writeToFile:@"/tmp/google.png"
                            options:NSDataWritingAtomic
                              error:&error]; 
    
    if (!written) {
        NSLog(@"write failed: %@", [error localizedDescription]);
        return 1;
    }
    NSLog(@"Success");
    NSData *readData = [NSData dataWithContentsOfFile:@"/tmp/google.png"];
    NSLog(@"The file is %lu bytes", [readData length]);
}
return 0;

}

[/code]


#2

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

It should be google.com/images/logos/ps_logo2.png

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