How to retrieve photo library path? - iphone

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?

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:

Related

How to email sqlite database of Iphone app from itself?

In my current app I am adding a data backup and restore feature.
User can create back up of database and can email it.
In restore feature user can import the database from email and can replace the current.
I am able to do create back up and restore of database file.
In restore I am just copying the file from my email to app Documents file replacing current one.
The problem is that in my code for backup of database, sqlite database is converted to NSData. I don't want to to be converted and need the exact sqlite file to be emailed.
Here is the code that I currently use to email sqlite database
-(void)backUpData
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *txtPath = [documentsDirectory stringByAppendingPathComponent:#"/DemoApp.sqlite"];
NSURL *newUrl = [[NSURL alloc] initWithString:
[txtPath stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
NSData *sqliteData = [[NSData alloc] initWithContentsOfURL:newUrl];
//send sqliteData to email composer delegate to attach it as attachment
[self showPicker:sqliteData];
}
How I can email sqlite db without converting it to NSData?
Use dataWithContentsOfFile when creating the NSData object giving the path rather than a url. For example:
NSString *dbPath = [[NSBundle mainBundle] pathForResource:#"Pubs" ofType:#"db"];
NSData *myData = [NSData dataWithContentsOfFile:dbPath];
[mailComposeVC addAttachmentData:myData mimeType:#"application/x-sqlite3" fileName:#"Pubs.db"];

Downloading and saving images in iOS

I am creating an iPhone app and in that I want to download multiple image files from web service. And I want to save the images in the local folder in the app itself and save the path to each files in SQLite database. How can I implement this? Please help.
**// Get an image from the URL below**
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"yorimageurl"]]];
NSLog(#"%f,%f",image.size.width,image.size.height);
**// Let's save the file into Document folder.**
NSString *Dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pngPath = [NSString stringWithFormat:#"%#/test.png",Dir];// this path if you want save reference path in sqlite
NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];
[data1 writeToFile:pngFilePath atomically:YES];
NSLog(#"saving jpeg");
NSString *jpegPath = [NSString stringWithFormat:#"%#/test.jpeg",Dir];// this path if you want save reference path in sqlite
NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];//1.0f = 100% quality
[data2 writeToFile:jpegFilePath atomically:YES];
NSLog(#"saving image done");
[image release];
>> Update Additional:
You need to save image name(unique name that you can do early..right..!!) in sqlite database and you can make path or retrieve image using that name as i shown below.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:#"test.png"];// here you jus need to pass image name that you entered when you stored it.
Hope, this will help you...
After implementing #Nit's code.. you can refer This for insert record in database.
This is showing how to insert record in DB. and for getting path read my comments in #Nit's answer.
UPDATE:
For getting path of each downloaded file you have to keep unique name for each file. Now you will have the path where you are writing file that is unique path for every file. Now you simply need to execute insert query every time you download a file or need to save filepath in array and execute query later according to your requirement. Don't forget to give different file name to every file otherwise only single file will be there and your path will be same.
For getting unique filename you can use timestamp:
NSString *fileName = [NSString stringWithFormat:#"%lf.png", [[NSDate date] timeIntervalSince1970];
UPDATE2:
You have path now like: /Users/John/Library/Application Support/iPhone Simulator/5.1/Applications/84A837AA-7881-4D99-B6E2-623DC9A2E5D3/Documents/test.png
For getting Image in Image view:
UIImage *img = [UIImage imageWithContentsOfFile:yourPath];
//[UIImage imageWithData:[NSData dataWithContentsOfFile:yourPath]];
UIImageView *imgView = // Alloc + Init + Setting frame etc
imgView.image = img;
This will show image in your imageview
Hope this helps

Save images for later use in iPhone

I want to save images that user selects from the camera roll, when app exits and restart i want to show those images to user. I have searched over internet and not found much useful resource. I know I can write the image in database as blob but I dont want to do it as increases the database size. Is it possible to save the image path and then later use that path to access that image.
Save the Image into the DocumentDirectory
NSString *imageName = "myImage.png";
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectoryPath = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectoryPath stringByAppendingPathComponent:imageName];
NSData* settingsData = UIImagePNGRepresentation(imageData);
[settingsData writeToFile:dataPath atomically:YES];
EDIT:
If you want to see your saved images on the iPhone/iPad (or share them) in iTunes->Apps->Documents
jut add in Info.plist "Application supports iTunes file sharing" : "YES"
You have to use UIImagePickerController
to get the media from the Camera roll
You will get the image in the delegate method
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
Here you can get the image and save is as NSData .. in a file and then retrieve back it next time
Edit :
to Save
Data = UIImageJPEGRepresentation(theImage,1.0);
NSString *imagePath = [ContentFolder stringByAppendingPathComponent:[NSString stringWithFormat:#" %d.plist",Counter]];
NSLog(#" iamge path %#",imagePath);
NSMutableDictionary *objectDictionary = [[NSMutableDictionary alloc]initWithObjectsAndKeys:data,#"data", nil];
[objectDictionary writeToFile:imagePath atomically:YES];
you should be able to change these according to your needs..

store images in iPhone application

I am simply creating a student management system in iPhone.
There I need to store student's small images,
Which should be appear in tableView,
Ok, I know how to work with tableView...
how to work with database...
But question is
Where to store images
how can we obtain the path of stored images..
Do i have to store entire images to database...
Or
i have to store relative path of image to database...
What is suggested by You masters?
Thanks in advance for helping me.
You can create image files on the file system of the device in the Document directory. Use something like this:
// Generate a unique user filename
NSString *imageFilename = [NSString stringWithFormat:#"student_image_%#", uniqueIdentifier];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [NSString stringWithFormat:#"%#/%#", [paths objectAtIndex:0], imageFilename];
NSData *storage = [image UIImagePNGRepresentation]; // or UIImageJPEGRepresentation
[storage writeToFile:path atomically:NO];
Best Regards,

Download and store file on iphone

i want to replace my sqlite DB stores on iphone by a sqlite which is on my server for an update.
how could i do that? i can download it and get it with an NSData object but how do i store it on the iphone and then replace the old one ?
thanks
That's pretty simple. It should just be something like this:
NSData *fetchedData = ...; /* downloaded using NSURLConnection or whatever*/
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"DBName.sqlite"];
[fetchedData writeToFile:filePath atomically:YES];