Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

DennisBlah

macrumors 6502
Original poster
Hey all, I'm trying to store images locally for offline use.
The code below works fine, in simulator, but on a actual device (iPhone 5S on iOS7.1.2) it doesn't store the image to the documents folder
Code:
                NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                
                NSData *imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: [NSString stringWithFormat: @"%@images/%@/%@", theHost, theSub, theImageName]]];
                UIImage *image = [[UIImage alloc] initWithData: imageData];
                NSString *jpegFilePath = [NSString stringWithFormat:@"%@/%@/%@", [paths objectAtIndex:0], theSub, theImageName];
                
                NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation(image, 6.0f)];
                [data2 writeToFile:jpegFilePath atomically:YES];

However I'm doing the same thing with a html page, in same sub, and that works fine
Code:
NSString *filePath = [NSString stringWithFormat:@"%@/%@/%@/index.html", [paths objectAtIndex:0], theSub, pageName];
    
    // Download and write to file
    NSURL *downloadURL = [NSURL URLWithString: url];
    NSData *urlData = [NSData dataWithContentsOfURL:downloadURL];

    [urlData writeToFile:filePath atomically:YES];

p.s. before starting I am checking if the sub exists, also at the image, if not, then creating it.

Hopefully someone can help me out because I really don't understand this thing... In simulator it works fine :-(

---------
Sorry it was a stupid fault. At the image check it never met the if statement 😱

Cheers!😎
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.