Graphics Reference
In-Depth Information
Listing 2.2
Using
contentsScale
to Correct the Image Display Scale
@implementation
ViewController
- (
void
)viewDidLoad
{
[
super
viewDidLoad
];
//load an image
UIImage
*image = [
UIImage
imageNamed
:
@"Snowman.png"
];
//add it directly to our view's layer
self
.
layerView
.
layer
.
contents
= (
__bridge
id
)image.
CGImage
;
//center the image
self
.
layerView
.
layer
.
contentsGravity
=
kCAGravityCenter
;
//set the contentsScale to match image
self
.
layerView
.
layer.contentsScale
= image.
scale
;
}
@end
Figure 2.4
The same Retina image displayed with the correct
contentsScale