So I have an array with images that I want to store locally since downloading them each time will take unnecessary effort. I've read that I should NOT store images in NSUserDefaults, which is fine, but I can't for the life of me find any examples on how to store it as a file in a directory that does not change (iOS 8 changed the UUID with each build which creates a new folder each time I run it in Xcode).
所以我有一个包含我想在本地存储的图像的数组,因为每次下载它们都会花费不必要的精力。我已经读过我不应该在NSUserDefaults中存储图像,这很好,但我不能在我的生活中找到任何关于如何将它作为文件存储在不改变的目录中的示例(iOS 8更改了每次构建的UUID,每次在Xcode中运行时都会创建一个新文件夹。
I generally have two questions here:
我这里一般有两个问题:
-
Could someone help me translate this into ObjC? I can't comment on the post since I don't have enough rep...It's the swift part farther down the post that I need help with. Save images in NSUserDefaults?
有人可以帮我翻译成ObjC吗?由于我没有足够的代表,所以我无法对帖子发表评论......这是我需要帮助的更快的部分。在NSUserDefaults中保存图像?
-
The other question I have is that it seems to take a lot of time to save the data locally, no matter if it's to file or into the NSUserDefaults. What happens here is that the user of my app closes the app before the data has been stored locally. Is there any way to prevent this? I can add an ActivityIndicator, sure, but I can't seem to find any callback which tells me when the process of saving data has been completed.
我的另一个问题是,无论是文件还是NSUserDefaults,都需要花费大量时间在本地保存数据。这里发生的是我的应用程序的用户在本地存储数据之前关闭应用程序。有什么方法可以防止这种情况吗?我可以添加一个ActivityIndicator,但我似乎无法找到任何回调,告诉我保存数据的过程何时完成。
Thanks!
1 个解决方案
#1
So I managed to solve it. The problem was that I simply mixed up the paths. If anyone else wants the translation from Swift to ObjC from the link in the original post here it is:
所以我设法解决了它。问题是我只是混淆了路径。如果其他人想要从原始帖子中的链接从Swift到ObjC的翻译,那么它是:
Write
NSString* relativePath = [NSString stringWithFormat:@"image_%d.jpg", 1];
NSString* realPath = [self documentsPathForFileName:relativePath];
// Write image data to user's folder
[self.ImageData writeToFile:realPath atomically:YES];
// Store path in NSUserDefaults
[defaults setObject:relativePath forKey:@"path"];
Read
NSString *relativePath = [defaults objectForKey:@"path"];
NSString *realPath = [self documentsPathForFileName:relativePath];
self.ImageData = [NSData dataWithContentsOfFile:realPath];
#1
So I managed to solve it. The problem was that I simply mixed up the paths. If anyone else wants the translation from Swift to ObjC from the link in the original post here it is:
所以我设法解决了它。问题是我只是混淆了路径。如果其他人想要从原始帖子中的链接从Swift到ObjC的翻译,那么它是:
Write
NSString* relativePath = [NSString stringWithFormat:@"image_%d.jpg", 1];
NSString* realPath = [self documentsPathForFileName:relativePath];
// Write image data to user's folder
[self.ImageData writeToFile:realPath atomically:YES];
// Store path in NSUserDefaults
[defaults setObject:relativePath forKey:@"path"];
Read
NSString *relativePath = [defaults objectForKey:@"path"];
NSString *realPath = [self documentsPathForFileName:relativePath];
self.ImageData = [NSData dataWithContentsOfFile:realPath];