Disable preview layer when recording on camera - ios

I am using https://github.com/chroman/HeartBeats to draw heartbeat's linechart. But, i want to remove preview layer (or disable it) and only display "linechart" on my own UIView, but i dont know where to edit.
im a beginer at iOS-dev, pls cmt if you know. thank you.
edit 1: i think CALayer is my issue. (on viewDidLoad):
imageLayer = [CALayer layer];
imageLayer.frame = self.view.layer.bounds;
imageLayer.contentsGravity = kCAGravityResizeAspectFill;
[self.view.layer addSublayer:imageLayer];
[self setupAVCapture];
edit 2: note: red backgroud is your finger when place on it. if not, it will show what camera currently recording.

try to set background color as clear color or black color to your layer something like,
imageLayer = [CALayer layer];
imageLayer.backgroundColor = [UIColor clearColor].CGColor;
or
imageLayer.backgroundColor = [UIColor blackColor].CGColor;

Related

CALAYER apply background color plus pattern image

I am having fun with Core Animation stuffs.
I needed to apply a pattern image to a CALAyer.
That part work perfectly with something like that:
color = UIColor(patternImage: UIImage(named: "myPatternImage")!
shapeLayer.fillColor = color.CGColor;
But now I need to apply a background color behind the pattern. my Patter image is a png (with alpha component).
So I want to apply a simple Color to the shape and after apply the pattern Image.
Is there a way to do that on the fly?
Just add another sublayer with background color and add it before your pattern background layer:
UIColor *backgroundColor = [UIColor redColor];
CALayer *solidBackgroundColorLayer = [[CALayer alloc] init];
solidBackgroundColorLayer.backgroundColor = backgroundColor.CGColor;
[[self layer] addSublayer:solidBackgroundColorLayer];
UIColor *color = [UIColor colorWithPatternImage:[UIImage imageNamed:#"MyPattern.png"]];
backgroundLayer = [[CALayer alloc] init];
[backgroundLayer setBackgroundColor:[color CGColor]];
[[self layer] addSublayer:backgroundLayer];

CALayer - How do I create an image with a solid background and transparent text?

I'm trying to create movie generation application by using AVComposition and have a trouble in making title frame.
Each frame is actually a calayer and title layer is on top of other frames.
Title(Text) needs to be transparent with black background so that they can see some part of the first content frame under title text letters.
I searched most articles about calayer mask, but nothing helped me.
I thought this article (How to make only the part covered by text/title transparent in a UIView in IOS) is helpful and coded like Dave's way, but got only white screen.
Here is what I have done:
// create UILabel from the title text
CGRect rectFrame = CGRectMake(0, 0, videoSize.width, videoSize.height);
UILabel *lbTitle = [[UILabel alloc] initWithFrame:rectFrame];
lbTitle.text = self.titleText;
lbTitle.font = [UIFont fontWithName:#"Helvetica" size:60];
lbTitle.textColor = [UIColor blackColor];
lbTitle.backgroundColor = [UIColor whiteColor];
// get title image and create mask layer
UIGraphicsBeginImageContextWithOptions(lbTitle.bounds.size, TRUE, [[UIScreen mainScreen] scale]);
[lbTitle.layer renderInContext:UIGraphicsGetCurrentContext()];
CGImageRef viewImage = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
UIGraphicsEndImageContext();
CALayer *maskLayer = [CALayer layer];
maskLayer.contents = (__bridge id)viewImage;
maskLayer.frame = rectFrame;
// create title background layer and set mastLayer as mast layer of this layer
// this layer corresponds to "UIView's layer" in Dave's method
CALayer *animatedTitleLayer = [CALayer layer];
animatedTitleLayer.backgroundColor = [UIColor whiteColor].CGColor;
animatedTitleLayer.mask = maskLayer;
animatedTitleLayer.frame = rectFrame;
...
[view.layer addSubLayer:animatedTitleLayer];
Here I used animatedTitleLayer as title background(black background), but what I see is white screen.
Anyone can help me? Thanks in advance.
The mask uses the alpha channel to determine what parts to mask out and what parts to keep. However, your label that you render into an image is rendered as black text on a white background so there is no transparency in the image.
You have also specified that the graphics context you are using to render the image is opaque so even if the background color of the label as was clear you would get an opaque image.
So you need to set a clear background color on the label and pass NO as the second argument when you create the graphics context.

iOS: Rotate an image, but not a mask. How to make this?

I make two images: one with my spinner image and another with mask image (partially transparent gradient). Imagine this: spinner image is rotating clockwise, in the bottom there is dark gradient (look like haze), rotated spinner partially hided with this gradient. That is my aim.
I add two UIImageView to my scene, but my mask image also influence to background colour and I don't want that. I want that my mask image masking only spinner, so I make this:
- (void)viewDidLoad
{
[super viewDidLoad];
//add mask to UIImageView
CALayer *mask = [CALayer layer];
mask.contents = (id)[[UIImage imageNamed:#"mask.png"] CGImage];
mask.frame = CGRectMake(0, 0, self.spinner.bounds.size.width,self.spinner.bounds.size.height);
self.spinner.layer.mask = mask;
self.spinner.layer.masksToBounds = YES;
//rotate UIImageView (trouble with mask, it must not be rotated)
CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:#"transform.rotation.z"];
animation.fromValue = [NSNumber numberWithFloat:0.0f];
animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
animation.duration = 20.0f;
animation.repeatCount = INFINITY;
[self.spinner.layer addAnimation:animation forKey:#"Spinner"];
}
As a result I got I spinner with nice mask image, but my mask rotates with my spinner, I want to always clip my mask to bottom of my spinner UIImageView. How can I do this? Or my conception is wrong, if so, give me some directions how can I archive what I want. Feel free to ask. Thanks.
Was tricky for me, but I made it.
Here is what you need to make this: you need a UIView (as a container for UIImageView).
Change this part in source code that I posted below:
CALayer *mask = [CALayer layer];
mask.contents = (id)[[UIImage imageNamed:#"mask.png"] CGImage];
mask.frame = self.yourContainerView.bounds;
self.yourContainerView.layer.mask = mask;
self.yourContainerView.layer.masksToBounds = YES;
That's all. Hope this will help somebody.

iOS - Creating a blurred layer using CALayer

I created a CALayer with white color and 0.4 of opacity. What I want to is to make this layer above an image and make it blur, like the notification layer is blurring on the app or the home screen you're opening here's my CALayer:
CALayer *lyr = [CALayer layer];
lyr.bounds = CGRectMake(0, 0, 190, 190);
lyr.position = CGPointMake(90, 50);
lyr.backgroundcolor = [UIColor whiteColor].CGColor;
lyr.opacity = 0.4f;
[self.view.layer addSubLayer:lyr];
Sorry if I didn't explained what I need exactly
Thanks in Advance
Take a look at FXBlurView
https://github.com/nicklockwood/FXBlurView
It also provides a method to apply a tinted blur on an image.
- (UIImage *)blurredImageWithRadius:(CGFloat)radius iterations:(NSUInteger)iterations tintColor:(UIColor *)tintColor;
-> Import UIImage+FXBlurImage.h

add shadow to a layer

I can add shadow to imageView layer using the following code.
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"test.png"]];
self.imageView.center = self.view.center;
CALayer *containerLayer= [CALayer layer];
containerLayer.shadowColor = [UIColor blackColor].CGColor;
containerLayer.shadowRadius = 10.0f;
containerLayer.shadowOffset = CGSizeMake(10.0f, 5.0f);
containerLayer.shadowOpacity = .8f;
[containerLayer addSublayer:self.imageView.layer];
[self.view.layer addSublayer:containerLayer];
1 . The problem is that I don't know why I have to add imageView.layer to containerLayer to get the imageView shadow effect. However, if I add containerLayer to imageView.layer, there's no shadow in imageView, why?
The error code is:
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"test.png"]];
self.imageView.center = self.view.center;
CALayer *containerLayer= [CALayer layer];
/*same as before*/
[self.imageView.layer addSublayer:containerLayer];
[self.view.layer addSublayer:self.imageView.layer];
Question2: the containerLayer's(used to provide shadow to imageView) frame = {{0, 0}, {0, 0}}, but the final position is in the center of screen. why?
A layer needs something opaque inside it to create a shadow around (unless you've specified a shadowPath explicitly). So your first version of the code works because the containerLayer has the imageView's layer as a sublayer. But, as you noticed from Question #2, the containerLayer's frame indicates that it is actually located in the upper left corner with a size of (0,0). The reason you can still see the image is that containerLayer is not masking to its bounds. Add this line to your first version, and the image disappears:
[containerLayer setMasksToBounds: YES]; // kitten (and shadow) is gone
Your version #2 of the code does not display a shadow because, in part, the containerLayer does not "contain" anything. If you use version #2 but give the containerLayer a new frame and an opaque background color, a shadow appears. (But this obviously isn't a solution, because the image is covered up...) Also note that there is no shadow when the layer's background is [UIColor clearColor].
[self.imageView.layer addSublayer:containerLayer];
containerLayer.frame = self.imageView.layer.bounds;
containerLayer.backgroundColor = [UIColor yellowColor].CGColor; // yellow box w/shadow
// containerLayer.backgroundColor = [UIColor clearColor].CGColor; // no shadow here
If you wanted to have a container with a shadow that houses the UIImageView, you could do something like this:
UIView * shadowView = [[UIView alloc] initWithFrame: self.imageView.frame];
shadowView.layer.shadowColor = [UIColor blackColor].CGColor;
shadowView.layer.shadowRadius = 10.0f;
shadowView.layer.shadowOffset = CGSizeMake(10.0f, 5.0f);
shadowView.layer.shadowOpacity = .8f;
[self.view addSubview: shadowView];
self.imageView.frame = (CGRect) { CGPointZero, self.imageView.frame.size };
[shadowView addSubview: self.imageView];
A CALayer could be used instead of a UIView in a similar way. Or you could apply shadow attributes to the imageView's layer directly, making sure that neither the view nor the layer is clipping/masking to bounds.
If you want to add a shadow over the imageview just change the alpha value (0.0 min - 1.0 max) of that imageview. This will give you the shadow effect and whenever you want to remove the shadow just restore the alpha value to 1.0.
For ex:
self.imageview.alpha = 0.5 // shadow state
self.imageview.alpha = 1.0 // original state

Resources