I came across a strange thing. It looks like the UIView contentScaleFactor always 1, even on Retina devices, unless you implement drawRect: Consider this code:
@interface MyView : UIView @end @implementation MyView - (id) initWithFrame: (CGRect) frame { self = [super initWithFrame: frame]; if (self) { NSLog(@"%s %g %g %g", __PRETTY_FUNCTION__, self.contentScaleFactor, self.layer.contentsScale, [UIScreen mainScreen].scale); } return self; } - (void) didMoveToWindow { if (self.window) NSLog(@"%s %g %g %g", __PRETTY_FUNCTION__, self.contentScaleFactor, self.layer.contentsScale, [UIScreen mainScreen].scale); } @end
On a Retina device, it prints the following:
-[MyView initWithFrame:] 1 1 2 -[MyView didMoveToWindow] 1 1 2
If I add an empty drawRect: implementation as follows:
- (void) drawRect: (CGRect) rect { }
works as expected:
-[MyView initWithFrame:] 2 2 2 -[MyView didMoveToWindow] 2 2 2
So it looks like it doesn't matter if there is a view in any hierarchy of views and on what screen it is displayed. The only thing that matters is that the view implements drawRect: or not.
Is this a bug or function? I know that I can change didMoveToWindow as shown below to fix it
- (void) didMoveToWindow { if (self.window) self.contentScaleFactor = self.window.screen.scale; }
but the default behavior is still bothering me.
You may ask why I need a contentScaleFactor at all if I am not drawing anything. This is because I just set self.layer.contents to the finished image, and then stretched the image using contentStretch . However, the image does not stretch properly on Retina devices if the contentScaleFactor not set correctly, even if the @2x image is used. To be precise, it works correctly if the @2x image is not used. This, I think, is a mistake.
Can someone share their understanding of why contentScaleFactor behaves this way? Is this specific to iOS 5 only?