Ios CGContext填充(rect:CGRect)与预期结果不匹配
我是quartz 2d新手,我刚刚创建了一个400*300位图图像上下文,然后用200*200填充一个矩形,但我得到的是一个矩形图像而不是正方形,有人能解释一下吗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
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))
听起来像是用户界面通过自动布局等方式进行拉伸。听起来像是用户界面通过自动布局等方式进行拉伸。