CALayer border get fuzzy on rotation - iphone

I am using
UIColor* clear = [UIColor blackColor];
[self.layer setBorderColor:[color CGColor]];
to set the border for an image.It is adding border but when i tried rotating that image to 45 degree,90 degree so on ,that border starts getting fuzzy and its not fixed or straight anymore. How can image that border fix like a line always whatever i do..

UIColor* clear = [UIColor blackColor]; [self.layer setBorderColor:[color CGColor]];
looks like something weird ;\ what is self here?
try to
UIImageView *imgView = [[UIImageView alloc] setImage:[UIImage imageNamed:#"pic.png"]];
[self.view addSubview:imgView];
imgView.layer.borderWidth = 2.0f;
imgView.layer.borderColor = [[UIColor blackColor] CGColor];
And don't forget to include QuartzCore framework and #import <QuartzCore/QuartzCore.h>

Related

optimizing rounded corners performance

I have the following code in my UITableViewCell:
[self.layer setBorderColor:[UIColor blackColor].CGColor];
[self.layer setShadowRadius:10.0];
[self.layer setCornerRadius:5.0];
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(5.0, 5.0)];
[self.layer setShadowPath:path.CGPath];
[self.layer setShouldRasterize:YES];
[self.layer setRasterizationScale:[UIScreen mainScreen].scale];
when I run instrument and set color offscreen - rendered yellow, this causes the cell to be yellow. When I remove the shouldRasterize it doesn't shade the cells to yellow. What are ways to improve this?
This is greatly hurting my scrolling performance. I am just trying to set rounded corners with some shadows in it.
I'm doing rounded corners like this:
self.layer.shadowColor = [UIColor grayColor].CGColor;
self.layer.shadowOffset = CGSizeMake(0.05, 0.05);
self.layer.shadowOpacity = 10;
self.layer.shadowRadius = 1.5;
self.layer.masksToBounds = NO;
self.layer.shouldRasterize = YES;
[self.layer setBorderColor: [[UIColor whiteColor] CGColor]];
[self.layer setBorderWidth: 5.0];

UINavigationBar with two rounded corners

I have seen this answer
https://stackoverflow.com/a/9243472/563381
And while it works well visual as soon as you set the mask on the Navigation bar's layer it no longer responds to touches... So the back button that appears on the bar can't be clicked. Any solution to cause touches to go through a CALAyer? I didnt think CALayer's blocked touches or that a mask would ever block touches.
Well, I really don't know why CALayer blocks touches and this sounds odd to me...
The way I round corners of UINavigationBar consists in putting 2 UIImageView (10x10 pixels) in the corners and add 2 images to them. These images work as a mask, without blocking touches. If you use antialiasing to draw your images, the look is perfect.
You should try to use this code:
self.navigationController.navigationBar.translucent = YES;
This wil turn on your back button.You can see your button, but its in another layer. That's why it will not work the touches..
UPDATE:
Use this line of code for test. This will work like a charm for you.
//Style UINavigationBar
UIView *background = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
background.backgroundColor = [UIColor blackColor];
[self.view addSubview:background];
self.navigationController.navigationBar.tintColor = [UIColor cyanColor];
self.navigationController.navigationBar.translucent = YES;
CALayer *capa = [self.navigationController navigationBar].layer;
[capa setShadowColor: [[UIColor blackColor] CGColor]];
[capa setShadowOpacity:0.85f];
[capa setShadowOffset: CGSizeMake(0.0f, 1.5f)];
[capa setShadowRadius:2.0f];
[capa setShouldRasterize:YES];
//Round
CGRect bounds = capa.bounds;
bounds.size.height += 10.0f; //I'm reserving enough room for the shadow
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds
byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)
cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = bounds;
maskLayer.path = maskPath.CGPath;
[capa addSublayer:maskLayer];
capa.mask = maskLayer;
//Back Btn
UIButton *btnback = [UIButton buttonWithType:UIButtonTypeCustom];
[btnback setFrame:CGRectMake(0, 0, 54, 29)];
[btnback setBackgroundImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
UILabel * btnlabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 40, 23)];
btnlabel.backgroundColor = [UIColor clearColor];
btnlabel.textColor = [UIColor whiteColor];
btnlabel.font = [UIFont boldSystemFontOfSize:13];
btnlabel.text = #"back";
[btnback addSubview:btnlabel];
[btnback addTarget:self action:#selector(backHome:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btnback];

Opacity of layer's shadow

I add shadow to the UIView:
[blurView.layer setCornerRadius:kCornerRadius];
blurView.layer.masksToBounds = NO;
blurView.layer.shadowColor = [[UIColor redColor] CGColor];
blurView.layer.backgroundColor = blurView.layer.shadowColor;
blurView.layer.shadowPath = [UIBezierPath
bezierPathWithRoundedRect:CGRectMake(-2.0*kCornerRadius, -2.0*kCornerRadius, blurView.bounds.size.width+4.0*kCornerRadius, blurView.bounds.size.height+4.0*kCornerRadius)
cornerRadius:kCornerRadius].CGPath;
blurView.layer.cornerRadius = kCornerRadius;
blurView.layer.shadowOffset = CGSizeMake(0.0, 0.0);
blurView.layer.shadowRadius = kCornerRadius;
blurView.layer.shadowOpacity = kOpacity;
blurView.layer.opacity = 1.0;
This part of code gives me a this view, like a uiview is blured:
When I try change opacity of blurView.layer (for example to 0.5), I get unexpected view:
As you can see - I get a sharp edges of uiview.
I think, shadow opacity is a part of opacity of layer - it's a cause of this 'error'. Can anybody help me to fix this? May be can I merge layers before change opacity of blurView.layer or something like this?
UPD
With this fix:
blurView.layer.shadowColor = [[UIColor colorWithRed:1 green:0 blue:0 alpha:0.5] CGColor];
I get this:
UPD
Answer is using setShouldRasterize method:
Thanks everybody!
[blurView.layer setShouldRasterize:YES];
The answer is using this code line:
blurView.layer.shouldRasterize = YES;
Maybe you could try putting this in?
blurView.layer.shadowColor = [[UIColor colorWithRed:1 green:0 blue:0 alpha:0.5] CGColor];
What happens if you put this :
blurView.layer.shadowOpacity = 1.0;
blurView.layer.opacity = 0.5;
I think that you might be setting the shadow opacity to 0.5 and then the whole layer to 0.5, making the shadow twice as transparent?
(or I'm completely wrong!)

border in UIImageView

I am trying to have an UIImageView bordered like the following:
I have tried using:
[imageView.layer setBorderColor: [[UIColor whiteColor] CGColor]];
[imageView.layer setBorderWidth: 2.0];
but then you can't see that gray border on the outside. It has that gray shadow effect on the outside.
How do I do this?
Take a look at the shadow properties of CALayer.
[imageView.layer setShadowOffset:CGSizeMake(-1.0, -1.0)];
[imageView.layer setShadowOpacity:0.5];
This question about adding shadows to UIImageView might help
imageView.layer.shadowOpacity=0.6;
imageView.layer.shadowRadius = 0.0;
imageView.layer.shadowColor = [UIColor grayColor].CGColor;
imageView.layer.shadowOffset = CGSizeMake(-2.0, 1.0);

Need help in adding a drop shadow to an image in a uiview in iOS4?

I am trying to create a drop shadow for an image. I also have an animation between views and this is the backdrop. However, when I use the following code, the image is not drawn. Anyone have any ideas?
self.frontViewBackground = [[[UIView alloc] initWithFrame:frame] autorelease];
//self.frontViewBackground.image = [UIImage imageNamed:#"whitepaper3.png"];
self.frontViewBackground.multipleTouchEnabled = YES;
[self.photoView addSubview:self.frontViewBackground];
UIImage *image = [UIImage imageNamed:#"whitepaper3.png"];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetShadow(ctx, CGSizeMake(1, -2), 3.0);
[image drawInRect:self.frontViewBackground.frame blendMode:kCGBlendModeNormal alpha:1.0];
CGContextRestoreGState(ctx);
Have you thought about using CALayer's shadow properties? For example you could do the following:
UIView *view = [[UIView alloc] initWithFrame:frame];
view.layer.shadowColor = [[UIColor blackColor] CGColor];
view.layer.shadowRadius = 5.0
Check out more here: http://developer.apple.com/iphone/library/documentation/GraphicsImaging/Reference/CALayer_class/Introduction/Introduction.html#//apple_ref/occ/instp/CALayer/shadowColor