store images in iPhone application - iphone

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,

Related

what is the best way to store bulk of images and MP3 song iOS Sdk

i am doing one iphone app, for that i have to store bulk of MP3 song and images.
can ay one tell me what is the best to store those in terms of performance.
Store the image and songs in the application directory. This is best and easy way to handle. Try the following code. it will be help you.
//Store Image/Songs files to Application Directory
+(BOOL)writeToFile:(NSData *)data fileName:(NSString *)fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// the path to write file
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
return [data writeToFile:appFile atomically:YES];
}
//Image/songs - Retrieve from Application Directory
+(NSData *)readFromFile:(NSString *)fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
NSData *myData = [NSData dataWithContentsOfFile:filePath];
if (myData) {
return myData;
}
return nil;
}
store the MP3 song and images into resources folder of your app project and give the refrences in the sqlite database(because saving large files in sqlite database is not a good practice)
I would suggest you to store all items of large size on the device disk, i.e, Documents directory and store their physical path in core data or sqlite or at least in a plist file so that you can retrieve them as per your convenience.

iPhone: Storing audio data on NSUserDefaults and memory issues

I am downloading several small size songs from server and storing(NSData) into NSUserDefaults, so that i can use it when needed to cache and play directly on the device, instead of downloading and playing again from server.
The problem, if i store few couple of smaller size songs as an data format in NSUserDefaults, it reduces lots of device memory and throwing memory warning or crashing etc.
Could you someone guide me how can i resolve it? How can i store song data persistently on the device for Cache purpose, and same time storing in less memory usage?
UPDATED: As per the suggestion, i tried to add song data into dictionary as file and tried to retrieve it as below. But, still i'm facing the same issue, memory warning after around 30mb of data retrieved.. Could someone help me to resolve this? I need to store around 40 mb of song data and store it.
NSURL *songurl = [NSURL URLWithString:downloadSOngUrl];
NSMutableData *songdata = [NSMutableData dataWithContentsOfURL:songurl];
NSString *fileName = [downloadSOngUrl lastPathComponent];
NSLog(#"fileName: %#",fileName);
[appDelegate writeSongIntoDocsDirectory :songdata :fileName];
-(void) writeSongIntoDocsDirectory :(NSData *) inSongData :(NSString *) songNamePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(#"songNamePath: %#", songNamePath);
songNamePath = [songNamePath stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet] ];
NSLog(#"songNamePath: %#", songNamePath);
if ( [inSongData writeToFile:[documentsDirectory stringByAppendingPathComponent:songNamePath] atomically:YES] )
{
// Success !
NSLog(#"Successfully saved the song into documents directory");
}
else
{
// Error !
NSLog(#"Error when Successfully saving song into documents directory");
}
}
-(NSData *) readSongDataFromDocsDirectory :(NSString *) filePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSData *readData = [NSData dataWithContentsOfFile:[documentsDirectory stringByAppendingPathComponent:filePath]];
return readData;
}
Thanks in advance!
Better store these audio files in documents directory as this will reduce the size of the app , NSUserDefaults is usually used to stored in small settings particular to the app , like the preferences and all for the particular user of the app. hoping this helps. . :)
Whatever you store in the NSUserDefaults will increase the app size as this object is always alive while the app runs...

How to retrieve photo library path?

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:

Does the iPhone compress images saved within my app's documents directory?

We are caching images downloaded from our server. We get the data from an ASIHTTPRequest callback like this:
#pragma mark ASIHTTPRequest callback
-(void)imageDownloadFinished:(ASIHTTPRequest*)aRequest
{
NSString* fileName = aRequest.url.path.lastPathComponent;
[self imageDidArrive:[aRequest responseData] forFileName:fileName];
}
We write the image data to our local storage like this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0] ;
NSString* folder = [[documentsDirectory stringByAppendingPathComponent:#"flook.images"] retain];
NSString* fileName = [folder stringByAppendingFormat:#"/%#", aBaseFilename];
BOOL writeSuccess = [anImageData writeToFile:fileName atomically:NO];
The downloaded images are always the expected size, around 45-85KB.
Later, we read images from our cache like this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0] ;
NSString* folder = [[documentsDirectory stringByAppendingPathComponent:#"flook.images"] retain];
NSString* fileName = [folder stringByAppendingFormat:#"/%#", aBaseFilename];
image = [UIImage imageWithContentsOfFile:fileName];
Occasionally, the images returned from this cache read are much smaller because they are much more compressed - around 5-10KB. Has the OS done this to us?
Edit - it turns out that we are downloading the small images, so the issue isn't on the iPhone
If I'm reading your code correctly, you're using the NSData method writeToFile:atomically: to write to the file. That does an exact byte-for-byte write of the contents of the NSData object.
It appears that the NSData object is created directly from the contents of the HTTP response, so the answer is "no", there should not be any compression taking place.
We have the solution. When the phone is running on the 3G network, O2 kindly steps in and applies extra JPG compression to our images, so that they look extra horrible.
See this post on the UK 3G forum.

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