保存和检索图像-Ios

保存和检索图像-Ios,ios,image,sqlite,Ios,Image,Sqlite,我想将一些图像保存在本地计算机上的文件夹(示例文档文件夹)中。然后将url保存在sqlite数据库中。我该怎么做?我还想检索图像,然后显示它。获取图像的NSData: NSData*imageData=UIImagePNGRepresentation(图像) 然后写入文件: [imageData WriteFile:filepath原子化:是]获取图像的NSData: NSData*imageData=UIImagePNGRepresentation(图像) 然后写入文件: [imageData

我想将一些图像保存在本地计算机上的文件夹(示例文档文件夹)中。然后将url保存在sqlite数据库中。我该怎么做?我还想检索图像,然后显示它。

获取图像的NSData:
NSData*imageData=UIImagePNGRepresentation(图像)
然后写入文件:

[imageData WriteFile:filepath原子化:是]

获取图像的NSData:
NSData*imageData=UIImagePNGRepresentation(图像)
然后写入文件:

[imageData WriteFile:filepath原子化:是]

您可以导航到应用程序的文档文件夹,然后使用
UIImagePNGRepresentation
保存
UIImage
,返回
NSData

示例代码:

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];   

然后,您只需将路径/文件名放入数据库,并在需要时检索它。

您可以导航到应用程序的文档文件夹,然后使用返回
NSData
UIImagePNGRepresentation
保存
UIImage

示例代码:

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];   
然后,您只需将路径/文件名放入数据库中,并在需要时检索它