Store Images in SQLite database and retrieve in ios5 - iphone

I am using SQLite 3.0 and iOS5.
Now I want to store images in vrchar2 datatype in database and retrieve it in my iPhone's view.
I have used images stored in BLOB data type and through NSDATA I have retrieved it..Nut now I want to retrieve it in ImageView ..
How can I use it?

You should first create a UIImage from NSData using:
UIImage *img = [[UIImage alloc] initWithData:theData];
Then you can use this image in your image view:
UIImageView *imgView = [[UIImageView alloc] initWithImage: img];
Don't forget to do the proper memory management though!

Is there any specific reason of using the whole image in to database?
The reason behind asking this is simply because I think storing NSData in database is storing a lot of data in the database, which on a longer run can make your database really heavy.
Better option is to save the image in Documents Directory or may be Library directory for that matter and then simply saving the full image path of the image in your database.
Then simply which retrieving it in an imageView you need to call
UIImageView *imgView = [UIImageView setImage:[[UIImage alloc] initWithContentsOfFile:filePath]];
Here all you need to pass is "filePath" which you have fetched from database.
Hope this helps you.

You have to create a DAO so that you can retrieve the information form the sqlite file. Then call that file to get all the info you need.
Then retrieve the blob like this:
const char * myData = sqlite3_column_blob(statement, 1(this number is different according to your database));
int myDataSize = sqlite3_column_bytes(statement, 1);
NSData *data = [NSData dataWithBytes:myData length:myDataSize];
Then initialize
myPhoto.thePhoto = [[UIImage alloc] initWithData:data];

Related

Extracting array of images from url

After doing some research I am a little confused. I can grab a single image from a url if I know the exact directory for example,
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://www.myUrl.com/pic/workaholics.png"]]];
UIImageView *view1 = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:view1];
This of coarse works fine, but what if you didn't know the exact path. In Linux you could always do some sort of recursive search but with iOS I can't figure it out. For example, in the "pic" directory that I specified in the latter case what if there was other directories called pic1,pic2,pic3 etc and each directory had an arbitrary number of png's. What I am having a hard time wrapping my head around is how can I recursively search the "pic" directory to extract all directories contents that match .png. I would of coarse then store this is NSData and convert to a string and then store the contents in an array. I'm very much confused at this point. Any help would be greatly appreciated.
Your difficulty here will be getting a directory listing from a web server - since you are retrieving the images over the web. Many web servers will not provide a directory listing as a security measure against hacking attempts.
if you control the web server, you can reconfigure it to provide a directory listing - then make an NSData object from the directory level URL (http://www.myUrl.com/pic/) - and parse this to pull out all the files within the directory. You can then request each individual picture and add it to an array yourself.
if you don't control the web server, but can write to the directory, you could achieve a similar implementation by creating a manifest file (XML-plist would be a good format) which lists the filenames contained in the directory. You'd need to update the manifest any time you added a new picture to the directory though.
otherwise - you have to do some pretty dirty/hacky things, like assuming there are no more than n pictures, all pictures have the filename "pic_m.png" and then iterating through a loop from 0 to n attempting to retrieve each file until you hit a failure.
UIImage needs an explicit thing (URL or filepath) to get its data from.
One potential solution is to implement searching for your .png files via NSFileManager's enumeratorAtPath: method. The reference guide I've linked to even has a code fragment you can use to to create UIImages (just switch the #"doc" to #png and instead of doing scanDocument, do your UIImage * image = ... thing).
I'll leave it as an exercise to figure out how to do it recursively, but I hope my answer helps you out!
NSURL *url = [NSURL
URLWithString:#”www.ArticleDean.com\images\sample.jpg”];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] intiWithData:data];
try this one

How to retrieve image from sqlite database in iPhone App

In my iPhone App I want to retrieve image from (stored in ) sqlite database table in "BLOB" datafield.
For that I am storing my image in NSData using code
NSData *imageData = UIImagePNGRepresentation(image);
and I am also able to retrieve image from NSData -> UIImage by code
self.img1=[UIImage imageWithData:imageData];
And displaying Image into imageView by
imageView2.image=img1;
I am storing image (in NSData form) into sqlite database table column (of BLOB Datatype) successfully.
I am using below code to retrieve image from database
NSData *imageData2 = [[arraySelect objectAtIndex:0] valueForKey:#"image_column"];
NSLog(#"imagedata2 in retrive :: %#",imageData2);
in NSLOG i am getting the same data stored in database column (in blob datatype "NSData form" )
Now I am retrieving image by
(while debugging it crashes here and showing meaasge EXE_BAD_ACCESS)
self.img3=[UIImage imageWithData:imageData2];
imageview3.image=img3;
But I am not getting that image in imageView.
I checked all outlets but they are ok.
So please Help and Suggest what I should do to get image in imageView from database.
Thanks.
#Prerak i think this will help you something..and solve your problem
http://dblog.com.au/iphone-development-tutorials/iphone-sdk-tutorial-reading-data-from-a-sqlite-database/

How to Convert data obtained from Sqlite database into NSArray to NSData in iPhone SDK

In My iPhone App,
I am converting my image in to NSData format and storing it into sqlite database Table in dataType="BLOB",
But, I am retriving that image (stored in NSData format) into NSArray Format from database,
The Problem is I am not able to convert NSArray to NSData ( Which would Help me to get my image back into NSData format )
Please Help and Suggest,
Thanks
Your requirement is so odd.
you can store image in local file system with the file name in sqlite
if you really want images store in sqlite, you can every image in sqlite separately rather than archive images in NSArray than convert to NSData
Use NSKeyedArchiver How to convert NSArray to NSData?
UIImage *imgData = [UIImage imageWithData:[yourArray objectAtIndex:theIndex]];
for multiple images in one array you can loop it
like
for (int i = 0;i<[yourArray count];i++) {
UIImage *img = [UIImage imageWithData:[yourArray objectAtIndex:i];
//do something here with each image
// for example, add it to an ImageView
}
P.S. if theres only one image, its index is 0
hope this helps

displaying an image from sqlite table

I was storing an image in a sqlite table as blob data and displaying it using the following code:
self.myImage.image = [[[UIImage alloc] initWithData:recipe.image] autorelease];
I'm now using the image file name in the sqlite fields instead and storing the image on the filesystem. what would the code be to display it that way? I'm having a hard time figuring it out.
normally to display an image from the filesystem I'd use:
UIImage *image = [UIImage imageNamed:#"something.png"];
in this case I have to grab the string that's in the table field/attribute.
thanks in advance.
You can load UIImage with absolute filepath by following API.
+ (UIImage *)imageWithContentsOfFile:(NSString *)path;
+(UIImage *)imageNamed:(NSString *)name will look for an image from application's main bundle. In other word, it will look for an image from Resources.
Editted:
You said that you stored the image on the filesystem. So, I think you can retrive the absolute filepath of image from sqlite database, doesn't it?
NSString *imgPath;
/// get imgPath from sqlite database
...
/// get the image by filepath
self.myImage.image = [UIImage imageWithContentOfFile:imgPath];
And, my question is how do you store the image on the filesystem.

Save Image to sandbox from UIImage in iPhone

I am showing very big size images in UITableView, because of this my app is getting crashed after some time. Now i want to resize the image and save it to disk. can some one help me resizing the image from NSData/UIImage and saving saving it to the disk.
I got the code to resize the image from UIImage, so as the result i have my resized in image in UIImage object, how to save it to iphone sandbox.
Thanks,
Here is code to save to the Documents directory on iOS that is from working code.
// Convert UIImage to JPEG
NSData *imgData = UIImageJPEGRepresentation(image, 1); // 1 is compression quality
// Identify the home directory and file name
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/Test.jpg"];
// Write the file. Choose YES atomically to enforce an all or none write. Use the NO flag if partially written files are okay which can occur in cases of corruption
[imgData writeToFile:jpgPath atomically:YES];
You're asking 'how do I write a file' right?
I guess there is a touch of complication in that you probably want to write into the Library/Caches directory, so when the phone gets backed up you don't save those files.
Get the root of the app folder w/ NSHomeDirectory() and then append Library/Caches.
You can write an NSData to the fs w/ a method on NSData. If you have a UIImage, you can do UIImageJPEGRepresentation() or UIImagePNGRepresentation to get data.
NSData *imgData = UIImageJPEGRepresentation(image, 1);
CGImageSourceRef source = CGImageSourceCreateWithData((CFMutableDataRef)imgData, NULL);
NSDictionary *metadata = [(NSDictionary *) CGImageSourceCopyPropertiesAtIndex(source,0,NULL)autorelease];
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/Test.jpg"];
[imageMutableData writeToFile:jpgPath atomically:YES];
It will copy your image to documents folder in sandbox with name Test.jpg.
If you want to do it for PNG you can try UIImagePNGRepresentation(image);