我发布这个问题是因为我有一个完整的答案,这篇文章是针对另一篇文章写的,当时我发现它不适用于原版,但我觉得它太浪费了.因此,我也将其作为社区维基,以便其他人可以充实问答.如果您觉得答案有用,请投票提出问题 - 作为社区维基我不应该为此投票获得积分,但它会帮助其他人找到它
如何获取iPhone上允许写入文件的路径?您可以(误导性地)在模拟器上随意写任何地方,但在iPhone上,您只能写入特定位置.
我的应用程序正在使用它NSDocumentDirectory来保存图像,我只想询问它是否是保存图像的安全方式(最多100个).我已经阅读了几个关于它的问题的线索和问题,虽然我不知道该跟哪个.有人说可以保存在那里.有人说我不应该NSDocumentDirectory用来储蓄,因为它会被备用iCloud.那么我在哪里可以保存它,当用户退出应用程序然后再次运行应用程序,然后图像应该仍在那里?我不太了解tmp目录或cache目录.但如果它是我应该使用的2个中的任何一个,我如何在我的代码中使用它们:
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask ,YES );
NSString *documentsDir = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Images%d.png", i]];
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];
Run Code Online (Sandbox Code Playgroud)
非常感谢你的帮助.