Reputation: 5256
I have a table view cell, [prototype], in which I added a imageview. Now I set the height and width of this imageview manually in the size inspector. However I'm using
[cell viewWithTag:12345]
to get the view pointer in my objective c code, and updating the image of the image view. However I find that my manual constraints are overridden, and the image takes its own size. I have tried various modes (Aspect fill, Aspect Fit, etc.) both in the attributes inspectors and programmatically.
So my question is how can I set the constraints? Where am I wrong? Why are they not working?
Upvotes: 0
Views: 1487
Reputation: 2862
Following code is helpfull
Case 1:
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100.0;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
image.tag = 101;
[cell addSubview:image];
}
UIImageView *imageObj =(UIImageView *)[cell viewWithTag:101];
[imageObj setImage:[UIImage imageNamed:@"url.jpeg"]];
// Set up the cell...
return cell;
}
OUT put:
And Case2:
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 200.0;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
image.tag = 101;
[cell addSubview:image];
}
UIImageView *imageObj =(UIImageView *)[cell viewWithTag:101];
[imageObj setImage:[UIImage imageNamed:@"url.jpeg"]];
// Set up the cell...
return cell;
}
Out put:
Actual image size is 460 x 246 but you can find difference according to frame
Thank you
Upvotes: 0
Reputation: 6065
First of all I would check if the problem lies on contentMode of image view or size of image view. It is also possible that your imageview is resized because of autoresizing mask. If you initialize your image view with
- (id)initWithImage:(UIImage *)image
method your imageview will be automatically resized to the size of image.
Upvotes: 0
Reputation: 47059
Use following method for specific hight and width with image
+ (UIImage*)resizeImage:(UIImage*)image withWidth:(int)width withHeight:(int)height
{
CGSize newSize = CGSizeMake(width, height);
float widthRatio = newSize.width/image.size.width;
float heightRatio = newSize.height/image.size.height;
if(widthRatio > heightRatio)
{
newSize=CGSizeMake(image.size.width*heightRatio,image.size.height*heightRatio);
}
else
{
newSize=CGSizeMake(image.size.width*widthRatio,image.size.height*widthRatio);
}
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
This method return NewImage, with specific size that you want.
Upvotes: 3