【发布时间】:2015-04-12 17:07:20
【问题描述】:
第一次选择和拍照。我有代码可以让用户选择(或获取)个人资料图片,并使用委托模式在应用程序的 uiview 中显示。为了持久化这张图片,很多文章建议你将图像存储在磁盘上,而不是存储在核心数据中,并且只将文件名存储在核心数据中。
我找到了以下存储文件的方法。
- (void)saveImage: (UIImage*)image
{
if (image != nil)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//create a file name perhaps using time and call it
NSString* path = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithString: @"profilefilename.png"] ];
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
}
}
但是,我不知道如何以及在何处调用此方法以将文件存储在磁盘上,以及何时何地存储核心数据。
感谢您的任何建议
【问题讨论】: