【发布时间】:2011-02-01 08:29:57
【问题描述】:
我正在编写一个应用程序,它将捆绑包的一些内容复制到应用程序文档的目录中,主要是图像和媒体。然后我从 Document 的目录访问整个应用程序中的这个媒体。
这在模拟器中完全可以正常工作,但在设备上却不行。资产只是出现为空。我已经完成了 NSLog 并且文件的路径看起来是正确的,并且我通过在控制台中转储文件列表来确认文件存在于目录中。
有什么想法吗?谢谢!
编辑
这是复制到文档目录的代码
NSString *pathToPublicationDirectory = [NSString stringWithFormat:@"install/%d",[[[manifest objectAtIndex:i] valueForKey:@"publicationID"] intValue]];
NSString *manifestPath = [[NSBundle mainBundle] pathForResource:@"content" ofType:@"xml" inDirectory:pathToPublicationDirectory];
[self parsePublicationAt:manifestPath];
// Get actual bundle path to publication folder
NSString *bundlePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:pathToPublicationDirectory];
// Then build the destination path
NSString *destinationPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d", [[[manifest objectAtIndex:i] valueForKey:@"publicationID"] intValue]]];
NSError *error = nil;
// If it already exists in the documents directory, delete it
if ([fileManager fileExistsAtPath:destinationPath]) {
[fileManager removeItemAtPath:destinationPath error:&error];
}
// Copy publication folder to documents directory
[fileManager copyItemAtPath:bundlePath toPath:destinationPath error:&error];
我正在用这种方法找出 docs 目录的路径:
- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
}
这是我如何构建图像路径的示例
path = [NSString stringWithFormat:@"%@/%d/%@", [self applicationDocumentsDirectory], [[thisItem valueForKey:@"publicationID"] intValue], [thisItem valueForKey:@"coverImage"]];
【问题讨论】:
-
根据我的经验,模拟器的访问限制较少。我相信我遇到过模拟器让我直接写入包的情况。一些代码将有助于了解发生了什么。
标签: iphone iphone-sdk-3.0 ios-simulator