简体   繁体   English

如何访问存储在iOS8文档目录中的图像

[英]How to access images stored in Documents Directory in ios8

In ios7 we can write images to documents directory and access it without any problem. 在ios7中,我们可以将图像写入文档目录并可以毫无问题地访问它。 In IOs8 i am able to write imagesdata to documents directory and access it. 在IOs8中,我能够将imagesdata写入文档目录并访问它。 The location of images folder changes every time i run the app on the Simulator.I went through many stack overflow questions and apple's Technical Note (link: https://developer.apple.com/library/ios/technotes/tn2406/_index.html ) but i didn't get any solution.Actually i am storing the path of images file in sqlite database. 每次我在模拟器上运行应用程序时,images文件夹的位置都会更改。我遇到了许多堆栈溢出问题和Apple的技术说明(链接: https//developer.apple.com/library/ios/technotes/tn2406/_index。 html ),但我没有任何解决方案。实际上,我将图像文件的路径存储在sqlite数据库中。

Any help appreciated. 任何帮助表示赞赏。

Seems like you are storing the complete path. 似乎您正在存储完整路径。

In IOS 8 Apple changed the directory structure.Apple extended folders in documents directory internally(ie something like /developer/data/some random number sequence).Directory folders keeps on changing on every build,if u store the complete path in your sqlite then you do not find any file at the location because the documents directory location has changed. 在IOS 8中,Apple更改了目录结构。Apple在内部扩展了文档目录中的文件夹(即/ developer / data /一些随机数序列)。目录文件夹在每次构建时都会不断变化,如果您将完整路径存储在sqlite中,您未在该位置找到任何文件,因为文档目录位置已更改。

The solution is do not store the complete path just store the path from documents directory into the database,while accessing images just prefix it with current application documents directory, then you are able to access your images from your apps directory 解决方案是不存储完整路径,而只是将文档目录中的路径存储到数据库中,而访问图像时只需在当前应用程序文档目录中添加图像前缀即可,然后您就可以从应用程序目录中访问图像了

Example: 例:

//Storing Images

NSString *imagePathString = [NSString stringWithFormat:@“/%@/%@.png”,@“Your Folder name”,@“image name”];

//Store this image path String to database

//Retrieving Images

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:imagePathString];

//This is the path for your images

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM