2012-07-09 68 views
0

我想将一些图像保存在本地计算机上的文件夹(示例文档文件夹)上。然后将该url保存在sqlite数据库中。我怎样才能做到这一点 ?我也想检索图像,然后显示它。保存并检索图像 - Ios

回答

2

您可以浏览到您的应用程序的文件夹,然后可以使用UIImagePNGRepresentation,返回保存UIImageNSData

示例代码:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDir = [paths objectAtIndex:0]; 
NSString *imgPath = [documentsDir stringByAppendingPathComponent:@"savedImage.png"]; 
NSData *imageData = UIImagePNGRepresentation(image); // image is the UIImage you want to save 
[imageData writeToFile:imgPath atomically:YES]; 

然后你只需要把路径/文件名在数据库中,当你需要它检索。

1

获取图像的NSData的: NSData *imageData = UIImagePNGRepresentation(image); ,然后写入文件: [imageData writeToFile:filepath atomically:YES];