I have this UIButton, he has an image, on normal iPhone it works fine, the image contentMode is set to UIViewcontentModeCenter and its fine. but on retina, the image is not center, it’s big and doesn’t fit into the button frame.
Why is that and how can I fix it?
[theImageView setImage:Image forState:UIControlStateNormal];
[theImageButton setImage:Image forState:UIControlStateHighlighted];
[theImageButton setImage:Image forState:UIControlStateSelected];
[theImageButton setBackgroundColor:[UIColor clearColor]];
[theImageButton addTarget:self action:@selector(dismissKeyboard:) forControlEvents:UIControlEventAllTouchEvents];
theImageButton.imageView.contentMode = UIViewContentModeCenter;
The image loading:
- (UIImage *)loadScreenShotImageFromDocumentsDirectory
{
UIImage * tempimage;
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/MeasureScreenShot.png", docDirectory];
tempimage = [[UIImage alloc] initWithContentsOfFile:filePath];
return tempimage;
}
I have to say, that the image that loaded into the contentMode is a screen shot that has been taken programmatically. maybe it has to do with it (anyway on normal display it works fine).
Thanks!
Sounds like you’re getting a retina image, with 1.0 scale. You need to tell the system, that your image is actually 2.0, if you use contexts to capture screenshot, use
0.0tells it to use current device’s main screen scale, or you can specify correct scale yourself.