【问题标题】:Check if file exists in resourses/project navigator检查资源/项目导航器中是否存在文件
【发布时间】:2011-09-28 11:46:51
【问题描述】:

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

我感觉我将图像保存在不同的目录中,而不是我应该将它们保存到的目录中。 你知道我应该怎么做吗?

【问题讨论】:

    标签: iphone objective-c xcode uiimage


    【解决方案1】:

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

    【讨论】:

    • 有没有办法将图片保存到 app bundle 中?
    • 感谢您的来信!但是由于应用程序包是“只读”的,有没有办法检查这个包中是否存在文件?我目前正在尝试检查它是否存在,如下所示: NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *path = [documentsDirectory stringByAppendingPathComponent:@"87_tn.jpg"]; if(![fileManager fileExistsAtPath:path]) { NSLog(@"它不存在"); }
    • 您不是在检查应用程序包,而是在检查文档目录。如果imageNamed 返回nil,则应用程序包中没有该名称的图像。或者您可以检查[[NSBundle mainBundle] pathForResource:@"123123" ofType:@"jpg"],这将为您提供一个图像路径,您可以使用NSFileManager 进行检查
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-05-20
        • 1970-01-01
        • 1970-01-01
        • 2021-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-11
        相关资源
        最近更新 更多