1

I'm having an issue whenever I am saving a .png image to my iOS Applications.

-(UIImage *) getImageFromURL:(NSString *)fileURL {
UIImage * result;

NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
result = [UIImage imageWithData:data];

return result;
}

-(void) saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
if ([[extension lowercaseString] isEqualToString:@"png"]) {
    [UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]] options:NSAtomicWrite error:nil];
} else if ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"]) {
    [UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];
} else {
    NSLog(@"Image Save Failed\nExtension: (%@) is not recognized, use (PNG/JPG)", extension);
}
}

The code above is what I'm using to download the images with, passing it a url to an image and then "png" as the file type.

For some reason, the image saves onto the devices, but it saves in an incorrect format. I've tested this by pulling up the iPhone Simulator program application folder and looking at the images directly. They are indeed saved, but even Mac OS X can't open them up as "Images" with Preview.

Am I doing something wrong? These images are actual png images that I'm just downloading to the device. This was previously working a week ago, now it isn't.

  • Try http://stackoverflow.com/questions/2499176/ios-download-image-from-url-and-save-in-device might help you in your code. – iCoder86 Jul 24 '12 at 11:26
  • This is the code that was working before that I had, but then last week it just suddenly stopped working properly, on the simulator and on actual devices. It literally worked one day, then the next day, no changes were made, nothing, and it just stopped working. – user1546813 Jul 24 '12 at 19:30

0 Answers0