问题描述
我的代码大部分都可以工作.
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 图像更改为圆形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!