Open1

CGContextを作ってそこに書き込む

nabeyangnabeyang

適当な領域CGRectを作って円を2つ描き込む

        let width = 350
        let height = 350
        let bytesPerRow: Int = 4 * width    
        guard let context = CGContext(data: nil,
                                    width: width,
                                    height: height,
                                    bitsPerComponent: 8,
                                    bytesPerRow: bytesPerRow,
                                    space: CGColorSpaceCreateDeviceRGB(),
                                       bitmapInfo: CGImageAlphaInfo.premultipliedFirst.rawValue | kCGBitmapByteOrder32Host.rawValue) else {
            return
        }
        let cx = width / 3
        let cy = height / 3
        let r = 50
        let circle =  UIBezierPath(arcCenter: CGPoint(x: cx, y: cy), radius: CGFloat(r), startAngle: 0, endAngle: CGFloat(Double.pi) * 2, clockwise: true)
        context.addPath(circle.cgPath)
        context.drawPath(using: .fill)
        do {
            let cx = width / 3 + 100
            let cy = height / 3 + 100
            let r = 50
            let circle =  UIBezierPath(arcCenter: CGPoint(x: cx, y: cy), radius: CGFloat(r), startAngle: 0, endAngle: CGFloat(Double.pi) * 2, clockwise: true)
            context.addPath(circle.cgPath)
            context.setFillColor(UIColor.red.cgColor)
            context.drawPath(using: .fill)
        }
        guard let cgImage = context.makeImage() else { return }
        let image = UIImage(cgImage: cgImage)
        let size = image.size
        let imageView = UIImageView(image: image)
        ctrl.view.addSubview(imageView)