简体   繁体   English

UITableView单元格在选择时突出显示,但UIImageView保持不透明。 如何突出显示整个单元格?

[英]UITableView cell is highlighted on selection, but the UIImageView remains opaque. How to highlight the entire cell?

在此处输入图片说明

The image is set on the imageView via a URL. 图像是通过URL在imageView上设置的。 When I do not set an image, the tablecell highlights just fine, but when the image is applied to it, it seems to become opaque, almost like the image has a higher z-index compared to the highlight view that the tablview is applying. 当我不设置图像时,表格单元格突出显示就很好了,但是当将图像应用于它时,它似乎变得不透明,几乎就像图像具有比tablview所应用的突出显示视图更高的z索引。 Does this mean I will have to use a custom highlight? 这是否意味着我将不得不使用自定义突出显示?

When the cell is highlighted it sets the highlighted property of labels and images. highlighted单元格时,将设置标签和图像的highlighted属性。

You need to provide a highlightedImage to the UIImageView or look at more complicated solutions. 您需要提供一个highlightedImageUIImageView还是看更复杂的解决方案。

Maybe a bit late for the party but this extension should work for your case. 派对可能会有点晚,但此扩展应该适合您的情况。

It loads async an image from an URL and then create the corresponding highlighted image by using the default gray color. 它从URL加载异步图像,然后使用默认的灰色创建相应的突出显示的图像。 If you want you can change the overlay color by working on the fillcolor parameter. 如果需要,可以通过处理fillcolor参数来更改覆盖颜色。

extension UIImageView {
    func loadImage(url: String, placeHolder: UIImage?) {
        let anURL = URL(string: url)!
        self.image = placeHolder
        let aRequest = URLRequest(url: anURL, cachePolicy: .returnCacheDataElseLoad, timeoutInterval: 10)
        URLSession(configuration: .default).dataTask(with: aRequest, completionHandler: { (data, response, error) in
            DispatchQueue.main.async {
                if let anError = error as NSError? {
                    AlertController.presentOkayAlert(error: anError)
                }
                else if let tmpData = data, let anImage = UIImage(data: tmpData) {
                    self.image = anImage
                    self.highlightedImage = self.filledImage(source: anImage, fillColor: UIColor.colorWithHexString("#c0c0c0", alpha: 0.6))
                }
            }
        }).resume()
    }

    func filledImage(source: UIImage, fillColor: UIColor) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(source.size, false, UIScreen.main.scale)

        let context = UIGraphicsGetCurrentContext()
        fillColor.setFill()
        context!.translateBy(x: 0, y: source.size.height)
        context!.scaleBy(x: 1.0, y: -1.0)
        context!.setBlendMode(CGBlendMode.colorBurn)
        let rect = CGRect(x: 0, y: 0, width: source.size.width, height: source.size.height)
        context!.draw(source.cgImage!, in: rect)
        context!.setBlendMode(CGBlendMode.normal)
        context!.addRect(rect)
        context!.drawPath(using: CGPathDrawingMode.fill)
        let coloredImg : UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()

        return coloredImg
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM