这里有一些对我有用的代码片段。
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *mapDirPath = [documentsDirectory stringByAppendingPathComponent:@"/MapCache"];
// 在文档目录中创建一个目录
NSError* error;
if (![[NSFileManager defaultManager] fileExistsAtPath:mapDirPath])
[[NSFileManager defaultManager] createDirectoryAtPath:mapDirPath withIntermediateDirectories:NO attributes:nil error:&error];
// 加载图像
NSString *filePath = [self.localImagePath stringByAppendingPathComponent: key];
if ( [[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
UIImage* image = [UIImage imageWithContentsOfFile:filePath];
return image;
}
// 写入图片
[[NSFileManager defaultManager] createFileAtPath:filePath contents:remoteData attributes:nil];
希望有帮助
当然,如果你从你的包中读取图像,你需要这个 [[NSBundle mainBundle] pathForResource: @"filename" ofType:@"png" directory: @"directory1"]