UIImageView changing size unexpectedly - ios

I have UITableView with cells that have images on it. Im using storyboard, and connect UIImageView as outlet and name it imageView. Images what I get come with different size (i download it through URL). But when table shows up sometimes image shown in incorrect size (it suppose to be square with 66x66 width and height, but in many cases square is bigger, or it actually have more width then height).
Obviously i want that borders of my UIImageView be stable. There is my code for cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UILabel *labelText = (UILabel *)[cell viewWithTag:1000];
labelText.text = [[self.listOfPlaceDetails objectAtIndex:indexPath.row] objectForKey:#"name"];
if (cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[cell.imageView setImageWithURL:[NSURL URLWithString:[[self.listOfPlaceDetails objectAtIndex:indexPath.row]objectForKey:#"imageFirst"]] placeholderImage:[UIImage imageNamed:#"dashie.png" ]completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType){
UIImage *resizedImage = [image resizedImageWithBounds:CGSizeMake(66, 66)];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.layer.cornerRadius = cell.imageView.bounds.size.width /2.0f;
cell.imageView.clipsToBounds = YES;
cell.separatorInset = UIEdgeInsetsMake(0, 82, 0, 0);
cell.imageView.image = resizedImage ;
}];
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
return cell;
}
Please explain me whats going on and how to fix that, any advice would be appreciated, thanks!

Add new imageView and set the image. I would recommend you make subclass of uitablviewCell and add the imageView as outlet view
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UILabel *labelText = (UILabel *)[cell viewWithTag:1000];
labelText.text = [[self.listOfPlaceDetails objectAtIndex:indexPath.row] objectForKey:#"name"];
if (cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:yourFrame];
imgView.contentMode = UIViewContentModeScaleAspectFit;
[cell addSubview:imgView];
imgView.tag = 7;
}
UIImageView *img = (UIImageView*)[cell viewWithTag:7];
UIImage *resizedImage = [image resizedImageWithBounds:CGSizeMake(66, 66)];
img.imqge = resizedImage

You are using standard UITableViewCell class and try to change its imageView property. Not your cell, that you created on Storyboard.
You should look through this tutorial Storyboards Tutorial in iOS 7: Part 1.
Your are interested in part:
Designing Your Own Prototype Cells
Using a Subclass for the Cell
I'll recommend use subclass of UITableViewCell issue.

Related

UITableviewCell Image size changing on Click of cell?

I am trying to display name and images in UITableViewCell like the normal way
cell.textLabel.text=#"some name";
cell.imageView.image=[array objectAtIndex:indexPath.row];
and when i load the ViewController , I can see all images are coming correctly but when I tried to click on any of UITableviewCell image size is changing to big(please check the example screenshot).
Please help me to solve this problem.
Loading time it looks like
When I click on the first cell ,the cell image becomes big like below image
Please check my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
cell.backgroundColor=[UIColor clearColor];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
cell.backgroundColor=[UIColor clearColor];
if([userName count]>0){
cell.textLabel.text=[userName objectAtIndex:indexPath.row];
[cell.imageView setImageWithURL:[NSURL URLWithString:[[userList valueForKey:#"photo"] objectAtIndex:indexPath.row]]
placeholderImage:[UIImage imageNamed:#"avatar.png"]];
}
CGSize itemSize = CGSizeMake(40, 40);
UIGraphicsBeginImageContextWithOptions(itemSize, NO, UIScreen.mainScreen.scale);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[cell.imageView.image drawInRect:imageRect];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.imageView.layer.masksToBounds=YES;
cell.imageView.layer.cornerRadius=9.0;
return cell;
}
My Didselect method is like this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *nib;
if ([[MyDevice getMyDevice] isiPad])
{
nib = #"nibname_iPad";
}
else
{
nib = #"nibname";
}
ViewController *chat = [[ViewController alloc] initWithNibName:xibName bundle:[NSBundle mainBundle]];
chat.hidesBottomBarWhenPushed=YES;
[self.navigationController pushViewController:chat animated:YES];
}
Try to use Custom Cells (Change the Style of the cell in the attributes inspector of the cell), its better and you can design it as the way you want. Create in the Story board the UILabels and the UIImage (with a specific width and height), finally in the attributes inspector of the UILabel and UIIamge set them with a specific Tag number, this number has to be unic for both of them. And access them like this:
In the method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Insert this:
UITableViewCell *customCell = [self.tableView dequeueReusableCellWithIdentifier:#"theNameOfTheCell"];
UILabel *label;
label = (UILabel *)[customCell viewWithTag:1];
label.text = #"Some text to label with tag number 1";
label = (UILabel *)[customCell viewWithTag:2];
label.text = #"Some text to label with tag number 2";
UIImageView *imageCell;
imageCell = (UIImageView *)[customCell viewWithTag:3];
CALayer * l = [imageCell layer];
UIImage *cellImage = [UIImage imageNamed:#"theImageYouWant.png"];
imageCell.image = cellImage;
Try with this and tell what append, this is the full Documentation: Official Documentation Table View Cells
Add content mode:
[cell.imageView setContentMode:UIViewContentModeScaleAspectFit];
Hope this helps... :)

Cell.selectedBackgroundview not showing

cell.selectedbackgroundview isn't working. I have figured out that the selectedview is placed behind my image in the cell resulting in no visible image (I worked this out by making the image small then a saw the tick behind the image). Is there a way to fix this? My code seems fine it must have something to do with the layers
static NSString *identifier = #"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *productImages = (UIImageView *)[cell viewWithTag:100];
productImages.image = [UIImage imageNamed:[AllProducts_Sections[indexPath.section]objectAtIndex:indexPath.row]];
collectionView.backgroundColor = [UIColor greenColor];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tick 1.png"]];
collectionView.allowsMultipleSelection = YES;
return cell
-(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];
//some code
}
UIView *selectedBackView=[[UIView alloc]initWithFrame:cell.bounds];
UIImageView *selectionImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tick 1.png"]];
[selectedBackView addSubview:selectionImg];
cell.selectedBackgroundView=selectedBackView;
return cell;
}
Also make sure that tick 1.png is in your project's bundle.

Much memory usage in UITableView

I have an UITableViewController and I customize its UITableViewCells. Each UITableViewCell contains two UIImageViews and two UILabels. Although when I get into the UITableView the memory usage raises to 100 MB and i do not know the reason. Here is the code for the cellForRowAtIndexPath method that handles the cell customization. Can anyone help me with this issue?
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = #"Cell";
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:simpleTableIdentifier];
}
UILabel *mnhmeioLabel = (UILabel *)[cell viewWithTag:102];
mnhmeioLabel.frame = CGRectMake(mnhmeioLabel.frame.origin.x,
mnhmeioLabel.frame.origin.y,
191,
21);
Group *tmpGroup = [mnhmeiaTotal objectAtIndex:indexPath.row];
NSLog(#"%# ",tmpGroup.title);
NSLog(#"%f %f", mnhmeioLabel.bounds.size.width,
mnhmeioLabel.bounds.size.height);
mnhmeioLabel.text = tmpGroup.title;
mnhmeioLabel.font = [UIFont fontWithName:#"Open Sans" size:10];
mnhmeioLabel.numberOfLines=0;
mnhmeioLabel.lineBreakMode=NSLineBreakByWordWrapping;
[mnhmeioLabel sizeToFit];
if( tmpGroup.imagesOfArticle.count > 0 ){
UIImageView *mnhmeioImageView = (UIImageView *)[cell viewWithTag:103];
mnhmeioImageView.image = [tmpGroup.imagesOfArticle objectAtIndex:0];
} else {
UIImageView *mnhmeioImageView = (UIImageView *)[cell viewWithTag:103];
mnhmeioImageView.image = [UIImage imageNamed:#""];
}
UIImageView *markerImageView=(UIImageView *)[cell viewWithTag:100];
markerImageView.image=[self selectMarker:tmpGroup];
UILabel *mnhmeioDescription = (UILabel *)[cell viewWithTag:101];
mnhmeioDescription.text=tmpGroup.constructedContent;
mnhmeioDescription.font = [UIFont fontWithName:#"Open Sans" size:6];
}
Probably, the images are big.
Or, perhaps you are not reusing the cells properly, if you are using iOS7 and Storyboards, please follow this tutorial (Prototype cells and Using subclass for a cell sections):
http://www.raywenderlich.com/50308/storyboards-tutorial-in-ios-7-part-1
Thank you all for your help. I had to decrease the size of each image as much as i could. This fixed my problem.

Click on a cell, the image size changed

I am setting the imageview size to be fixed. I have set the constraint in the storybard, and the constraint is correct. But after I loading some images from the web service, when I click on the cell, some of the images changed it's size, some of them don't. I have set the image view contentMode to aspect fit. How to make the image auto resize to fit in the imageview, and I don't have to click on the cell, to make it resize to fit?
The cellForRow:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
LocalCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[LocalCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString *photoReference = [self.restaurants[indexPath.row][#"photos"] objectAtIndex:0][#"photo_reference"];
NSString *photo = [NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/photo?maxwidth=80&photoreference=%#&sensor=true&key=%#", photoReference, AppKey];
cell.name.text = self.restaurants[indexPath.row][#"name"];
cell.desc.text = self.restaurants[indexPath.row][#"vicinity"];
[cell.imageView setImageWithURL:[NSURL URLWithString:photo] placeholderImage:[UIImage imageNamed:#"placeholder"]];
return cell;
}
Set your UIImageView clipsToBounds propery to YES as below
[yourImageView clipsToBounds:YES];
I just figured out:
- (void)layoutSubviews
{
[super layoutSubviews];
self.imageView.frame = CGRectMake(15, 15, 80, 50);
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
}

iOS - when to remove subviews from UITableViewCell

I have a UITableView which a UIImage is added as a subview to each cell of it. The images are PNG transparent. The problem is when I scroll through the UITableView, the images get overlapped and then I receive the memory warning and stuff.
here's the current code for configuring a cell:
- (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];
}
int cellNumber = indexPath.row + 1;
NSString *cellImage1 = [NSString stringWithFormat:#"c%i.png", cellNumber];
UIImage *theImage = [UIImage imageNamed:cellImage1];
UIImageView *cellImage = [[UIImageView alloc] initWithImage:theImage];
[cell.contentView addSubview:cellImage];
return cell;
}
I know the code for removing the UIImage subview would be like:
[cell.imageView removeFromSuperview];
But I don't know where to put it. I've placed it between all the lines; even added an else, in the if statement. didn't seem to work!
UITableViewCell already have a image view attached. Remove:
UIImageView *cellImage = [[UIImageView alloc] initWithImage:theImage];
[cell.contentView addSubview:cellImage];
Add:
[cell.imageview setImage:theImage];
and you're good to go!
Result code will be like:
- (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];
}
UIImage *cellImage = [UIImage imageNamed:[NSString stringWithFormat:#"c%i.png", indexPath.row + 1]];
[cell.imageView setImage:cellImage];
return cell;
}
You just kepp this:
[cell.imageview setImage:theImage];
and remove:
UIImageView *cellImage = [[UIImageView alloc] initWithImage:theImage];
[cell.contentView addSubview:cellImage];

Resources