<small id='YSKH6'></small><noframes id='YSKH6'>

    1. <i id='YSKH6'><tr id='YSKH6'><dt id='YSKH6'><q id='YSKH6'><span id='YSKH6'><b id='YSKH6'><form id='YSKH6'><ins id='YSKH6'></ins><ul id='YSKH6'></ul><sub id='YSKH6'></sub></form><legend id='YSKH6'></legend><bdo id='YSKH6'><pre id='YSKH6'><center id='YSKH6'></center></pre></bdo></b><th id='YSKH6'></th></span></q></dt></tr></i><div id='YSKH6'><tfoot id='YSKH6'></tfoot><dl id='YSKH6'><fieldset id='YSKH6'></fieldset></dl></div>
    2. <legend id='YSKH6'><style id='YSKH6'><dir id='YSKH6'><q id='YSKH6'></q></dir></style></legend>
      <tfoot id='YSKH6'></tfoot>

          <bdo id='YSKH6'></bdo><ul id='YSKH6'></ul>

        如何在 UITableView 中将 UITableViewCell 图像更改为圆形

        时间:2024-04-14
          <tbody id='YWVow'></tbody>

          <small id='YWVow'></small><noframes id='YWVow'>

            <legend id='YWVow'><style id='YWVow'><dir id='YWVow'><q id='YWVow'></q></dir></style></legend>

              <i id='YWVow'><tr id='YWVow'><dt id='YWVow'><q id='YWVow'><span id='YWVow'><b id='YWVow'><form id='YWVow'><ins id='YWVow'></ins><ul id='YWVow'></ul><sub id='YWVow'></sub></form><legend id='YWVow'></legend><bdo id='YWVow'><pre id='YWVow'><center id='YWVow'></center></pre></bdo></b><th id='YWVow'></th></span></q></dt></tr></i><div id='YWVow'><tfoot id='YWVow'></tfoot><dl id='YWVow'><fieldset id='YWVow'></fieldset></dl></div>

                <bdo id='YWVow'></bdo><ul id='YWVow'></ul>
                <tfoot id='YWVow'></tfoot>
                  本文介绍了如何在 UITableView 中将 UITableViewCell 图像更改为圆形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的代码大部分都可以工作.

                  My code works for the most part.

                  • 我遇到的问题是图像一开始是方形的,当我滚动表格或刷新表格时变为圆形.

                  我错过了什么?

                  这是我的代码:

                      cell.imageView.layer.cornerRadius = cell.imageView.frame.size.width / 2.0;
                      cell.imageView.layer.borderWidth = 3.0;
                      cell.imageView.layer.borderColor = [UIColor colorWithRed:157.0/255.0 green:34.0/255.0 blue:53.0/255.0 alpha:1.0]
                      .CGColor;
                      cell.imageView.clipsToBounds = YES;
                  
                      NSDictionary *tweet = (self.results)[indexPath.row];
                      
                      dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
                      dispatch_async(queue, ^{
                      
                          NSString * imageString = [tweet valueForKeyPath:@"user.profile_image_url"];
                          NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:imageString]];
                          
                          if (imageData != nil)
                          {
                              dispatch_async(dispatch_get_main_queue(), ^{
                                  
                                  cell.imageView.image = [UIImage imageWithData: imageData];
                  
                                  [cell setNeedsLayout];
                              });
                          }
                      });
                  

                  EDIT ---- 代码在cellForRowAtIndexPath

                  • 当我启动应用程序时,单元格图像是方形的.如果我 pullToRefresh 更改为圆形,或者如果我开始滚动表格,它们将更改为圆形.
                  • Right when I launch the app the cell images are square. If I pullToRefresh the change to round or if I start scrolling the table they change to round.

                  编辑两个

                  我看到的另一个问题是滚动时图像会发生变化.

                  Another issue I am seeing is the images change as I scroll.

                  如何防止这种情况发生?

                  How do I prevent this?

                  推荐答案

                  如果有人遇到这个问题,这里是解决方案

                  In order if anyone having this problem, here is the solution in

                  斯威夫特

                  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
                      var cell:ContactCellTableViewCell? = tableView.dequeueReusableCellWithIdentifier("contactCell", forIndexPath: indexPath) as? ContactCellTableViewCell
                  
                  
                  
                      var contact : ContactStruct
                      image = searchResults[indexPath.row]
                      let newImage = resizeImage(image, toTheSize: CGSizeMake(70, 70))
                      var cellImageLayer: CALayer?  = cell?.imageView.layer
                      cellImageLayer!.cornerRadius = 35
                      cellImageLayer!.masksToBounds = true
                      cell?.imageView.image = newImage
                  
                  
                      return cell!
                  }
                  

                  您可能会注意到 35 硬编码基本上是图像大小的一半,即此处的 70.这是调整大小的功能:

                  As you might noticed the 35 hardcode is basically half of the image size which is 70 in here. And here is the resize function:

                  func resizeImage(image:UIImage, toTheSize size:CGSize)->UIImage{
                  
                  
                      var scale = CGFloat(max(size.width/image.size.width,
                          size.height/image.size.height))
                      var width:CGFloat  = image.size.width * scale
                      var height:CGFloat = image.size.height * scale;
                  
                      var rr:CGRect = CGRectMake( 0, 0, width, height);
                  
                      UIGraphicsBeginImageContextWithOptions(size, false, 0);
                      image.drawInRect(rr)
                      let newImage = UIGraphicsGetImageFromCurrentImageContext()
                      UIGraphicsEndImageContext();
                      return newImage
                  }
                  

                  注意:我使用的自定义单元格类如下:

                  Note: I am using a custom cell class as below:

                  import UIKit
                  
                  class ContactCellTableViewCell: UITableViewCell {
                  
                      @IBOutlet weak var nameLabel: UILabel!
                      @IBOutlet weak var contactImage: UIImageView!
                      @IBOutlet weak var phoneLabel: UILabel!
                  
                  
                  
                  
                      override func awakeFromNib() {
                          super.awakeFromNib()
                  
                      }
                  
                      override func setSelected(selected: Bool, animated: Bool) {
                          super.setSelected(selected, animated: animated)
                  
                          // Configure the view for the selected state
                      }
                  }
                  

                  我确实花了一段时间才弄清楚这一点.最初滚动后图像会变圆,但现在使用此代码,您将从一开始就将圆角图像放在单元格内.希望它对任何人都有帮助.

                  It really took me a while to figure this one out. The images become rounded after scrolling at first, but now using this code you are going to have the rounded images inside the cell from beginning. Hope it helped anyone.

                  这篇关于如何在 UITableView 中将 UITableViewCell 图像更改为圆形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:更改 UIImageView 模式 iPhone/iPad 下一篇:无法转换类型 '() -&gt; 的值_' 指定类型 UIImageView

                  相关文章

                  <tfoot id='T2jgi'></tfoot>

                    1. <legend id='T2jgi'><style id='T2jgi'><dir id='T2jgi'><q id='T2jgi'></q></dir></style></legend>
                      <i id='T2jgi'><tr id='T2jgi'><dt id='T2jgi'><q id='T2jgi'><span id='T2jgi'><b id='T2jgi'><form id='T2jgi'><ins id='T2jgi'></ins><ul id='T2jgi'></ul><sub id='T2jgi'></sub></form><legend id='T2jgi'></legend><bdo id='T2jgi'><pre id='T2jgi'><center id='T2jgi'></center></pre></bdo></b><th id='T2jgi'></th></span></q></dt></tr></i><div id='T2jgi'><tfoot id='T2jgi'></tfoot><dl id='T2jgi'><fieldset id='T2jgi'></fieldset></dl></div>

                      • <bdo id='T2jgi'></bdo><ul id='T2jgi'></ul>

                      <small id='T2jgi'></small><noframes id='T2jgi'>