保存和检索图像-Ios
我想将一些图像保存在本地计算机上的文件夹(示例文档文件夹)中。然后将url保存在sqlite数据库中。我该怎么做?我还想检索图像,然后显示它。获取图像的NSData:保存和检索图像-Ios,ios,image,sqlite,Ios,Image,Sqlite,我想将一些图像保存在本地计算机上的文件夹(示例文档文件夹)中。然后将url保存在sqlite数据库中。我该怎么做?我还想检索图像,然后显示它。获取图像的NSData: NSData*imageData=UIImagePNGRepresentation(图像) 然后写入文件: [imageData WriteFile:filepath原子化:是]获取图像的NSData: NSData*imageData=UIImagePNGRepresentation(图像) 然后写入文件: [imageData
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];
然后,您只需将路径/文件名放入数据库中,并在需要时检索它