ios 如何在 TableView 中调整 cell.imageView 的大小并在 Swift 中应用 tintColor
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31624736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to resize a cell.imageView in a TableView and apply tintColor in Swift
提问by Anthony
I have a tableView with a cell created in cellForRowAtIndexPath and add some dummy text:
我有一个在 cellForRowAtIndexPath 中创建的单元格的 tableView 并添加一些虚拟文本:
cell = UITableViewCell(style: UITableViewCellStyle.Subtitle,
reuseIdentifier: "cell")
cell.textLabel?.text = "Test Title"
cell.detailTextLabel?.text = "Test detail label"
Then I add a test image to the cell's imageView:
然后我将测试图像添加到单元格的 imageView:
var image = UIImage(named: "cd.png")
cell.imageView!.image = image
Result:
结果:
To adjust the color, I use the following code:
要调整颜色,我使用以下代码:
cell.imageView?.tintColor = UIColor.redColor()
Result:
结果:
The image is too big in the cell, so I adjust the size using the following code:
单元格中的图像太大,因此我使用以下代码调整大小:
var itemSize:CGSize = CGSizeMake(20, 20)
UIGraphicsBeginImageContextWithOptions(itemSize, false, UIScreen.mainScreen().scale)
var imageRect : CGRect = CGRectMake(0, 0, itemSize.width, itemSize.height)
cell.imageView!.image?.drawInRect(imageRect)
cell.imageView!.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
Whilst the image does resize, the tintColor is lost:
虽然图像确实调整了大小,但 tintColor 丢失了:
Any ideas please?
请问有什么想法吗?
回答by Hamza Ansari
Solution Without custom cell
没有自定义单元格的解决方案
func imageWithImage(image:UIImage,scaledToSize newSize:CGSize)->UIImage{
UIGraphicsBeginImageContext( newSize )
image.drawInRect(CGRect(x: 0,y: 0,width: newSize.width,height: newSize.height))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage.imageWithRenderingMode(.AlwaysTemplate)
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
cell.imageView?.tintColor = UIColor.greenColor()
cell.imageView?.image = imageWithImage(UIImage(named: "imageName")!, scaledToSize: CGSize(width: 20, height: 20))
// Configure the cell...
return cell
}
回答by Ashish Kakkad
You can do it by another way :
你可以通过另一种方式做到这一点:
1) Create custom cell with your own UIImageView
size and 2 separate labels
1) 使用您自己的UIImageView
大小和 2 个单独的标签创建自定义单元格
2) Add UIImageView
as Subview
of Cell
2)添加UIImageView
作为Subview
电池的
var cellImg : UIImageView = UIImageView(frame: CGRectMake(5, 5, 50, 50))
cellImg.image = UIImage(named: "yourimage.png")
cell.addSubview(cellImg)
回答by Kushal Shrestha
For Swift 3
对于 Swift 3
func imageWithImage(image:UIImage,scaledToSize newSize:CGSize)->UIImage{
UIGraphicsBeginImageContext( newSize )
image.draw(in: CGRect(x: 0,y: 0,width: newSize.width,height: newSize.height))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!.withRenderingMode(.alwaysTemplate)
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
cell.imageView?.tintColor = UIColor.greenColor()
cell.imageView?.image = imageWithImage(image: UIImage(named: "imageName")!,scaledToSize: CGSize(width: 20, height: 20))
// Configure the cell...
return cell
}
回答by soheil pakgohar
in swift 4 :
在 swift 4 中:
func image( _ image:UIImage, withSize newSize:CGSize) -> UIImage {
UIGraphicsBeginImageContext(newSize)
image.draw(in: CGRect(x: 0,y: 0,width: newSize.width,height: newSize.height))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!.withRenderingMode(.automatic)
}
cell.imageView?.image = image(UIImage(named: "yourImage.png")!, withSize: CGSize(width: 30, height: 30))
回答by Paul
For OBJECTIVE C without Custom tableViewCell
对于没有自定义 tableViewCell 的 OBJECTIVE C
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"abc" forIndexPath:indexPath];
// Configure the cell...
if (cell==nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"abc"];
}
cell.textLabel.text = [menuItemsArray objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[menuItemsImagesArray objectAtIndex:indexPath.row]];
CGSize itemSize = CGSizeMake(20, 20);
UIGraphicsBeginImageContextWithOptions(itemSize, false, self.view.contentScaleFactor);
CGRect imageRect = CGRectMake(0, 0, itemSize.width, itemSize.height);
[cell.imageView.image drawInRect:imageRect];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return cell; }
回答by Alix
I came here because I was trying to find the answer to same question.
我来这里是因为我试图找到同一问题的答案。
There are several answers that manipulate the image. I was having the same issue. but didn't want to manipulate the image.
有几个答案可以操纵图像。我遇到了同样的问题。但不想操纵图像。
What I did find that my images were 200x200 and dropped them as 1x. I moved them to the 3x section and iOS would automatically resize them more to icon size. For everyone's reference I have added 1x vs 3x comparison
我确实发现我的图像是 200x200 并将它们丢弃为 1x。我将它们移到 3x 部分,iOS 会自动将它们调整为更大的图标大小。供大家参考,我添加了 1x 与 3x 比较
I thought to post as it did solve my issue. It might be helpful to anyone with the same intention as mine.
我想发帖,因为它确实解决了我的问题。这可能对与我有相同意图的人有所帮助。
happy coding.
快乐编码。