views:

32

answers:

1

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)];  
    } 
  }
A: 

It's hard to say without code but off the top of my head, there are a couple of possible sources for this problem:

(1) The problem might be with the image view and not the image itself. Something in the code might be resizing the view. Check the views resizing properties. If the image view is full screen, then the view controller might actually think it is landscape orientation and it is actually displaying the image correctly but for the wrong orientation. (Confusing orientation constants is an easy way to make that happen.)

(2) The images are misformatted when they are saved. When the UIImage regenerates them, the width and height are swapped. I've never seen that happen but an image in the wrong format could in theory cause that.

Since this only happens on the device, I would bet that the problem is (1). Ignore the images for a bit and instead look at how the view controllers handle their orientation and sizing.

TechZen
i am not using imageview.. am rather drawing an image using drawinRect
Deepika
You've most likely transposed width and height when setting up the drawing area. Can't say without seeing the code. This isn't a common or known problem. You've got something off in this particular build.
TechZen
Hi, I have pasted the code.Please help.The image gets inverted only after drawing it after retrieving from db and in iphone device.
Deepika
Sorry, I don't see a problem. Do you do anything when you assign myPic? Since it only happens on the device could it be that the view thinks its orientation has changed and that it is trying to draw portrait in landscape or vice versa? That's all I can think of.
TechZen