检查NSDocumentDirectory中的现有图像 [英] Check existing image in NSDocumentDirectory

查看:67
本文介绍了检查NSDocumentDirectory中的现有图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个选择器,将我的图像保存在 NSDocumentDirectory 中,在这里:

I have a picker that saves my image in NSDocumentDirectory, here:

        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是否已存在.然后,如果存在图像,即使仍在选择器中选择了图像/图像名称,也不会添加相同的图像/图像名称.

How to determine or implement to check if an existing image/imageName is already there. Then if an images exist it will not add the same image/imageName even if its still picked in the picker.

推荐答案

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

Use the below code for checking a specific image is already exists in the documents directory:

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

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

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

In your code you can add it like this:

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

这篇关于检查NSDocumentDirectory中的现有图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆