Reputation: 55705
By default UIGraphicsImageRenderer
sets the scale to the device's screen scale, on iPhone 6s it's 2x and iPhone 6s Plus 3x, therefore even though you've given it a size with dimension 300 it's creating it at either 600 or 900 depending on which device is being used. When you want to ensure it's always 300, how do you set the scale?
let outputBounds = CGRect(x: 0, y: 0, width: 300, height: 300)
let renderer = UIGraphicsImageRenderer(bounds: outputBounds)
let image = renderer.image { context in
//...
}
Previously you would set the scale via the last parameter here:
UIGraphicsBeginImageContextWithOptions(bounds.size, false, 1)
Upvotes: 26
Views: 7430
Reputation: 13127
You should use the UIGraphicsImageRendererFormat
class when creating your UIGraphicsImageRenderer
. If you want to write exact pixels rather than scaled points, use something like this:
let format = UIGraphicsImageRendererFormat()
format.scale = 1
let renderer = UIGraphicsImageRenderer(size: yourImageSize, format: format)
let image = renderer.image { ctx in
// etc
}
I'm keeping a list of iOS 10 code examples and will add one based on this.
Upvotes: 76