Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios CGContext填充(rect:CGRect)与预期结果不匹配_Ios_Objective C_Swift_Quartz 2d - Fatal编程技术网

Ios CGContext填充(rect:CGRect)与预期结果不匹配

Ios CGContext填充(rect:CGRect)与预期结果不匹配,ios,objective-c,swift,quartz-2d,Ios,Objective C,Swift,Quartz 2d,我是quartz 2d新手,我刚刚创建了一个400*300位图图像上下文,然后用200*200填充一个矩形,但我得到的是一个矩形图像而不是正方形,有人能解释一下吗 override func draw(_ rect: CGRect) { // Drawing code let myBoundingBox = UIScreen.main.bounds let myBitmapContext = myCreateBitmapContext(p

我是quartz 2d新手,我刚刚创建了一个400*300位图图像上下文,然后用200*200填充一个矩形,但我得到的是一个矩形图像而不是正方形,有人能解释一下吗

    override func draw(_ rect: CGRect) {
        // Drawing code

        let myBoundingBox = UIScreen.main.bounds
        let myBitmapContext = myCreateBitmapContext(pixelWidth: 400 , pixelHeight: 300 )
        myBitmapContext?.setFillColor(red: 1, green: 0, blue: 0, alpha: 1)
        myBitmapContext?.fill(.init(x: 0, y: 0, width: 200, height: 200))
        guard let myImage = myBitmapContext?.makeImage() else { return }
        let context = UIGraphicsGetCurrentContext()
        context?.draw(myImage, in: myBoundingBox)

    }

    func myCreateBitmapContext(pixelWidth: Int, pixelHeight: Int) -> CGContext? {

        let bytesPerRow = pixelWidth * 4

        guard let colorSpace = CGColorSpace.init(name: CGColorSpace.sRGB) else { return nil }
        let context = CGContext.init(data: nil, width: pixelWidth, height: pixelHeight, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)
        return context
    }

好吧,图像在绘制时被拉伸,如果我想得到预期的答案,我需要设置Function
draw(u-image:UIImage,在rect:CGRect中,通过平铺:Bool=false)
参数
rect
与图像大小相同,或者更改
contentMode
图像显示方式
imageView
,但是我还不知道如何在石英2D中设置。 然后我更改了代码,最终得到了我期望的结果

context?.draw(myImage, in: CGRect(x: 0, y: 0, width: 400, height: 300))

嗯,在绘图时图像正在拉伸,如果我想得到预期的答案,我需要设置Function
draw(image:UIImage,in-rect:CGRect,通过平铺:Bool=false)
参数
rect
与图像大小相同,或者更改
contentMode
显示的图像,如
imageView
,但是我还不知道如何在石英2D中设置。 然后我更改了代码,最终得到了我期望的结果

context?.draw(myImage, in: CGRect(x: 0, y: 0, width: 400, height: 300))

听起来像是用户界面通过自动布局等方式进行拉伸。听起来像是用户界面通过自动布局等方式进行拉伸。