Dear All,
I am making an application which saves the camera roll images as blob in sqlite3 database. When the image is retrieved, its dimensions changes(height and width get interchanged). This happens only on iphone and not on simulator. Please help.
Basically when i retrieve an image from db and run it on iphone device it changes from potrait to landscape mode
Hi, Please see the code below, The first snippet is storing image in db, the 2nd is retrieving and the 3rd is drawing.
-(int) InsertImageInImagesTable:(UIImage *)taggedImage{
NSData *imgData=UIImagePNGRepresentation(taggedImage);
//unsigned char aBuufer[[imgData length]];
//[imgData getBytes:aBuufer length:[imgData length]];
NSString *sql=[NSString stringWithFormat:@"INSERT INTO tblImages(Image)values(?)"];
sqlite3_stmt *statement;
int imageId=-1;
if(statement=[self prepare:sql])
{
sqlite3_bind_blob(statement, 1, [imgData bytes], [imgData length],nil);
//sqlite3_bind_int((statement, 2, [imgData bytes], [imgData length],nil);
sqlite3_step(statement);
imageId=sqlite3_last_insert_rowid(dbh);
}
sqlite3_finalize(statement);
return imageId;
}
-(NSDictionary *)SelectImagesFromImagesTable{
NSMutableArray *imgArr=[[NSMutableArray alloc]init];
NSMutableArray *idArr=[[NSMutableArray alloc]init];
NSString *slctSql=@"Select ImageId, Image from tblImages order by ImageId asc";
sqlite3_stmt *statement;
//NSData *imgData;
int imageId=-1;
if(statement=[self prepare:slctSql])
{
//sqlite3_
while(sqlite3_step(statement)==SQLITE_ROW)
{
imageId=sqlite3_column_int(statement, 0);
NSData *imgData=[[NSData alloc] initWithBytes:sqlite3_column_blob(statement, 1) length:sqlite3_column_bytes(statement, 1)];
UIImage *im=[UIImage imageWithData:imgData];
[imgArr addObject:[UIImage imageWithData:imgData]];
[idArr addObject:[NSString stringWithFormat:@"%d",imageId]];
[imgData release];
}
sqlite3_finalize(statement);
}
NSMutableDictionary *imgDict=[NSMutableDictionary dictionaryWithObjects:imgArr forKeys:idArr];
[imgArr release];
[idArr release];
//array=idArr;
return imgDict;
}
-(void)drawRect:(CGRect)rect
{
float newHeight;
float newWidth;
float ratio=1.0;
CGContextRef ctx = UIGraphicsGetCurrentContext();
if (myPic != NULL)
{
int hh=myPic.size.height;
int ww=myPic.size.width;
if(myPic.size.height>367)
{
ratio = myPic.size.height/367;
if (myPic.size.width/320 > ratio) {
ratio = myPic.size.width/320;
}
}
newHeight = myPic.size.height/ratio;
newWidth = myPic.size.width/ratio;
[myPic drawInRect:CGRectMake(self.center.x-(newWidth/2),self.center.y-newHeight/2),newWidth,newHeight)];
}
}