0

我在我的应用程序中使用BCTabBarController,我试图自定义它,以便它使用Core Graphics自动突出显示图像,以便我不需要每个图像的四个副本。 (Retina,Retina-selected,Legacy,Legacy-selected)Retina支持自定义UITabBarController类似于UIImage的突出显示?

用户Ephraim已为此posted a great starting point,但它返回传统大小的图像。我玩过一些设置,但我对Core Graphics不是很熟悉,所以我在黑暗中拍摄。

以法莲代码:

- (UIImage *) imageWithBackgroundColor:(UIColor *)bgColor 
         shadeAlpha1:(CGFloat)alpha1 
         shadeAlpha2:(CGFloat)alpha2 
         shadeAlpha3:(CGFloat)alpha3 
         shadowColor:(UIColor *)shadowColor 
         shadowOffset:(CGSize)shadowOffset 
         shadowBlur:(CGFloat)shadowBlur { 

UIImage *image = self; 

CGColorRef cgColor = [bgColor CGColor]; 
CGColorRef cgShadowColor = [shadowColor CGColor]; 

CGFloat components[16] = {1,1,1,alpha1,1,1,1,alpha1,1,1,1,alpha2,1,1,1,alpha3}; 
CGFloat locations[4] = {0,0.5,0.6,1}; 

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 

CGGradientRef colorGradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, (size_t)4); 

CGRect contextRect; 
contextRect.origin.x = 0.0f; 
contextRect.origin.y = 0.0f; 
contextRect.size = [image size]; 
//contextRect.size = CGSizeMake([image size].width+5,[image size].height+5); 
// Retrieve source image and begin image context 
UIImage *itemImage = image; 
CGSize itemImageSize = [itemImage size]; 
CGPoint itemImagePosition; 
itemImagePosition.x = ceilf((contextRect.size.width - itemImageSize.width)/2); 
itemImagePosition.y = ceilf((contextRect.size.height - itemImageSize.height)/2); 
UIGraphicsBeginImageContext(contextRect.size); 
CGContextRef c = UIGraphicsGetCurrentContext(); 
// Setup shadow 
CGContextSetShadowWithColor(c, shadowOffset, shadowBlur, cgShadowColor); 
// Setup transparency layer and clip to mask 
CGContextBeginTransparencyLayer(c, NULL); 
CGContextScaleCTM(c, 1.0, -1.0); 
CGContextClipToMask(c, CGRectMake(itemImagePosition.x, -itemImagePosition.y, itemImageSize.width, -itemImageSize.height), [itemImage CGImage]); 
// Fill and end the transparency layer 
CGContextSetFillColorWithColor(c, cgColor);  
contextRect.size.height = -contextRect.size.height; 
CGContextFillRect(c, contextRect); 
CGContextDrawLinearGradient(c, colorGradient,CGPointZero,CGPointMake(contextRect.size.width*1.0/4.0,contextRect.size.height),0); 
CGContextEndTransparencyLayer(c); 
//CGPointMake(contextRect.size.width*3.0/4.0, 0) 
// Set selected image and end context 
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

CGColorSpaceRelease(colorSpace); 
CGGradientRelease(colorGradient); 

return resultImage; 

} 

为了实现这个代码,我已在我的项目类别的UIImage,然后做出了如下修改BCTab.h:

- (id)initWithIconImageName:(NSString *)imageName { 
if (self = [super init]) { 
    self.adjustsImageWhenHighlighted = NO; 
    self.background = [UIImage imageNamed:@"BCTabBarController.bundle/tab-background.png"]; 
    self.rightBorder = [UIImage imageNamed:@"BCTabBarController.bundle/tab-right-border.png"]; 
    self.backgroundColor = [UIColor clearColor]; 

//  NSString *selectedName = [NSString stringWithFormat:@"%@-selected.%@", 
//         [imageName stringByDeletingPathExtension], 
//         [imageName pathExtension]]; 


    UIImage *defImage = [UIImage imageNamed:imageName]; 

    [self setImage:[defImage imageWithBackgroundColor:[UIColor lightGrayColor] shadeAlpha1:0.4 shadeAlpha2:0.0 shadeAlpha3:0.6 shadowColor:[UIColor blackColor] shadowOffset:CGSizeMake(0.0, -1.0f) shadowBlur:3.0] forState:UIControlStateNormal]; 
    [self setImage:[defImage imageWithBackgroundColor:[UIColor redColor] shadeAlpha1:0.4 shadeAlpha2:0.0 shadeAlpha3:0.6 shadowColor:[UIColor blackColor] shadowOffset:CGSizeMake(0.0, -1.0f) shadowBlur:3.0] forState:UIControlStateSelected]; 

} 
return self; 
} 

如何使用Ephraim's代码正确使用Retina显示屏?

回答

0

挖掘了互联网后,a Google search带我回到StackOverflow。 I found this answerthis question,其讨论了应当用于在初始化UIImageGraphicsContext时设置比例因子的不同方法。

UIGraphicsBeginImageContext(contextRect.size);需要改变,以UIGraphicsBeginImageContextWithOptions(contextRect.size, NO, scale);,其中“scale”是你要使用的规模 值。我从[[UIScreen mainScreen] scale]抓住它。

+1

更好的是,使用0.0作为比例。然后,UIKit会自动选择一个适合屏幕的值(通过与您手动执行的操作相似的流程)。 – 2012-02-12 20:13:58