views:

792

answers:

2

I have following code in my application:

// to set tip - photo in photo frame    
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:pathOfThumbNail]];
UIImage *cellThumbImg;

if([data length]>0){
    cellThumbImg = [UIImage imageWithData:data];
} else {
    cellThumbImg = [UIImage imageNamed:@"130X90.gif"];
}

UIImageView *imgView = [[UIImageView alloc]initWithImage:cellThumbImg];

imgView.frame = photoFrame;

[imgBg setContentMode:UIViewContentModeScaleToFill];
[cell.contentView addSubview:imgView];
//[cell.contentView sendSubviewToBack:imgView];

[imgView release];
[data release];

I want the following:

  • Suppose an image ( which is loaded through nsdata ) is having size of 60 x 60 then content mode should be UIVIewContentModeCenter
  • Suppose an image ( which is loaded through nsdata ) is having more size then 60 x 60 then content mode should be UIViewContentModeScaleToFill.

But my question is how can I determine the size of the image loaded through NSData?

+1  A: 

You can remove the following line because the UIImageView will size itself according to the image used to initialize it:

imgView.frame=photoFrame;

From there you can get the size of the image by taking the size of the UIImageView:

CGRect image_bounds = imgView.frame;
fbrereto
@fbrereton - Sir, You are right. However, I tried something different which is added in following answer.
sugar
A: 

I tried following code & it worked.

// to set tip - photo in photo frame    
NSData *data=[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:pathOfThumbNail]];
UIImage *cellThumbImg;
if([data length]>0)
{ 
     cellThumbImg=[UIImage imageWithData:data];
} 
else 
{ 
     cellThumbImg=[UIImage imageNamed:@"130X90.gif"]; 
}
UIImageView *imgView=[[UIImageView alloc]initWithImage:cellThumbImg];
imgView.frame=photoFrame;

(cellThumbImg.size.height>60 || cellThumbImg.size.width>60 ) ? 
      [imgView setContentMode:UIViewContentModeScaleToFill] : 
      [imgView setContentMode:UIViewContentModeCenter] ;

[cell.contentView addSubview:imgView]; 
[imgView release]; [data release];
sugar