Setting the size for an image in a TableViewCell - ios

I have a table with rows of height 90.
I want to have an image on each row of size 50x50.
I am doing the following in cellForRowAtIndexPath:
cell.imageView.frame = CGRectMake(0, 0, 50, 50);
cell.imageView.clipsToBounds = YES;
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
if (item.image){
cell.imageView.image = [item.image smallImage];
cell.imageView.alpha = 1.0f;
} else {
cell.imageView.image = [UIImage imageNamed:#"icon-greyed.png"];
cell.imageView.alpha = 0.25f;
}
However the images appear with height 90 (the height of the row). What am I not doing right?

You will need to either add a UIImageView to cell.contentView which I wouldn't recommend, or create a custom UITableViewCell, which I would recommend. Then you can add a UIImageView to your custom cell and resize it to the dimensions you require.

Related

how to set frame for cell.imageView.frame?

i tried this below code in cell for row...
if([cell.textLabel.text isEqualToString:#"Home"])
{
cell.imageView.image = [UIImage imageNamed:#"Home-50.png"];
cell.imageView.frame=CGRectMake( 10, 15, 20, 20 );
}
This is below code is in didselect row at Indexpath...
if ([[arrCell objectAtIndex:indexPath.row] isEqualToString:#"Home"]) {
cell.imageView.image = [UIImage imageNamed:#"Home1-50.png"];
cell.imageView.frame=CGRectMake( 10, 15, 20, 20 );
}
I am using this above code to display image in tableview cell and when cell is clicked Then Image change to another image...Images are Changing But,the frames are not applying...
No you can't do this because it is fixed layout of UITableViewCell.If you want to do this go with below option
OPTION 1: CustomCell
Through CustomCell you can set image view frame whatever you want.
OPTION 2: Programmatically creating image view add it to cell
UIImageView *img = [[UIImageView alloc] init];
img.image = [UIImage imageNamed:#"yourImageName.png"];
[img setFrame:CGRectMake( 10, 15, 20, 20)];
[[cell.contentView addSubview:img];];
Can be achieved by transform scalingļ¼š
cell.imageView?.image = UIImage(named: "icon_shareToFriends")
if let imageView = cell.imageView, imageView.transform == .identity {
imageView.transform = imageView.transform.scaledBy(x: 0.55, y: 0.55)
imageView.contentMode = .center
}

Set image on tableView Cell size

I have tableView Cell size and need to set image on tableView cell., but image not set of define the Cell size (following code into tableView Cell size stored cellSize variable).
My Code is...,
CGRect cellSize = [tableView rectForRowAtIndexPath:indexPath];
cell.imageView.frame = cellSize;
cell.imageView.image = [UIImage imageNamed:#"logo_black.png"];
above code in image not set image properly in tableView Cell.
So, How to set image on TableView Cell?
If you are trying to say that the image u are setting in each cell is not getting displayed at the right position then u can try doing the following:
first create a prototype cell and position the UIImageView at the right place in the cell u want and go to the scale properties of UIImage and note down the co-ordinates... then
instead of
CGRect cellSize = [tableView rectForRowAtIndexPath:indexPath];
cell.imageView.frame = cellSize;
this you can write the code as follows:
cell.imageView.frame = CGRectMake(x,y,width,height);
ex.
CGRectMake(0,0,20,20)
CGRect cellSize = cell.frame;
cell.imageView.frame = cellSize;
Try this code:
UIView *bgView = [[UIView allow ] init];
bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageName:#"imgName"]];
cell.selectedBackgroundView = bgView;

Loading Remote images in TableView while keeping sizes intact

I want to show a table view where each cell will just contain one image. Images are loaded from respective URLs and there is no restriction on the size of the image.
I want to show the image in each cell with their proper sizes - that is maintain the aspect ratio. I am guessing this would entail having dynamic heights for each cell. But I am not sure and can't find a resource which explains how to achieve this.
What would be the best way to do this?
You need to get the image height and width dynamically first and set the same hieght and width to your table view cells.
I have not added table view but just some code will help you see how you can show two different images with their original height and width and adding the same to a view.
NSURL *imageURL1 = [NSURL URLWithString:#"http://cdn.macrumors.com/article-new/2014/12/applelogo.png"];
NSData *imageData1 = [NSData dataWithContentsOfURL:imageURL1];
UIImage *image1 = [UIImage imageWithData:imageData1];
UIImageView *imageView1 = [[UIImageView alloc] initWithImage: image1];
//CGFloat height = imageView.frame.size.height;
//CGFloat width = imageView.frame.size.width;
[self.view addSubview:imageView1];
//sample urls :- http://pre01.deviantart.net/e711/th/pre/f/2013/179/2/c/ios_7_icons__updated__by_iynque-d69mme1.png
NSURL *imageURL2 = [NSURL URLWithString:#"http://icons.iconarchive.com/icons/position-relative/social-2/128/ios-icon.png"];
NSData *imageData2 = [NSData dataWithContentsOfURL:imageURL2];
UIImage *image2 = [UIImage imageWithData:imageData2];
UIImageView *imageView2 = [[UIImageView alloc] initWithImage: image2];
CGFloat height = imageView2.frame.size.height;
CGFloat width = imageView2.frame.size.width;
imageView2.frame = CGRectMake(0, 300, width, height);
NSLog(#"height :- %f, Width :- %f ",height, width);
[self.view addSubview:imageView2];
Hopefully, you are loading the images asynchronously, as answered here: Loading images for UITableViewCell from URL (need to load them asynchronously)
You'll need a custom cell or prototype cell with a UIImageView set to UIViewContentModeScaleAspectFit. In the cell, set the constraints how you want them: that is, if you want the image to be the full width of the screen, set leading and trailing constraints, and constrain to the top and bottom.
Then, set your table view to use dynamic sizing:
tableView.estimatedRowHeight = 80.0; //or some reasonable first approximation
tableView.rowHeight = UITableViewAutomaticDimension;

unable to set the image view to be circle

i need to make the profile photo image circle (similar to whats app)
The cell i use is UITableViewCellStyleDefault
here's my code:
User *user = [DBHelper fetchUserWithUserID:userID];
cell.imageView.image = [Helper imageWithBlob:user.profile.thumbnailPhotoBlob defaultImageFile:FILE_DEFAULT_PHOTO];
cell.imageView.layer.cornerRadius = cell.imageView.frame.size.height /2;
cell.imageView.clipsToBounds = YES;
cell.imageView.layer.masksToBounds = YES;
But the result image view is still a square
EDIT: The image view's frame is {0, 0, 0, 0}, but it did show the image.
EDIT 2:
I tried the answer but the image is not circle. Here's what i did:
float height = [self tableView:tableView heightForRowAtIndexPath:indexPath];
[Helper circlizeImageView:cell.imageView cellHeihgt:height];
+ (void)circlizeImageView:(UIImageView *)imageView cellHeihgt:(float)cellHeight {
imageView.frame = CGRectMake(0, 0, cellHeight, cellHeight);
[Helper circlizeView:imageView];
}
+ (void)circlizeView:(UIView *)view {
view.layer.cornerRadius = view.frame.size.height / 2;
view.clipsToBounds = YES;
view.layer.masksToBounds = YES;
}
The code is all fine.
The only problem is with this line:
cell.imageView.layer.cornerRadius = cell.imageView.frame.size.height/2;
//CGRect rectTest = cell.imageView.frame;
//returns a rect of (0,0,0,0) & hence the cornerRadius being set is 0.
Try:
cell.imageView.layer.cornerRadius = 10; //or some fixed value
In retrospect, what you're trying to do will not get you to make the imageView circular because the default imageView is rectangular (the height is always more than the width) and so... setting a corner radius that is half of the height/width will not work.
It'll be better if you use a custom UITableViewCell and add a square framed imageView.
ANyways... Since the default imageView touches the top of the default UITableViewCell and ends at the bottom, you can get the height of the imageView by using the height of the row (in case you have dynamically sized cells)
So, something as simple as this:
cell.imageView.layer.cornerRadius = cell.frame.size.height/2;
But none of this looks good since the imageView is rectangularish and cells being of dynamic height will make it look like a poor design (the fixed radius looks best imho)
You have two options here:
1- Add the imageView as a subView to you cell.
2- Override layoutSubviews in a custom cell:
- (void)layoutSubviews {
[super layoutSubviews];
self.imageView.frame = CGRectMake(0,0,40,40);
}

iOS - Cell image blurred

I have placed an image in a UITableViewCell and for some reason, the image is a bit blurred...
This is what I'm using:
NSURL *urlForProfileImage = [NSURL URLWithString: [_currentTweet[#"user"] objectForKey:#"profile_image_url_https"]];
UIImage *thumbnail = [UIImage imageWithData: [NSData dataWithContentsOfURL:urlForProfileImage]];
cell.imageView.image = thumbnail;
Is there another way to provide the desired result but maintain the images quality?
Reason :
The default imageView size is 40x40 and so your image needs to be 80x80 pixels (retina display).
But the image that you are getting from the "pbs.twimg.com/profile_images/192098593/Apple_normal.png" is 48x48.
And so it is blurred.
Solution :
One option is that you add a custom imageView which is 24x24. (width : 24 & height : 24) Then your image will not show blurred.
Or, you can try modifying the height and width of the imageView by subclassing the class UITableViewCell and using its layoutSubviews method. The "trick" is to write layout code in this method, otherwise the code does not have any effect :
- (void)layoutSubviews {
[super layoutSubviews];
self.imageView.bounds = CGRectMake(0,0,24,24);
self.imageView.frame = CGRectMake(0,0,24,24);
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
CGRect tmpFrame = self.textLabel.frame;
tmpFrame.origin.x = 30;
self.textLabel.frame = tmpFrame;
tmpFrame = self.detailTextLabel.frame;
tmpFrame.origin.x = 30;
self.detailTextLabel.frame = tmpFrame;
}

Resources