开发者

Images get rotated when retrieved from sqliite3 db

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 d开发者_JS百科ictionaryWithObjects: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)];  
    } 
  }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜