ganesh manoj
ganesh manoj

Reputation: 977

image retrieving in sqlite

I am using using sqlite in my project
Previously I had saved image in data but now I am saving image in bytes like following code

NSUInteger len = [entObject.photoImageData length];  
 Byte *byteData = (Byte*)malloc(len);  
 memcpy(byteData, [entObject.photoImageData bytes], len);  
insertString = [NSString stringWithFormat:@"INSERT INTO TBL_COUNTDOWN (DAIRYID,EVENTID,DESCRIPTION,EVENTIMAGE) VALUES (\"%@\",\"%d\",\"No Data\",\"%s\")",self.dairyId,self.eventId,byteData];

For retrieving image I am using following code

NSData *dataForCachedImage = [[NSData alloc] initWithBytes:sqlite3_column_blob(statement, 3) length: sqlite3_column_bytes(statement, 3)];  
                    NSLog(@"dataForCachedImage/getAllEvents is %@",dataForCachedImage);  
                    UIImage *cachedImage = [UIImage imageWithData:dataForCachedImage];  
                    NSLog(@"cachedImage/getAllEvents is %@",cachedImage);

For dataForCachedImage in NSLog I am getting data but for cachedImage I getting null

I dont no whats wrong in my code. Please help me

Thanks in advance

Upvotes: 0

Views: 200

Answers (1)

Prince Kumar Sharma
Prince Kumar Sharma

Reputation: 12641

You can save image to document directory and store its path to sqlite database save Image code is as follows

 #define DOCUMENTS_FOLDER [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]

 // Create a new dated file
 NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0];
 NSString *caldate = [now description];

 NSString *filePath= [NSString stringWithFormat:@"%@/%@.jpg", DOCUMENTS_FOLDER,caldate];
 NSURL *url = [NSURL fileURLWithPath:filePath];

 UIImage *image = [UIImage imageNamed:@"name.jpg"];
 NSData *imageData = UIImagePNGRepresentation(image);

 [imageData writeToFile:filePath atomically:YES];

anytime you want to retrieve Image then get it then use following method to getImage

- (UIImage*)getImage
{
 NSString *imagePath = [DOCUMENTS_FOLDER stringByAppendingPathComponent:@"imagename.jpg"];
 UIImage *img = [UIImage imageWithContentsOfFile:imagePath];
 return img;
}

deleting file

  NSFileManager *fileManager=[NSFileManager defaultManager];
  [fileManager removeItemAtPath:savedFilePath error:nil];

may this will help you..

Upvotes: 1

Related Questions