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.
Related
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];
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/
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
I am making an app that takes photos from web site for some Username and shows it in a UITable with username then when clicking user name it shows photos for this user and then clicking to name of photo it shows full screen photo.
My question is I am using NSData to get photos from internet. Do I have to save the data to CoreData? When pressing name of user it creates NSData and downloads photos from internet and shows them on UITable. And it takes time.
What is good approach? and How can save this images to CoreData?
I am using this method
NSData *imageData=[flickr dataForPhotoID:firstPhoto.id fromFarm:firstPhoto.farm
onServer:firstPhoto.server withSecret:firstPhoto.secret inFormat:
FlickrFetcherPhotoFormatSquare];
and here definition of dataForPhotoID method
- (NSData *)dataForPhotoID:(NSString *)photoID fromFarm:(NSString *)farm
onServer:(NSString *)server withSecret:(NSString *)secret
inFormat:(FlickrFetcherPhotoFormat)format {
#if TEST_HIGH_NETWORK_LATENCY
sleep(1);
#endif
NSString *formatString;
switch (format) {
case FlickrFetcherPhotoFormatSquare: formatString = #"s"; break;
case FlickrFetcherPhotoFormatLarge: formatString = #"b"; break;
}
NSString *photoURLString = [NSString stringWithFormat:#"http://farm%#.static.flickr.com/%#/%#_%#_%#.jpg", farm, server, photoID, secret, formatString];
NSURL *url = [NSURL URLWithString:photoURLString];
return [NSData dataWithContentsOfURL:url];
}
First, always store your images in a usable format such as PNG or JPEG instead of NSData. This will save you a lot of headaches.
Second, the rule for storing binary data is:
< 100kb store in the same table as the relevant data
< 1mb store in a separate table attached via a relationship to avoid loading unnecessarily
1mb store on disk and reference it inside of Core Data
Update
The storage inside of Core Data should be binary and you can write accessor methods for it. Take a look at this answer: Core data images from desktop to iphone
Update
The example code I linked to describes how to create accessors in your NSManagedObject subclass that will convert the image back and forth between a UIImage and binary data.
You can simply store UIImage objects in CoreData directly, just use Transformable data type, and you are ready to go
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);