-2

I try to view the picture on the cell as a circle And I can not . Closest I've come this ellipse

this is my code :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.imageView.clipsToBounds = YES;
    cell.imageView.layer.cornerRadius = cell.imageView.frame.size.width / 2.0;
    cell.imageView.layer.borderWidth = 2.0;
    cell.imageView.layer.borderColor = [UIColor blueColor].CGColor;

    cell.imageView.image=[UIImage imageWithData:[picCoreData valueForKey:@"image"]];//i get the picture from core data.

    return cell;
}
mimpami
  • 83
  • 1
  • 10

3 Answers3

0

imageView correct frame is not set when you try to access it here in cellForRowAtIndexPath. Try this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

  static NSString *CellIdentifier = @"Cell";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  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.clipsToBounds = YES;
  cell.imageView.layer.cornerRadius = itemSize.width / 2.0;
  cell.imageView.layer.borderWidth = 2.0;
  cell.imageView.layer.borderColor = [UIColor blueColor].CGColor;

  cell.imageView.image=[UIImage imageWithData:[picCoreData valueForKey:@"image"]];

  UIGraphicsEndImageContext();

  return cell;
}
Irfan
  • 5,070
  • 1
  • 28
  • 32
0

I created the exact round image view by giving hard coded values. like if my image view is 30 , i give 15 and round rect is produced. Because subtitle is a pre-defined cell style type, and image view in it is optional which is created when the it is necessary So when you check the size of image view in the cellForRowAtIndexPath,it gives zero size. It creates it after the imageView is initialised. You have to give constant radius then it will make round corners.

Use this code.

iOS UITableViewCell cell.imageView setting rounded corner.

Also initialise the cell if it is nil in the upper code.

Community
  • 1
  • 1
0

I got the solution,Please try the below coding

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  return 60;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *str = @"cell";
  UITableViewCell *cell = [tableViewImageView dequeueReusableCellWithIdentifier:str];
  if(cell==nil)
  {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
  }
  UIImageView *imageRound = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
  imageRound.layer.masksToBounds = YES;
  imageRound.layer.cornerRadius = 25;
  imageRound.layer.borderWidth = 2.0;
  imageRound.layer.borderColor = [UIColor blueColor].CGColor;
  [cell.contentView addSubview:imageRound];
  cell.imageView.image = [UIImage imageNamed:@"give your image name here"];
  return cell;
}

Thank You-:)

user3182143
  • 9,459
  • 3
  • 32
  • 39