1

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?

user572555
  • 51
  • 1
  • 2
  • 5

1 Answers1

0

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 use NSData instead of NSMutableData 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 the NSData object from database and convert the NSData to UIImage using imageWithData:

Vineet Singh
  • 4,009
  • 1
  • 28
  • 39
Robin
  • 10,011
  • 5
  • 49
  • 75
  • Thanks.But how can i save NSData in database?(means in which format...(varchar,integer????)) – user572555 Jan 14 '11 at 05:59
  • well sorry for that part but i have not used the sqllite by my self. but you can make a txt file to save the image. google for how to make a text file in iphone – Robin Jan 14 '11 at 06:02
  • follow this link http://howtomakeiphoneapps.com/2009/06/reading-and-writing-text-files-in-iphone-os-3-0/ – Robin Jan 14 '11 at 06:03
  • You can save the data of image in BLOB type in SQLite.That would be helpful. – Sabby Jan 14 '11 at 07:24