gpt4 book ai didi

iphone - 保存在 NSDocumentDirectory 中

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:50:21 24 4
gpt4 key购买 nike

我的 NSDocumenDirectory 保存有奇怪的问题。这是一个预览:首先,我选择图像(在我的 imagePickerViewController 中):

enter image description here

在我的 PreviewController 中:

enter image description here

所以第一次尝试,没问题。

然后我重新访问 imagePickerViewController 以添加另一张图片:

enter image description here

在我的 PreviewController 中:

enter image description here

这就是问题所在。在上图中,它从旧预览中复制了最后一张图像(就像副本一样)。我不知道我在我的代码中做错了什么。但是我在文件存在时保存它。请看:

for (int i = 0; i < info.count; i++) {
NSLog(@"%@", [info objectAtIndex:i]);
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask ,YES );
NSString *documentsDir = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"firstSlotImages%d.png", i]];
if ([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]) {

NSLog(@"file doesnt exist");

} else {
ALAssetRepresentation *rep = [[info objectAtIndex: i] defaultRepresentation];
UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage]];
//----resize the images
image = [self imageByScalingAndCroppingForSize:image toSize:CGSizeMake(256,256*image.size.height/image.size.width)];

NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:YES];

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

我需要的是重新添加与新图像相同的图像。与上次预览相同。

最佳答案

四张图片按它们在预览中显示的顺序传递,因此在第一个示例中,橙色猫排在第三位,在第二个示例中,橙色猫排在第四位。新图像没有保存,因为它是第三个,并且您已经有一个名为“firstSlotImages2.png”的文件。如果您在不检查文件是否存在的情况下重新保存每个图像,您应该会得到您要查找的结果。

媒体信息中有一个键:UIImagePickerControllerMediaURL,它返回一个 NSURL,将其转换为字符串并获取 lastPathComponent。使用此作为文件名以保存到您要将其保存到的目录。然后,您可以通过将相同的文件名保存在 NSMutableArray 或 NSMutableDictionary 中来保存对这些图像的引用

关于iphone - 保存在 NSDocumentDirectory 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11411135/

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