0

This question already has an answer here:

(1) How to fix UITableView separator on iOS 7?

I met the same question as iOS: UIImageView changes with change in UITableView.rowHeight, how to avoid? described, of course, in SO, there were many Q/As like this, and I tried many answers, but I did not get the right result:

(1) when in ...cellForRowAtIndexPath...:

 NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
NSString *CellLabel = [menuLabels objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = CellLabel;
tableView.rowHeight = 43;

 UIImage* im = [UIImage imageNamed: @"news"];
cell.imageView.image = im;

I got the following: enter image description here

(2) I tried the above answer: based on (1)'s code, I added:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(32,32), YES, 0);
[im drawInRect:CGRectMake(0,0,32,32)];
UIImage* im2 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.imageView.image = im2;
cell.imageView.contentMode = UIViewContentModeCenter;

I got another strange picture: enter image description here

Append:

I hope I got the following result:

enter image description here

here is a continual line under the icon

Community
  • 1
  • 1
abelard2008
  • 1,984
  • 1
  • 20
  • 35
  • Note: for change height of row you have to use method -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath of the UITableViewDelegate protocol. – stosha Mar 04 '15 at 14:13
  • the height of row is the key question, I can change it to bigger in `storyboard`, such as 50 or bigger, of course, `tableView.rowHeight = 43;` can work for me, also, whichever way I used to change the height, I got the same result – abelard2008 Mar 04 '15 at 14:18
  • Is it your really code or you use subclass of UITableViewCell? – stosha Mar 04 '15 at 14:24
  • @stosha, You mean, maybe, my icon is too large? – abelard2008 Mar 04 '15 at 14:45
  • try something like this: tableView.rowHeight = 32.0f; UIImage* im = [UIImage imageNamed: @"news"]; cell.imageView.frame = CGRectMake(0, 0, 32, 32); cell.imageView.image = im; instead of (2) – stosha Mar 04 '15 at 14:55
  • @stosha, I got the result same as my above first picture – abelard2008 Mar 04 '15 at 15:08

1 Answers1

0

Change the boolean setting of BeginImageContextWithOptions from YES to NO and I believe your weird black background will just go away:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(32,32), NO/*Modified*/, 0);
[im drawInRect:CGRectMake(0,0,32,32)];
UIImage* im2 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

From apple documentation about UIGraphicsBeginImageContextWithOptions, NO indicates that it will preserve alpha channel when drawing.

void UIGraphicsBeginImageContextWithOptions ( CGSize size, BOOL opaque, CGFloat scale );

opaque
A Boolean flag indicating whether the bitmap is opaque. If you know the bitmap is fully opaque, specify YES to ignore the alpha channel and optimize the bitmap’s storage. Specifying NO means that the bitmap must include an alpha channel to handle any partially transparent pixels.

Yuchen
  • 30,852
  • 26
  • 164
  • 234
  • the only difference is that I got the first result without black round pad when changing YES to NO, but thank you! – abelard2008 Mar 04 '15 at 15:05
  • @abelard20008, isn't that what you are asking for? If you want to change the size of the image you can change the `32` value to whatever size you want in the `CGSizeMake(32,32)`. – Yuchen Mar 04 '15 at 15:07
  • @Yunchen, I append a picture I hoped in my question , please see it again, thank you – abelard2008 Mar 04 '15 at 15:17
  • @abelard20008 Do you want to say that you want to make the image even bigger and with less margin? That seems to be the only difference I can tell from the screenshot you just posted. – Yuchen Mar 04 '15 at 15:19
  • Sorry man, don't really get what you are asking about. You said that you met the same question as http://stackoverflow.com/questions/25587836/ios-uiimageview-changes-with-change-in-uitableview-rowheight-how-to-avoid. However, matt's solution seems very solid in that answer, changing the contentMode to `UIViewContentModeCenter` will fix the size of the image. So, let me confirm, do you want to change the size of the image with the table view height instead? – Yuchen Mar 04 '15 at 15:35
  • @abelard20008, UITableView×Comments, what do you mean by that? – Yuchen Mar 04 '15 at 15:37
  • Thank you at first. Only I want to extend the line to left side under the image like the third picture in my question. Maybe I did not completely understand the need that author described. Also in many iOS app, you can see the effect like 3rd picture, such as `zhihu` – abelard2008 Mar 04 '15 at 15:45
  • @Yunchen, I am sorry, this was a error because of timeout 5 mins – abelard2008 Mar 04 '15 at 15:48
  • 1
    @abelard20008, this should be what you are for http://stackoverflow.com/questions/18773239/how-to-fix-uitableview-separator-on-ios-7 – Yuchen Mar 04 '15 at 15:54
  • I only want to say, thank you very very much for your warmheated and time. – abelard2008 Mar 04 '15 at 15:59
  • @abelard20008, man, it would be so much easier if you can put your third screenshot upfront and describe the issue without the those unrelated code and unnecessary links. Nice to know that you have what you want. – Yuchen Mar 04 '15 at 16:03