检查资源/项目导航器中是否存在文件

时间:2011-09-28 11:46:52

标签: iphone objective-c xcode uiimage

我需要检查项目导航器中是否存在图像。如果不是,则需要从互联网上下载。目标是能够使用像[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"];  

我感觉我将图像保存在与保存它们不同的目录中。 你对我该怎么做有什么想法吗?

3 个答案:

答案 0 :(得分:2)

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];
}

答案 1 :(得分:1)

NSData * data = [NSData dataWithData:UIImageJPEGRepresentation([UIImage imageWithContentsOfFile:imagePath],0.8)]; [data writeToFile:imagePath atomically:YES];

答案 2 :(得分:0)

我做了类似的事情..这里有一些代码的摘录......不是完整的代码......

- (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"];

    }

}
}
相关问题