gpt4 book ai didi

iphone - 检查 NSDocumentDirectory 中的现有图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:32:16 25 4
gpt4 key购买 nike

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

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

最佳答案

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

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

关于iphone - 检查 NSDocumentDirectory 中的现有图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11219202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com