1
votes

I am having a problem rendering an image of a viewcontroller that has a slider with a custom uiappearance set. The image renders fine, however, the slider in the image has just the default look. If I go to that viewcontroller (vc2), the uiappearance is set to the custom look. Any ideas how to render an image with the custom slider appearance?

I have a UIImageView on the First View Controller. On the Second View Controller, I have a slider.

ViewController.m

ViewController *vc2 = [[ViewController alloc]init];
vc2 = [self.storyboard instantiateViewControllerWithIdentifier:@"vc2"];
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size,NO,0.0);
[vc2.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[_image setImage:viewImage];

AppDelegate.m

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary      *)launchOptions
    {
    [self customizeAppearance];
    // Override point for customization after application launch.
    return YES;
    }
    - (void) customizeAppearance; {
    UIImage *minImage = [[UIImage imageNamed:@"slider_minimum.png"]
                         resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 0)];
    UIImage *maxImage = [[UIImage imageNamed:@"slider_maximum.png"]
                         resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 13)];
    UIImage *thumbImage = [UIImage imageNamed:@"slider_thumb.png"];

    [[UISlider appearance] setMaximumTrackImage:maxImage
                                       forState:UIControlStateNormal];
    [[UISlider appearance] setMinimumTrackImage:minImage
                                       forState:UIControlStateNormal];
    [[UISlider appearance] setThumbImage:thumbImage
                                forState:UIControlStateNormal];
    [[UISlider appearance] setThumbImage:thumbImage
                                forState:UIControlStateHighlighted];

    }
1
It could have something to do with the fact that you're not displaying the view controller's view before getting an image of it. Try pushing it and taking the image, then immediately dismissing. - Greg
@PartiallyFinite: Even if I change it to [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; and a slider to the first viewcontroller, it still does the default slider. Weird right? - Sam Teedo
According to this answer, UIAppearance customisations are only applied just before layoutSubviews is called on the view, so they probably haven't been applied at the time you're getting the snapshot. - Greg

1 Answers

0
votes

Thanks to @PartiallyFinite direction, I figured out that I needed to do the following inside of ViewController.:

UIImage *minImage = [UIImage imageNamed:@"slider_minimum.png"];
UIImage *maxImage = [UIImage imageNamed:@"slider_maximum.png"];
UIImage *tumbImage= [UIImage imageNamed:@"slider_thumb.png"];

minImage = [minImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, 3, 0, 0)];
maxImage = [maxImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 13)];

[_slider setMinimumTrackImage:minImage forState:UIControlStateNormal];
[_slider setMaximumTrackImage:maxImage forState:UIControlStateNormal];
[_slider setThumbImage:tumbImage forState:UIControlStateNormal];