eView中将UITableViewCell图像更改为Circl

eView中将UITableViewCell图像更改为Circl

本文介绍了如何在UITableView中将UITableViewCell图像更改为Circle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码大部分工作.

  • 我遇到的问题是图像开始时是正方形,然后在滚动表或刷新表时变为圆形.

我想念什么?

这是我的代码:

    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];
            });
        }
    });

编辑 ----代码位于cellForRowAtIndexPath

EDIT ---- The code is in 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.

如何防止这种情况?

推荐答案

如果有人遇到此问题,以下是

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图像更改为Circle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 23:57