如何将子视图图像制作成Uiimageview

时间:2019-06-24 11:03:16

标签: swift uiimageview subview

我想让(mosaic.addSubview(pixel))图像显示到UIImageView中。让我运行另一个功能。

如果仅使用(imageView.image = image)来生成图像,它将失去一些我不知道为什么的功能。我该怎么做才能使图像 (imageView.image)= mosaic.addSubview(pixel)

@IBOutlet weak var imageView: UIImageView!

let mosaic = UIView()

func createMosaic(_ image: UIImage) {

    self.navigationController?.popToViewController(self, animated: true)

    let time = Date()      
    let scale: Int = 5
    let imageWidth = Int(image.size.width)
    let imageHeight = Int(image.size.height)

    let width = imageWidth * scale
    let height = imageHeight * scale
    let x = (Int(view.bounds.width) - width) / 2
    let y = (Int(view.bounds.height) - height) / 3
    mosaic.frame = CGRect(x: x, y: y, width: width, height: height)

    let rect = CGRect(x: 0, y: 0, width: 32, height: 32)

    for x in 0 ..< imageWidth {
        for y in 0 ..< imageHeight {
            let color: UIColor = image.getPixelColor(CGPoint(x: x, y: y))
            let background = UIImage.fromColor(color: color)

            let imagee = UIImage(named: "Image-\(arc4random_uniform(256))")!

            let renderer = UIGraphicsImageRenderer(size: rect.size)

            let result = renderer.image { ctx in
                UIColor.white.set()
                ctx.fill(rect)
                background.draw(in: rect, blendMode: .normal, alpha: 1)
                imagee.draw(in: rect, blendMode: .overlay, alpha: 1)
            }
            let pixel = UIImageView.init(frame: CGRect(x: x * scale, y: y * scale, width: scale, height: scale))
            pixel.image = result
            mosaic.addSubview(pixel)

            // This line will render a image perfectly

            imageView.image = image

            // I would like to make the subview to be this imageView
        }

0 个答案:

没有答案