// 判断文件是否已经在沙盒中已经存在
+ (BOOL)isFileExist:(NSString *)fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSString *filePath = [path stringByAppendingPathComponent:fileName];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL result = [fileManager fileExistsAtPath:filePath];
return result;
}
注:NSBundle读取文件是安装包里的文件,[UIImage imageNamed:@""],这种形式都可以用 NSBundle来读取。
保存在沙盒里的文件只能去拼接路径去读取。如下:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSString *filePath = [path stringByAppendingPathComponent:fileName];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];