2013-07-31 137 views
1

我在渲染具有自定義外觀設置的滑塊的視圖控制器的圖像時出現問題。圖像呈現很好,但是,圖像中的滑塊僅具有默認外觀。如果我去那個viewcontroller(vc2),uiappearance被設置爲自定義外觀。任何想法如何使用自定義滑塊外觀呈現圖像?用UIAppearance渲染圖像

我在第一個視圖控制器上有一個UIImageView。在第二個視圖控制器上,我有一個滑塊。

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]; 

    } 
+0

這可能與您在獲取圖像之前未顯示視圖控制器的視圖有關。嘗試推動並拍攝圖像,然後立即解散。 – Greg

+0

@PartiallyFinite:即使我將其更改爲[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];和第一個視圖控制器的滑塊,它仍然會使用默認滑塊。奇怪的權利? –

+1

根據[這個答案](http://stackoverflow.com/a/13322872/834998),'UIAppearance'自定義只在layoutSubviews在視圖上被調用之前應用,所以它們可能沒有被應用於你得到快照的時間。 – Greg

回答

0

感謝@PartiallyFinite方向,我想通了,我需要做的視圖控制器內的下列:

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];