如何检索照片库路径?
我正在我的应用程序中使用照片库中保存的照片。 如何检索照片库中保存的照片的路径?
我已将该 UIImage
转换为 NSData
并将该数据保存在应用程序的沙箱中(在一个文件中)。使用 sqlite 创建了一个数据库并将文件路径保存在数据库中。 每当我需要图像时,我都会从数据库中保存的文件路径检索 NSData
。
NSMutableData *data=[[NSMutableData alloc]init];
data =UIImagePNGRepresentation([UIImageimageNamed:@"image.png"]);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"first.txt"]; [data writeToFile:filePath atomically:YES];
NSString *m=[[@"'"stringByAppendingFormat:filePath]stringByAppendingFormat:@"'"];
NSString *query=[[@"INSERT INTO newtable (image) VALUES (" stringByAppendingFormat:m]stringByAppendingFormat:@")"];
NSLog(@"query....%@",query);
[obj configureDatabase:@"Editerdb.rdb"];//function to configure database
[obj insertInTable:query];// function to insert into db
这段代码正在运行。 有什么简单的方法可以做到这一点吗?
I am using photos saved in photo library in my application.
how can I retrieve path of photos saved in photo library?
I had converted that UIImage
into NSData
and saved that data in application's sandbox(in one file).Using sqlite , I have created a database and saving file path in database.
Whenever i need image, I retrieve NSData
from file path saved in database.
NSMutableData *data=[[NSMutableData alloc]init];
data =UIImagePNGRepresentation([UIImageimageNamed:@"image.png"]);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"first.txt"]; [data writeToFile:filePath atomically:YES];
NSString *m=[[@"'"stringByAppendingFormat:filePath]stringByAppendingFormat:@"'"];
NSString *query=[[@"INSERT INTO newtable (image) VALUES (" stringByAppendingFormat:m]stringByAppendingFormat:@")"];
NSLog(@"query....%@",query);
[obj configureDatabase:@"Editerdb.rdb"];//function to configure database
[obj insertInTable:query];// function to insert into db
This code is working.
Is there any simple way to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,在第一行中,您将遇到内存泄漏。查看
UIImagePNGRepresentation
上的文档以获取帮助。所以删除第一行代码。并使用NSData
而不是NSMutableData
因为您不会更改图像的内容。其次,您不会将图像路径保存在数据库中,而是再次保存图像本身,以供参考
UIImagePNGRepresentation
上的文档,因此当想要图像时只需检索NSData
对象从数据库中获取并使用imageWithData:
将NSData
转换为UIImage
Well first of all in the first line you are going to have a memory leak. Check the documentation on
UIImagePNGRepresentation
to for assistance. so remove the the first line of code. and useNSData
instead ofNSMutableData
coz you are not changing the contents of image.and second of all you are not saving the image path in database but the image itself again for this referrer to documentation on
UIImagePNGRepresentation
so when to want to image just retrive theNSData
object from database and convert theNSData
toUIImage
usingimageWithData: