I am downloading images from my server. Some clients might have 3 images and another might have 1. When the app starts I have an NSThread that runs in the background loading the images. All the image have the same name like image1.jpg, image2.jpg and so on.
I wrote some code that would check the link for the file and if it returned 'nil' it would skip the file. The NSURL is not working like I was hoping. Even if the file is not in the clients folder it still comes back as true. It firsts checks to see if the file is local already.
I am giving the NSURL the address where the file should be if it exists. Even if it is not there, the IF statement bellow still evaluates to true? What am I missing?
I wrote some code that would check the link for the file and if it returned 'nil' it would skip the file. The NSURL is not working like I was hoping. Even if the file is not in the clients folder it still comes back as true. It firsts checks to see if the file is local already.
Code:
photoNameArray = [NSArray arrayWithObjects:@"image1.jpg",@"image2.jpg",@"image3.jpg", nil];
for (int x = 0; x < [photoNameArray count]; x++) {
NSString *testForLocalImageFileAtPath = [NSString stringWithFormat:@"%@/clients/%@/%@",[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],nameWithWhiteSpacesRemoved, [photoNameArray objectAtIndex:x]]; //creates a string path for the image files localy, it does not check the path, just the string.
//NSLog(@"test for image %@",testForLocalImageFileAtPath);
if (![fileManager fileExistsAtPath:testForLocalImageFileAtPath]){
NSString *pathToImages = [NSString stringWithFormat:@"http://www...........com/app/clients/%@/images/photos/%@", nameWithWhiteSpacesRemoved, [photoNameArray objectAtIndex:x]]; //if file is not local, get it on the internet.
NSURL *checkURL = [[NSURL alloc] initWithString:pathToImages];// check for file first
NSLog(@"path to URL %@", testForLocalFileAtPath);
NSLog(@"the URL %@", checkURL);
if (checkURL != nil) {
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:pathToImages]]]; //Loads the file from the internet to a UIImage
[self saveImageToDir:image withDirectoryName:nameWithWhiteSpacesRemoved withImageNumber:x]; //send message to method to to save the image to a local directory.
}
}
}
I am giving the NSURL the address where the file should be if it exists. Even if it is not there, the IF statement bellow still evaluates to true? What am I missing?