ios - 重新创建选定的UITabBarItem边框

标签 ios ios5 core-graphics core-image

我想重新创建一个标签栏,但我偶然发现了这个问题。正如您在我当前(右图)下面的图像中看到的那样,所选择的选项卡栏项目比UITabBar中的选项要清晰或锐利得多。请注意左侧图标周围的1点小边框(我不知道该怎么做)以及图标内部的渐变,这在我的身上非常明显。我已经将“核心图形”和“核心图像过滤器”视为可能的方法,但似乎无法获得这种效果。我找到了一个较旧的thread,它是我想要的一部分,但答案似乎对我不起作用,并且需要手动遍历图像的像素(我不知道是否需要这样做)。有人能帮我吗?


这是我当前正在使用的代码,顺便说一句,如果您看到任何错误,欢迎您纠正一些错误,因为我是从Core Graphics开始的:

- (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);
}

最佳答案

我也在做这个工作,您可能可以做的优化是,不必在每次调用drawrect时呈现UIImage,而是可以将UIImage对象保存在ivar中,而只需更新UIImageView.image属性以显示它们即可。

我正在使用“光泽”生成图像,如下所示:

(plus_icon.png是一个30 x 30的图像,带有一个4 px宽的十字,在透明背景上以黑色占据了整个物体:就像在imageView 2和4中一样呈现:

-(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.
}

但是我的边框轮廓还没有破裂,但是如果我将其破裂,将会更新。

关于ios - 重新创建选定的UITabBarItem边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11138760/

相关文章:

objective-c - 有序集和核心数据 (NSOrderedSet)

iphone - 设置需要显示矩形 : paints a white rectangle only

ios - 发送异步请求 :queue:completionHandler: With Delegate Methods

ios - MW照片浏览器 : Show captions for every photo

iphone - CGAffineTransform 旋转 90 度

ios5 - MKPointAnnotation : title always visible. 可能吗?

ios - 如何将 CGPaths 持久化到 CoreData

iphone - 动画 CAShapeLayer 饼图

iphone - Xib Size - 似乎认为它比实际长

ios - 使用 writeJPEGRepresentation 函数保存带有深度数据的 CIImage