检查NSDocumentDirectory中的现有图像

时间:2012-06-27 03:58:05

标签: iphone ios xcode

我有一个选择器可以将我的图像保存在NSDocumentDirectory,此处:

        for (int i = 0; i < image.count; i++) {
            NSLog(@"%@", [image objectAtIndex:i]);
            NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,    NSUserDomainMask ,YES );
            NSString *documentsDirec = [paths objectAtIndex:0];
            NSString *savedPath = [documentsDirec stringByAppendingPathComponent:[NSString stringWithFormat:@"myImages%d.png", i]];
            ALAssetRepresentation *rep = [[image objectAtIndex: i] defaultRepresentation];
            UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage]];
            NSData *imageData = UIImagePNGRepresentation(image);
            [imageData writeToFile:savedPath atomically:YES];

            NSLog(@"saving at:%@",savedPath);
         }

如何确定或实施以检查现有的image / imageName是否已存在。然后,如果存在图像,即使它仍然在拾取器中被拾取,它也不会添加相同的image / imageName。

3 个答案:

答案 0 :(得分:4)

使用以下代码检查文档目录中是否已存在特定图像:

NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSString *imageName = [documentsPath stringByAppendingPathComponent:@"imageName.jpg"];
BOOL fileExists = [[NSFileManager defaultManager] imageName];

在您的代码中,您可以像这样添加:

 for (int i = 0; i < image.count; i++) {
                NSLog(@"%@", [image objectAtIndex:i]);
                NSArray *paths = NSSearchPathForDirectoriesInDomains(  NSDocumentDirectory,    NSUserDomainMask ,YES );
                NSString *documentsDirec = [paths objectAtIndex:0];
                NSString *savedPath = [documentsDirec stringByAppendingPathComponent: [NSString stringWithFormat:@"myImages%d.png", i]];

                BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:savedPath];

                //Save only if file not exists
                if(!fileExists) {
                ALAssetRepresentation *rep = [[image objectAtIndex: i] defaultRepresentation];
                UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage]];
                NSData *imageData = UIImagePNGRepresentation(image);
                [imageData writeToFile:savedPath atomically:YES];

                NSLog(@"saving at:%@",savedPath);
               }
             }

答案 1 :(得分:1)

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"imageName.jpg"];

        if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
            NSLog(@"file exists at the path");
        }
        else
            NSLog(@"file doesnt exist");

我认为这会对你有所帮助。

答案 2 :(得分:0)

这样做:

     for (int i = 0; i < image.count; i++) 
     {
        NSLog(@"%@", [image objectAtIndex:i]);
        NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,    NSUserDomainMask ,YES );
        NSString *documentsDirec = [paths objectAtIndex:0];
        NSString *savedPath = [documentsDirec stringByAppendingPathComponent:[NSString stringWithFormat:@"myImages%d.png", i]];
        if(![[NSFileManager defaultManager]fileExistsAtPath:savedPath]) //if not exits then save image
        {
         ALAssetRepresentation *rep = [[image objectAtIndex: i] defaultRepresentation];
         UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage]];
         NSData *imageData = UIImagePNGRepresentation(image);
         [imageData writeToFile:savedPath atomically:YES];
         NSLog(@"saving at:%@",savedPath);
        }
     }