在NSDocumentDirectory中保存好吗?

Baz*_*nga 6 iphone xcode ios

我的应用程序正在使用它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)

非常感谢你的帮助.

and*_*cam 19

tmpcache目录定期的iOS清理.如果图像是一般用途,请使用相机胶卷,因为其他两个答案建议.但是,如果这些图像仅用于应用程序的范围,您仍然可以安全地将它们存储在Documents目录中,您只需在保存后包含"从iCloud备份中排除"函数调用每个文件,以防止Apple拒绝你的应用程序使用太多的iCloud空间.当然有一个权衡,禁用这意味着如果用户删除应用程序或获取其他设备(等),用户将丢失他们的照片,但这个警告比不在商店中获取应用程序更可取.

要禁用文件上的iCloud备份,iOS版本> 5.0有两种方法:

UPDATE!将两种方法合并为自动处理iOS版本的单一功能:

#include <sys/xattr.h> // Needed import for setting file attributes

+(BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)fileURL {

    // First ensure the file actually exists
    if (![[NSFileManager defaultManager] fileExistsAtPath:[fileURL path]]) {
        NSLog(@"File %@ doesn't exist!",[fileURL path]);
        return NO;
    }

    // Determine the iOS version to choose correct skipBackup method
    NSString *currSysVer = [[UIDevice currentDevice] systemVersion];

    if ([currSysVer isEqualToString:@"5.0.1"]) {
        const char* filePath = [[fileURL path] fileSystemRepresentation];
        const char* attrName = "com.apple.MobileBackup";
        u_int8_t attrValue = 1;
        int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
        NSLog(@"Excluded '%@' from backup",fileURL);
        return result == 0;
    }
    else if (&NSURLIsExcludedFromBackupKey) {
        NSError *error = nil;
        BOOL result = [fileURL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error];
        if (result == NO) {
            NSLog(@"Error excluding '%@' from backup. Error: %@",fileURL, error);
            return NO;
        }
        else { // Succeeded
            NSLog(@"Excluded '%@' from backup",fileURL);
            return YES;
        }
    } else {
        // iOS version is below 5.0, no need to do anything
        return YES;
    }
}
Run Code Online (Sandbox Code Playgroud)

如果你的应用程序必须支持5.0,那么不幸的是你唯一的选择就是将这些照片保存在Caches目录中,这意味着它们不会被备份(这不会导致App Store拒绝),但每当存储监视程序决定时是时候清理Caches文件夹了,你会丢失那些照片.根本不是一个理想的实现,但这就是5.0中的野兽的本质,苹果在备份排除中加入了事后的想法.

编辑:忘了回答'如何保存到tmp/cache目录'部分问题.如果您决定沿着这条路走下去:

  • 保存到tmp:

    NSString *tempDir = NSTemporaryDirectory();
    NSString *savedImagePath = [tempDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Images%d.png", i]];
    
    Run Code Online (Sandbox Code Playgroud)

(请注意,这似乎在模拟器中没有任何效果,但它在设备上按预期工作)

  • 保存到Cache:

    NSString *cacheDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)lastObject];
    NSString *savedImagePath = [cacheDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Images%d.png", i]];
    
    Run Code Online (Sandbox Code Playgroud)