检查文件是否存在资源/项目导航器
问题描述:
我需要检查项目导航器中是否存在图像。如果没有,它需要从互联网上下载。目标是能够使用像[UIImage imageNamed:@“23451.jpg”];检查文件是否存在资源/项目导航器
所以我需要检查图像是否存在,如果没有下载它。我试图做到这一点如下:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//Set image
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"12345.jpg"];
//Check if the image exists at a given path
BOOL imageExists = [[NSFileManager defaultManager] fileExistsAtPath:imagePath];
//If the image doesn't exists download it.
if (!imageExists)
{
NSData* data = UIImageJPEGRepresentation([UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"www.mywebsite.com/12345.jpg"]]]);
[data writeToFile:imagePath atomically:YES];
}
如果一切顺利,图像保存在上述路径。但我还是我不能够使用这个图像与
[UIImage imageNamed:@"12345.jpg"];
我已经得到了我节省了图像在不同的目录,比我应该将它们保存到的感觉。 你有什么想法我应该怎么做?
答
imageNamed
只会在应用程序包中处理表单图像,而不是存储在Document目录中的表单图像。而且您无法将图像添加到应用程序包中,只读。
if (!imageExists)
{
NSData* data = UIImageJPEGRepresentation([UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"www.mywebsite.com/12345.jpg"]]]);
[data writeToFile:imagePath atomically:YES];
}
有没有必要做这个,你现在是一个JPG JPG:
if (!imageExists)
{
NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.mywebsite.com/12345.jpg"]];
[data writeToFile:imagePath atomically:YES];
}
答
我没有类似的东西..在这里,在代码中的一些摘录..不是完整的代码.. 。
- (void)loadAndStoreImage{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSURL *theurlPortrait = [[NSURL alloc] initWithString:[[DataController sharedDataController].imageResponse objectForKey:@"portraitImageUrl"] ];
NSLog(@"Image url %@ ",theurlPortrait);
imageDataPortrait = [NSData dataWithContentsOfURL:theurlPortrait];
[theurlPortrait release];
imagePortrait= [[UIImage alloc]initWithData:imageDataPortrait];
oldPathPortrait = [documentsDirectory stringByAppendingPathComponent:@"pt_wallpaper_1.png"];
NSURL *theurlLandscape = [[NSURL alloc] initWithString:[[DataController sharedDataController].imageResponse objectForKey:@"landscapeImageUrl"]];
imageDataLandscape = [NSData dataWithContentsOfURL:theurlLandscape];
[theurlLandscape release];
imageLandscape= [[UIImage alloc]initWithData:imageDataLandscape];
oldPathLandscape = [documentsDirectory stringByAppendingPathComponent:@"ls_wallpaper_1.png"];
[self replaceImages];
}
-(void)replaceImages{
//check if images were fully downloaded or corrupted.
if (isUserSignedIn) {
if (imageDataPortrait && imageDataLandscape) {
[UIImagePNGRepresentation(imagePortrait) writeToFile:oldPathPortrait atomically:YES];
[UIImagePNGRepresentation(imageLandscape) writeToFile:oldPathLandscape atomically:YES];
NSLog(@"IMAGES REPLACED");
NSDate *currentDate=[NSDate date];
[[ NSUserDefaults standardUserDefaults] setObject:currentDate forKey:@"previousDate"];
}
}
}
答
的NSData *数据= [NSData的dataWithData:UIImageJPEGRepresentation([UIImage的imageWithContentsOfFile:的ImagePath],0.8)]; [data writeToFile:imagePath atomically:YES];
有没有将图像保存到应用程序包的方法? –
刚刚编辑我的答案,没有。它只读。 – rckoenes
感谢您的注意!但是由于应用程序包是'只读',有没有办法检查这个包中是否存在文件?我目前正在试图检查它是否存在如下:NSFileManager * fileManager = [NSFileManager defaultManager]; NSString * documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0]; NSString * path = [documentsDirectory stringByAppendingPathComponent:@“87_tn.jpg”];如果(![fileManager fileExistsAtPath:path]) { NSLog(@“It does exist”); } –