简体   繁体   English

如何在 swift 的 collectionview 单元格外裁剪部分图像

[英]How to crop part of the image outside of a collectionview cell in swift

I have a simple design app where i have just a round image on the top right corner.我有一个简单的设计应用程序,右上角只有一个圆形图像。 I am trying to do something like what they have on the bloom app.我正在尝试做一些类似于他们在 bloom 应用程序上所做的事情。

在此处输入图像描述

See how the edge of the image is getting cut off at the edge of the cell, I am trying to go for something like that, but I keep getting this.看看图像的边缘是如何在单元格的边缘被切断的,我正在尝试 go 以获取类似的东西,但我一直得到这个。

在此处输入图像描述

I had a feeling that the entire cell is just an image so that it how it looks like that, but I don't know if making an entire cell image is what i want to do, since I plan to eventually be able to change the cell's color and the circle image's color at different times, and use different color, so it is not in my interest to use just a large image for the cell, since I might want to use different images too.我有一种感觉,整个细胞只是一个图像,所以它看起来像那样,但我不知道制作整个细胞图像是否是我想要做的,因为我计划最终能够改变单元格的颜色和圆形图像在不同时间的颜色,并使用不同的颜色,所以我不感兴趣只为单元格使用大图像,因为我可能也想使用不同的图像。 It also seems to be easier to figure this out, than just create a couple dozen different images, which also increases it's app size.解决这个问题似乎也比仅仅创建几十个不同的图像更容易,这也增加了它的应用程序大小。

I have looked at a few different posts, but none of them seems to work for me.我看过几个不同的帖子,但似乎没有一个对我有用。

I tried:我试过:

  • setting the frame of the image and background to the bounds将图像和背景的框架设置为边界
  • using all the different content mode使用所有不同的内容模式
  • using clipsToBound, and masksToBounds and set them to either true and false.使用 clipsToBound 和 masksToBounds 并将它们设置为 true 或 false。

this is what is in my cell:这是我的牢房里的东西:

    contentView.addSubview(cellBackgrounded)
        cellBackgrounded.addSubview(imgView)
//        imgView.frame = cellBackgrounded.bounds  set this to = self.bounds, contentView.frame
//        contentView.frame = self.bounds
//        cellBackgrounded.frame = self.bounds
//        imgView.layer.masksToBounds = false
        imgView.translatesAutoresizingMaskIntoConstraints = false
        imgView.image = UIImage(named: "Oval")?.withRenderingMode(.alwaysTemplate)
        imgView.clipsToBounds = true
        imgView.contentMode = .scaleToFill
        imgView.tintColor = .red
        
        cellBackgrounded.translatesAutoresizingMaskIntoConstraints = false
        cellBackgrounded.backgroundColor = .white
        cellBackgrounded.addSubview(cellName)
        cellName.translatesAutoresizingMaskIntoConstraints = false
        cellName.font = UIFontMetrics.default.scaledFont(for: UIFont.systemFont(ofSize: 50, weight: .bold))
        cellName.textColor = .black
        cellName.numberOfLines = 0
        cellName.textAlignment = .center
        
        darkShadow.frame = self.bounds
        darkShadow.cornerRadius = 15
        darkShadow.backgroundColor = UIColor.offWhite.cgColor
        darkShadow.shadowColor = UIColor.black.withAlphaComponent(0.2).cgColor
        darkShadow.shadowOffset = CGSize(width: 10, height: 10)
        darkShadow.shadowOpacity = 1
        darkShadow.shadowRadius = 15
        self.layer.insertSublayer(darkShadow, at: 0)
        
        NSLayoutConstraint.activate([
            cellBackgrounded.topAnchor.constraint(equalTo: topAnchor),
            cellBackgrounded.leadingAnchor.constraint(equalTo: leadingAnchor),
            cellBackgrounded.trailingAnchor.constraint(equalTo: trailingAnchor),
            cellBackgrounded.bottomAnchor.constraint(equalTo: bottomAnchor),
            imgView.topAnchor.constraint(equalTo: cellBackgrounded.topAnchor, constant: -40),
            imgView.trailingAnchor.constraint(equalTo: cellBackgrounded.trailingAnchor, constant: 40),
            cellName.centerXAnchor.constraint(equalTo: cellBackgrounded.centerXAnchor),
            cellName.centerYAnchor.constraint(equalTo: cellBackgrounded.centerYAnchor),
            cellName.leadingAnchor.constraint(equalTo: cellBackgrounded.leadingAnchor, constant: 10),
            cellName.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -10),
            imgView.heightAnchor.constraint(equalTo: cellBackgrounded.heightAnchor, multiplier: 0.7),
            imgView.widthAnchor.constraint(equalTo: cellBackgrounded.widthAnchor, multiplier: 0.7)
        ])

if there is anything else i can help with please ask, Thank you如果还有什么我可以帮忙的,请询问,谢谢

Use clipToBounds使用clipToBounds

According to apple docs:根据苹果文档:

When the value of this property is true, Core Animation creates an implicit clipping mask that matches the bounds of the layer and includes any corner radius effects当此属性的值为 true 时,Core Animation 会创建一个隐式剪切蒙版,该蒙版与图层的边界相匹配,并包括任何角半径效果

You must do clipsToBound to the containerView to solve this problem您必须对 containerView 执行 clipsToBound 才能解决此问题

containerView.clipsToBound = true

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

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