2

我有一个选择器,可以将我的图像保存在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);
         }

如何确定或实施检查现有图像/图像名称是否已经存在。然后,如果图像存在,即使它仍然在选择器中被选中,它也不会添加相同的图像/图像名称。

4

3 回答 3

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);
               }
             }
于 2012-06-27T04:04:30.573 回答
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");

我想这会对你有所帮助。

于 2012-06-27T04:19:20.130 回答
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);
        }
     }
于 2012-06-27T04:24:45.420 回答