Morgan Wilde
Morgan Wilde

Reputation: 17303

UIImage drawInRect doesn't draw with scale

I'm trying to scale down a 2x image and then draw a UIImage in a rect, but somehow that has no effect. Even though the property .scale changes, the images appears to be of the same scale.

Here's the implementation for my 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)
    }

Upvotes: 2

Views: 1830

Answers (1)

user3386109
user3386109

Reputation: 34829

The scale:0.5 says that that there are two points per pixel in the image instead of the normal (for a @2x image) two pixels per point. That would make the image 4x bigger. However, drawInRect doesn't care, it just scales the image back down to fit the rectangle.

Upvotes: 2

Related Questions