从缓存文件夹中删除过时的图像

时间:2014-09-08 19:59:26

标签: ios objective-c

所以我有一个从我的网络服务返回的图像。我按以下格式保存图像:

LOGO_3100B874-4D92-E211-AAF5-001A6426DE62_20140908095804_BIG_HD.GIF

现在,如果有人更新了我服务器上的图像,则日期参数会发生如下变化:

LOGO_3100B874-4D92-E211-AAF5-001A6426DE62_20140908095895_BIG_HD.GIF

现在我的代码将使用最新的图像(因为该文件不存在)现在,我无法弄清楚如何找到旧的图像文件路径并将其从缓存文件夹。我该怎么做呢?这是我用来保存手机的代码:

   NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/logos/LOGO_%@_%@_%@_%@.GIF",[[NSUserDefaults standardUserDefaults]objectForKey:@"urlIMG"],exhibtorKeyId,formatedDate,PhotoSize,[[NSUserDefaults standardUserDefaults]objectForKey:@"screenResolution"]]];

NSString *imagePath = [PATH stringByAppendingPathComponent:[NSString stringWithFormat:@"LOGO_%@_%@_%@_%@.GIF",exhibtorKeyId,formatedDate,PhotoSize,[[NSUserDefaults standardUserDefaults]objectForKey:@"screenResolution"]]];




NSString *imgurl;

if(![[NSFileManager defaultManager] fileExistsAtPath:imagePath])
{

    NSString * oldImageName = [NSString stringWithFormat:@"LOGO_%@_%@_%@",exhibtorKeyId,PhotoSize,[[NSUserDefaults standardUserDefaults]objectForKey:@"screenResolution"]];

#define PATH [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]


    NSString * oldImageFilePath = [PATH stringByAppendingPathComponent:oldImageName];


    NSString *pathAndFileName = ??//How would I get the file with the old date? 
    if ([[NSFileManager defaultManager] fileExistsAtPath:pathAndFileName])
    {
        NSLog(@"Rmove Image");
    }
    else
    {
        NSLog(@"File not found");
    }

    NSError *error = nil;

    NSData *data = [NSData dataWithContentsOfURL:url options:0 error:&error];

    if(!error)
    {
        [data writeToFile:imagePath options:0 error:&error];
        image = [UIImage imageWithContentsOfFile:imagePath];
    }
    else
    {
        image = [UIImage imageNamed:@"empty_profile.png"];
    }

}
else

     image = [UIImage imageWithContentsOfFile:imagePath];

    imgurl = model.customText1;

希望我有意义。谢谢!

1 个答案:

答案 0 :(得分:0)

所以我想出了一个方法。我将以前的图像路径存储在NSUserDefaults中。以下是要遵循的代码:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/logos/LOGO_%@_%@_%@_%@.GIF",[[NSUserDefaults standardUserDefaults]objectForKey:@"urlIMG"],exhibtorKeyId,formatedDate,PhotoSize,[[NSUserDefaults standardUserDefaults]objectForKey:@"screenResolution"]]];


    NSString *imagePath = [PATH stringByAppendingPathComponent:[NSString stringWithFormat:@"LOGO_%@_%@_%@_%@.GIF",exhibtorKeyId,formatedDate,PhotoSize,[[NSUserDefaults standardUserDefaults]objectForKey:@"screenResolution"]]];



    if (![[NSFileManager defaultManager] fileExistsAtPath:imagePath]) {

        NSString * oldImageName = [NSString stringWithFormat:@"LOGO_%@_%@_%@",exhibtorKeyId,PhotoSize,[[NSUserDefaults standardUserDefaults]objectForKey:@"screenResolution"]];


        if ([[NSUserDefaults standardUserDefaults]objectForKey:oldImageName]) {

            NSError *error = nil;
            NSString *removeItemPath = [[NSUserDefaults standardUserDefaults]objectForKey:oldImageName];

            [[NSFileManager defaultManager] removeItemAtPath:removeItemPath error:&error];
            [[NSUserDefaults standardUserDefaults]setObject:nil forKey:oldImageName];

        }


    }
    NSString *imgurl;

    if(![[NSFileManager defaultManager] fileExistsAtPath:imagePath])
    {    
        NSString * oldImageName = [NSString stringWithFormat:@"LOGO_%@_%@_%@",exhibtorKeyId,PhotoSize,[[NSUserDefaults standardUserDefaults]objectForKey:@"screenResolution"]];

        [[NSUserDefaults standardUserDefaults]setObject:imagePath forKey:oldImageName];

        NSError *error = nil;

        NSData *data = [NSData dataWithContentsOfURL:url options:0 error:&error];

        if(!error)
        {
            [data writeToFile:imagePath options:0 error:&error];
            image = [UIImage imageWithContentsOfFile:imagePath];
        }
        else
        {
            image = [UIImage imageNamed:@"empty_profile.png"];
        }

    }
    else
         image = [UIImage imageWithContentsOfFile:imagePath];