uiimageview在带有clipstobounds的Uiview中添加为子视图

发布于 2025-02-13 06:10:06 字数 1778 浏览 2 评论 0原文

我有一个带有UIImageView的Uiview,如子视图所述,UIImageView是重复的纹理。 Uiview宽度和高度是正确的,但图像不超出大小。我添加了夹具,但根本没有剪裁图像。是否有特定的订单,或者我在做什么错了该图像没有夹在父母视图中?

let rectangleView = UIView(frame: CGRect(x: x, y: y, width: width, height: height))
rectangleView.isUserInteractionEnabled = false

if let texturesUrl = layout.Url, let url = texturesUrl.isValidURL() ? URL(string: texturesUrl) : URL(string: String(format: AppManager.shared.baseTexturesUrl, texturesUrl)) {
     let widthLimit = scale * CGFloat(layout.Width ?? 0)
     let heightLimit = scale * CGFloat(layout.Height ?? 0)
     let widthStep = scale * CGFloat(layout.TileWidth ?? layout.Width ?? 0)
     let heightStep = scale * CGFloat(layout.TileHeight ?? layout.Height ?? 0)

     var locY = CGFloat(0)
     let size = CGSize(width: widthStep, height: heightStep)

     if widthLimit > 0, heightLimit > 0 {
          while locY < heightLimit {
              var locX = CGFloat(0)
                  while locX < widthLimit {
                      let imageView = UIImageView()
                      rectangleView.addSubview(imageView)
                      imageView.contentMode = .scaleAspectFill
                      imageView.translatesAutoresizingMaskIntoConstraints = false
                      imageView.clipsToBounds = true
                      imageView.isUserInteractionEnabled = false
                      imageView.anchor(top: rectangleView.topAnchor, leading: rectangleView.leadingAnchor, bottom: nil, trailing: nil, padding: UIEdgeInsets(top: locY, left: locX, bottom: 0, right: 0), size: size)
                      imageView.setImage(with: url, size: size)
                      locX += widthStep
                  }
                  locY += heightStep
              }
          }
     }

I have a UIView with a UIImageView as subview added, the UIImageView is a texture that repeats. The UIView width and height are correct, but the image is out of the size. I added the ClipsToBounds, but it's not clipping the image at all. Is there a specific order or what am I doing wrong the image is not clipped inside it's parent view?

let rectangleView = UIView(frame: CGRect(x: x, y: y, width: width, height: height))
rectangleView.isUserInteractionEnabled = false

if let texturesUrl = layout.Url, let url = texturesUrl.isValidURL() ? URL(string: texturesUrl) : URL(string: String(format: AppManager.shared.baseTexturesUrl, texturesUrl)) {
     let widthLimit = scale * CGFloat(layout.Width ?? 0)
     let heightLimit = scale * CGFloat(layout.Height ?? 0)
     let widthStep = scale * CGFloat(layout.TileWidth ?? layout.Width ?? 0)
     let heightStep = scale * CGFloat(layout.TileHeight ?? layout.Height ?? 0)

     var locY = CGFloat(0)
     let size = CGSize(width: widthStep, height: heightStep)

     if widthLimit > 0, heightLimit > 0 {
          while locY < heightLimit {
              var locX = CGFloat(0)
                  while locX < widthLimit {
                      let imageView = UIImageView()
                      rectangleView.addSubview(imageView)
                      imageView.contentMode = .scaleAspectFill
                      imageView.translatesAutoresizingMaskIntoConstraints = false
                      imageView.clipsToBounds = true
                      imageView.isUserInteractionEnabled = false
                      imageView.anchor(top: rectangleView.topAnchor, leading: rectangleView.leadingAnchor, bottom: nil, trailing: nil, padding: UIEdgeInsets(top: locY, left: locX, bottom: 0, right: 0), size: size)
                      imageView.setImage(with: url, size: size)
                      locX += widthStep
                  }
                  locY += heightStep
              }
          }
     }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼藏柔 2025-02-20 06:10:07

您不需要添加太多图像视图,只需将其用作重复背景:

rectangleView.backgroundColor = UIColor(patternImage: myImage)

请参阅 uicolor(patternimage :)

You don't need to add so many image views, just use it as a repeating background:

rectangleView.backgroundColor = UIColor(patternImage: myImage)

See documentation for UIColor(patternImage:).

逐鹿 2025-02-20 06:10:07

您可以使用CarePlicatorLayer更有效地完成此操作。

这是一个快速示例:

class TileExampleViewController: UIViewController {
    
    let tiledView = UIView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tiledView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(tiledView)
        
        let g = view.safeAreaLayoutGuide
        NSLayoutConstraint.activate([
            tiledView.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
            tiledView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
            tiledView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
            tiledView.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -20.0),
        ])
        
    }
    
    override func viewDidLayoutSubviews() {
        // we want to do this here, when we know the
        //  size / frame of the tiledView
        
        // make sure we can load the image
        guard let tileImage = UIImage(named: "tileSquare") else { return }
        
        // let's just pick 80 x 80 for the tile size
        let tileSize: CGSize = CGSize(width: 80.0, height: 80.0)

        // create a "horizontal" replicator layer
        let hReplicatorLayer = CAReplicatorLayer()
        hReplicatorLayer.frame.size = tiledView.frame.size
        hReplicatorLayer.masksToBounds = true
        
        // create a "vertical" replicator layer
        let vReplicatorLayer = CAReplicatorLayer()
        vReplicatorLayer.frame.size = tiledView.frame.size
        vReplicatorLayer.masksToBounds = true
        
        // create a layer to hold the image
        let imageLayer = CALayer()
        imageLayer.contents = tileImage.cgImage
        imageLayer.frame.size = tileSize
        
        // add the imageLayer to the horizontal replicator layer
        hReplicatorLayer.addSublayer(imageLayer)
        
        // add the horizontal replicator layer to the vertical replicator layer
        vReplicatorLayer.addSublayer(hReplicatorLayer)

        // how many "tiles" do we need to fill the width
        let hCount = tiledView.frame.width / tileSize.width
        hReplicatorLayer.instanceCount = Int(ceil(hCount))
        
        // Shift each image instance right by tileSize width
        hReplicatorLayer.instanceTransform = CATransform3DMakeTranslation(
            tileSize.width, 0, 0
        )
        
        // how many "rows" do we need to fill the height
        let vCount = tiledView.frame.height / tileSize.height
        vReplicatorLayer.instanceCount = Int(ceil(vCount))
        
        // shift each "row" down by tileSize height
        vReplicatorLayer.instanceTransform = CATransform3DMakeTranslation(
            0, tileSize.height, 0
        )

        // add the vertical replicator layer as a sublayer
        tiledView.layer.addSublayer(vReplicatorLayer)

    }
    
}

我使用了此图片映像:

”在此处输入图像描述

,我们使用让tilesize获得此结果: cgsize = cgsize(width:80.0,高度:80.0)

//i.sstatic.net/tp2z2.png“ alt =”输入图像描述在这里“>

使用让tilesize:cgsize = cgsize(width:120.0,高度:160.0)

“在此处输入图像说明”

with ling tilesize:cgsize = cgsize = cgsize(width:40.0,高度,40.0)< /code>:

“在此处输入图像说明”

You can do this much more efficiently with CAReplicatorLayer.

Here's a quick example:

class TileExampleViewController: UIViewController {
    
    let tiledView = UIView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tiledView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(tiledView)
        
        let g = view.safeAreaLayoutGuide
        NSLayoutConstraint.activate([
            tiledView.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
            tiledView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
            tiledView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
            tiledView.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -20.0),
        ])
        
    }
    
    override func viewDidLayoutSubviews() {
        // we want to do this here, when we know the
        //  size / frame of the tiledView
        
        // make sure we can load the image
        guard let tileImage = UIImage(named: "tileSquare") else { return }
        
        // let's just pick 80 x 80 for the tile size
        let tileSize: CGSize = CGSize(width: 80.0, height: 80.0)

        // create a "horizontal" replicator layer
        let hReplicatorLayer = CAReplicatorLayer()
        hReplicatorLayer.frame.size = tiledView.frame.size
        hReplicatorLayer.masksToBounds = true
        
        // create a "vertical" replicator layer
        let vReplicatorLayer = CAReplicatorLayer()
        vReplicatorLayer.frame.size = tiledView.frame.size
        vReplicatorLayer.masksToBounds = true
        
        // create a layer to hold the image
        let imageLayer = CALayer()
        imageLayer.contents = tileImage.cgImage
        imageLayer.frame.size = tileSize
        
        // add the imageLayer to the horizontal replicator layer
        hReplicatorLayer.addSublayer(imageLayer)
        
        // add the horizontal replicator layer to the vertical replicator layer
        vReplicatorLayer.addSublayer(hReplicatorLayer)

        // how many "tiles" do we need to fill the width
        let hCount = tiledView.frame.width / tileSize.width
        hReplicatorLayer.instanceCount = Int(ceil(hCount))
        
        // Shift each image instance right by tileSize width
        hReplicatorLayer.instanceTransform = CATransform3DMakeTranslation(
            tileSize.width, 0, 0
        )
        
        // how many "rows" do we need to fill the height
        let vCount = tiledView.frame.height / tileSize.height
        vReplicatorLayer.instanceCount = Int(ceil(vCount))
        
        // shift each "row" down by tileSize height
        vReplicatorLayer.instanceTransform = CATransform3DMakeTranslation(
            0, tileSize.height, 0
        )

        // add the vertical replicator layer as a sublayer
        tiledView.layer.addSublayer(vReplicatorLayer)

    }
    
}

I used this tile image:

enter image description here

and we get this result with let tileSize: CGSize = CGSize(width: 80.0, height: 80.0):

enter image description here

with let tileSize: CGSize = CGSize(width: 120.0, height: 160.0):

enter image description here

with let tileSize: CGSize = CGSize(width: 40.0, height: 40.0):

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文