UIImage drawInRect不会按比例绘制

时间:2014-06-28 22:45:23

标签: ios swift

我正在尝试缩小2x图像,然后在矩形中绘制UIImage,但不知怎的,这没有效果。即使属性.scale发生变化,图像也会显示相同的比例。

以下是我的drawRect()

的实施方式
   override func drawRect(rect: CGRect) {
        // Load the artwork
        let areaRect = CGRect(x: 0, y: 0, width: rect.width, height: rect.height)
        var art = UIImage(named: "art-1-square-640")
        // Make the circular mask
        let maskRef = UIImage(named: "art-circular-mask").CGImage
        let maskCircle = CGImageMaskCreate(
            UInt(rect.width),
            UInt(rect.height),
            CGImageGetBitsPerComponent(maskRef),
            CGImageGetBitsPerPixel(maskRef),
            CGImageGetBytesPerRow(maskRef),
            CGImageGetDataProvider(maskRef),
            nil,
            false)
        let artMaskedRef = CGImageCreateWithMask(art.CGImage, maskCircle)
        var artMasked = UIImage(CGImage: artMaskedRef, scale: 0.5, orientation: UIImageOrientation.Up);
        artMasked.drawInRect(areaRect)
    }

1 个答案:

答案 0 :(得分:1)

scale:0.5表示图像中每个像素有两个点而不是普通(对于@ 2x图像)每个点有两个像素。这将使图像变大4倍。但是,drawInRect并不关心,它只是缩小图像以适应矩形。