2012-06-21 3 views
2

탭 모음을 다시 만들고 싶지만이 문제가 발생했습니다. 아래 이미지에서 볼 수 있듯이, 현재 선택된 (오른쪽 이미지) 탭 바 항목은 UITabBar의 것보다 훨씬 선명하거나 선명합니다. 왼쪽의 아이콘 주위에 작은 1 포인트 경계가 있음을 알 수 있습니다.이 경계선은 내 눈에 띄는 아이콘 안에있는 그라디언트뿐입니다. 이미 Core Graphics 및 Core Images Filters를 가능한 접근 방식으로 생각했지만 그 효과를 얻지 못하는 것 같습니다. 나는 내가 원하는 부분의 일부인 더 오래된 thread을 발견했다. 그러나 대답은 나를 위해 작동하지 않는 것처럼 보이고 이미지의 픽셀을 통해 수동 루프가 필요하다. (내가 원하는지 모르겠다.) 누군가 나를 도울 수 있습니까? current선택한 UITabBarItem 테두리 다시 만들기

desired 은 내가 코어 그래픽으로 시작하고 어떤 때문에 보는 경우에 BTW, 당신은 몇 가지 실수를 수정하기 환영합니다, 제가 현재 사용하고 코드입니다 :

- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextSaveGState(context); 
    { 
     /* Adjust for different coordinate systems from UIKit and Core Graphics and center the image */ 
     CGContextTranslateCTM(context, self.bounds.size.width/2.0 - self.image.size.width/2.0, self.bounds.size.height - self.bounds.size.height/2.0 + self.image.size.height/2.0); 
     CGContextScaleCTM(context, 1.0f, -1.0f); 

     CGRect rect = CGRectMake(0, 0, self.image.size.width, self.image.size.height); 

     /* Add a drop shadow */ 
     UIColor *dropShadowColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.8f]; 
     CGContextSetShadowWithColor(context, CGSizeMake(0, 1), 5, dropShadowColor.CGColor); 

     /* Draw the original image */ 
     CGContextDrawImage(context, rect, self.image.CGImage); 

     /* Clip to the original image, so that we only draw the shadows on the 
      inside of the image but nothing outside. */ 
     CGContextClipToMask(context, rect, self.image.CGImage); 

     if(self.isSelected){ 
      /* draw background image */ 
      CGImageRef background = [UIImage imageNamed:@"UITabBarBlueGradient"].CGImage; 
      CGContextDrawImage(context, rect, background); 
     } 
     else{ 
      /* draw background color to unselected items */ 
      CGColorRef backgroundColor = [UIColor colorWithRed:95/255.0 green:95/255.0 blue:95/255.0 alpha:1].CGColor; 
      CGContextSetFillColorWithColor(context, backgroundColor); 
      CGContextFillRect(context, rect); 

      /* location of the gradient's colors */ 
      CGFloat locations[] = { 0.0, 1.0 }; 

      NSArray *colors = [NSArray arrayWithObjects:(id)[UIColor colorWithRed:1 green:1 blue:1 alpha:0].CGColor, (id)[UIColor colorWithRed:1 green:1 blue:1 alpha:0.6].CGColor, nil]; 

      /* create the gradient with colors and locations */ 
      CGGradientRef gradient = CGGradientCreateWithColors(colorSpace,(__bridge CFArrayRef) colors, locations); 
      { 
       /* start and end points of the gradient */ 
       CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect)); 
       CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect)); 
       /* draw gradient */ 
       CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0); 
      } 
      CGGradientRelease(gradient); 
     } 
    } 
    CGContextRestoreGState(context); 
    CGColorSpaceRelease(colorSpace); 
} 

답변

1

나는 이것에 대해서도 일하고있다. 당신이 만들 수있는 최적화는 drawii가 호출 될 때마다 UIImage를 렌더링하는 것이 아니라 ivar에서 UIImage 객체를 저장하고 UIImageView.image 속성을 업데이트하여 표시 할 수 있도록하는 것이다.

나는이 같은 "빛"내 이미지를 생성하고 있습니다 :

(plus_icon.png 투명한 배경에 검은 색 전체 일을 점유하는 4 픽셀 넓은 십자가와 30 X 30 이미지 : . rendered plus icon

-(UIImage *)tabBarImage{ 

    UIGraphicsBeginImageContext(CGSizeMake(60, 60)); 

    UIImage *image = [UIImage imageNamed:@"plus_icon"]; 

    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextSetFillColorWithColor(ctx, [[UIColor clearColor] CGColor]); 
    CGContextFillRect(ctx, CGRectMake(0, 0, 60, 60)); 

    CGRect imageRect = CGRectMake(15, 15, 30, 30); 
    CGContextDrawImage(ctx, imageRect, [image CGImage]); 

    image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return image; 
} 

-(UIImage *)sourceImage{ 
    UIGraphicsBeginImageContext(CGSizeMake(60.0, 60.0)); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    size_t num_locations = 2; 
    CGFloat locations[2] = { 0.3, 1.0 }; 

    CGFloat components[8] = {NC(72), NC(122), NC(229), 1.0, NC(110), NC(202), NC(255), 1.0 }; 

    CGColorSpaceRef cspace; 
    CGGradientRef gradient; 
    cspace = CGColorSpaceCreateDeviceRGB(); 
    gradient = CGGradientCreateWithColorComponents (cspace, components, locations, num_locations); 

    CGPoint sPoint = CGPointMake(0.0, 15.0); 
    CGPoint ePoint = CGPointMake(0.0, 45.0); 
    CGContextDrawLinearGradient (context, gradient, sPoint, ePoint, kCGGradientDrawsBeforeStartLocation| kCGGradientDrawsAfterEndLocation); 
    CGGradientRelease(gradient); 
    CGColorSpaceRelease(cspace); 
    [self addShineToContext:context]; 

    UIImage * image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext();  

    return image; 
} 

-(void)addShineToContext:(CGContextRef) context{ 
    CGContextSaveGState(context); 
    size_t num_locations = 2; 
    CGFloat locations[2] = { 0.3, 0.7}; 
    CGFloat components[8] = {1.0, 1.0, 1.0, 0.8, 1.0, 1.0, 1.0, 0.0};//{0.82, 0.82, 0.82, 0.4, 0.92, 0.92, 0.92, .8 }; 

    CGColorSpaceRef cspace; 
    CGGradientRef gradient; 
    cspace = CGColorSpaceCreateDeviceRGB(); 
    gradient = CGGradientCreateWithColorComponents (cspace, components, locations, num_locations); 

    CGPoint sPoint = CGPointMake(25.0f, 15.0); 
    CGPoint ePoint = CGPointMake(35.0f, 44.0f); 


    [self addShineClip:context]; 

    CGContextDrawLinearGradient(context, gradient, sPoint, ePoint, kCGGradientDrawsBeforeStartLocation); 
// CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]); 
// CGContextFillRect(context, CGRectMake(15,15, 30, 30)); 

    CGColorSpaceRelease(cspace); 
    CGGradientRelease(gradient); 

    CGContextRestoreGState(context); 

} 

-(void)addShineClip:(CGContextRef)context{ 
    CGContextMoveToPoint(context, 15, 35); 
    CGContextAddQuadCurveToPoint(context, 25, 30, 45, 28); 
    CGContextAddLineToPoint(context, 45, 15); 
    CGContextAddLineToPoint(context, 15, 15); 
    CGContextClosePath(context); 
    CGContextClip(context); 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    self.imageView1.image = [self compositeOverSlate:[self drawTabBarOverSourceWithBlend:kCGBlendModeSourceIn]]; 
    self.imageView2.image = [self compositeOverSlate:[self drawTabBarOverSourceWithBlend:kCGBlendModeDestinationIn]]; 
    self.imageView3.image = [self compositeOverSlate:[self drawTabBarOverSourceWithBlend:kCGBlendModeSourceAtop]]; 
    self.imageView4.image = [self compositeOverSlate:[self drawTabBarOverSourceWithBlend:kCGBlendModeDestinationAtop]]; 
} 

-(UIImage *)compositeOverSlate:(UIImage *)image{ 
    UIGraphicsBeginImageContext(image.size); 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGRect imageRect = CGRectMake(0, 0, 0, 0); 
    imageRect.size = image.size; 

    CGContextSetFillColorWithColor(ctx, [[UIColor darkGrayColor] CGColor]); 

    CGContextFillRect(ctx, imageRect); 
    CGContextSetShadow(ctx, CGSizeMake(-1.0, 2.0), .5); 
    CGContextDrawImage(ctx, imageRect, [image CGImage]); 

    UIImage *result = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 
    return result; 
} 

-(UIImage *)drawTabBarOverSourceWithBlend:(CGBlendMode)blendMode{ 
    UIGraphicsBeginImageContext(CGSizeMake(60,60)); 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 

    CGContextDrawImage(ctx, CGRectMake(0, 0, 60.0, 60.0), [[self sourceImage] CGImage]); 

    CGContextSetBlendMode(ctx, blendMode); 
    CGContextDrawImage(ctx, CGRectMake(0, 0, 60.0, 60.0), [[self tabBarImage] CGImage]); 

    UIImage *result = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return result; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

하지만 경계의 윤곽이 아직 금이없는,하지만 난 그것을 깰 경우 업데이트됩니다

이 같은 이미지 뷰 2와 4에서처럼 렌더링하는