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

既存の image/imageName が既に存在するかどうかを確認または確認する方法。次に、画像が存在する場合、ピッカーでまだ選択されていても、同じ画像/画像名を追加しません。

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 に答える