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

时间:2015-09-23 20:14:43

标签: ios objective-c uitableview uiimageview

enter image description here

图像通过URL在imageView上设置。当我没有设置图像时,tablecell突出显示就好了,但是当图像应用于它时,它似乎变得不透明,几乎就像图像具有更高的z-index而不是tablview正在应用的高亮视图。这是否意味着我将不得不使用自定义突出显示?

2 个答案:

答案 0 :(得分:0)

当单元格为highlighted时,它会设置标签和图片的highlighted属性。

您需要向highlightedImage提供UIImageView或查看更复杂的解决方案。

答案 1 :(得分:0)

派对可能有点迟,但this extension应该适用于您的情况。

它从URL加载异步图像,然后使用默认的灰色颜色创建相应的突出显示图像。如果需要,可以通过处理 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
    }
}