How to draw a rounded transparent rectangle over GLKView - ios

I'm working on a business card scanner application and I'm using AVCaptureSession and AVCaptureDevice for detecting edges of the card. I'd like to create a view on top of the preview layer which is rendered using GLKView and that view should have rounded rectangles and the outside of the rectangle should be blurred and the inside should be transparent.
Like this image:
How can I achieve this in Objective-C?
Thanks
Jugs

You can set background color of view to the UIColor with alpha
coverView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];

Related

Making UIView foreground transparent

I am modifying an IOS app to allow an additional background layer. All the existing overlays are UIImageViews, which handle transparency just fine.
The existing background is the main UIView. I want to change the black background to transparent, to put another view behind. It doesn't work. The simplest code which doesn't work as I would hope is as follows:
# import "TestUIView.h"
#implementation TestUIView
- (void)drawRect:(CGRect)rect {
self.opaque = NO;
[self setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 0,0,0,0);
CGContextFillRect(context, rect);
// and here goes my code where I draw to the transparent background
}
#end
When this is executed, it produces a black rectangle. I have set its background to transparent and filled its foreground with transparent. What do I have to do to get a UIView where I can draw against a transparent background?
(And I know I can change the alpha of the view as a whole, that's not the issue. I want opaque drawing on a truly transparent background).
UPDATE: Its not in the setbackgroundcolor, I appreciate it isn't necessary and so shouldn't be there, but the same thing happens if it is commented out. I can't draw against a transparent background; the code above shows I can't even make a transparent rectangle to draw onto.
Thanks
By default the value of opaque is NO. Also if you set the alpha to 0, then the view will be completely invisible to the user.If you want to have transparency, then you need to have an alpha value greater than 0 and less than 1.
Also the backgroundColor property is used to set the view’s color rather than drawing that color yourself.
If you need to change the background color in drawRect, you will have to draw it yourself:
self.backgroundColor=yourColorWithSomeAlphaValue;//
[self.backgroundColor setFill];
CGContextFillRect(UIGraphicsGetCurrentContext(), rect);
More explanation is provided in here:

Is it possible to add a transparent layer or view above a colored view?

I'm using AVFoundation framework to scan a barcode, but that may be unrelevant for my problem.
What I want:
I would like that the square bordered in green be transparent (not with the darkened black).
Here is what I have done:
I have 2 views: backgroundView( which occupies the whole screen) and highlightView which is the square bordered with green, on top of backgroundView (I have used a XIB for dimensions and positions) :
self.highlightView.layer.borderColor = [UIColor greenColor].CGColor;
self.highlightView.layer.borderWidth = 3;
// this following line does not allow the square to be transparent
self.highlightView.layer.opacity = 0;
// relative to AVFoundation
_previewLayer.frame = _backgroundView.bounds;
[_backgroundView.layer addSublayer:_previewLayer];
_previewLayer.backgroundColor = [UIColor blackColor].CGColor;
_previewLayer.opacity = 0.3;
UPDATE : xib (here representing the square with a clear color background), the backgroundView has the property black color background).
As I mentioned, you were looking in the wrong direction. There are multiple posts with a problem similar to yours, which have pretty decent answers. (You will have to study and understand to make the most of them):
Cut Out Shape with Animation
Simply mask a UIView with a rectangle
To sum it up, you need to apply the semi-transparent color to the layer of backgroundView and then play around with the layer's mask property to get the work done.
You can find many tutorials to learn using the layer and mask together.
Hope this helps.

How to remove gray color effect when touch move using objective c

I converted an image to fully gray when loaded, but I want to remove the gray color from it when touch move and view original image color.
I want to know how to convert from gray color effect to the original image and from original to gray when user moves finger over the image.
My solution is the following.
You need TWO UIImageViews. Add one on the other and make them to overlap pixel by pixel. You have two options:
B&W in foreground
Color in foreground
Both solutions fundamentally are the same. You need to use a mask on the topmost view. With a mask layer the white areas will be fully opaque, blacks are 100% transparent, grays are semi-transparent.
Example for the mask:
CALayer *maskLayer = [CALayer layer];
UIImage *mask = [UIImage imageNamed:#"mask.png"];
maskLayer.contents = (id)mask.CGImage;
maskLayer.bounds = (CGRect){CGPointZero, mask.size};
UIImageView *viewToMask = ;
viewToMask.image = [UIImage imageNamed:#"blackandwhite.png"];
viewToMask.layer.mask = maskLayer;
[self.view addSubview:viewToMask];
Certainly you have to add/remove the mask on touch/release. I leave you with that.
Now what happens when you move your finger? Just move the mask to different positions like so:
viewToMask.layer.mask.position = offset position based on touch position
I hope I could help you.

iOS opaque UIView with a transparent section

I'm trying to make a simple view that is opaque except for a circle of a given diameter in the center. It is meant to overlay the camera, as they don't want the entire screen showing, simply that you center your face in the circle and snap the picture.
It's a few simple lines of code to to the opposite - a clear view with an opaque circle in the center - but I cannot figure out how to do the opposite.
Any help or pointers appreciated....
It sounds like you just want a simple mask. You can do that using Ash's method.
UIImage *_maskingImage = [UIImage imageNamed:#"mask"];
CALayer *_maskingLayer = [CALayer layer];
_maskingLayer.frame = theView.bounds;
[_maskingLayer setContents:(id)[_maskingImage CGImage]];
[theView.layer setMask:_maskingLayer];
You'll need an image that is a circle that has a transparency gradient going from black on the edge to transparent on the center. Remember which file type you're using as not all support transparency (like jpeg).
Alternatively, you could just use a UIImageView and have an image that has the gradient in it instead.
What about using the mask property of the view layer ?
Try something like this (the 2 views should have the same size) :
#import <QuartzCore/QuartzCore.h>
UIView *blackCircleOnClearBackground;
UIView *cameraView;
cameraView.layer.mask = blackCircleOnClearBackground.layer;

Transparent UILabel textColor on superview.superview (sort of)

I would like to achieve the same effect as in the new iTunes remote update for iOS 7 for my UILabel.
If you look at this screen:
(The one on the right) you'll notice that the UILabel text color is the background blurred album cover without the black mask.
At the moment I have transparent UILabel textColor (http://cl.ly/SInK), my code is similar to https://github.com/robinsenior/RSMaskedLabel
My first assumption is to have a view hierarchy like that
UIImageView (Light blurred image)
|
UIImageView/UIView (Dark blurred image or just a dark mask for the superview)
|
UILabel (And all my other view).
I would like the UILabel to have a transparent text color on the first UIImageView, ignoring the second one/Mask).
I can't wrap my head around a solution to achieve this effect.
Since iOS 8 Apple offer a new class, UIVibrancyEffect, which can be added to a UIVisualEffectView. This combined with a UIBlurEffect can achieve the exact same result as my screenshot above.
Source: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIVibrancyEffect/index.html
I don't think you're going to be able to directly do this with a UILabel. What I did was decompose it into several pieces:
How to draw only the background of a label. For this I cribbed code from CGPathRef from string to turn the string into a path. Then the subclassing UILabel and adding the following drawRect did the appropriate thing.
-(void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
// Convert the attributed text to a UIBezierPath
CGPathRef path = [self.attributedText createPath];
// Adjust the path bounds horizontally as requested by the view
CGRect bounds = [self textRectForBounds:rect limitedToNumberOfLines:self.numberOfLines];
CGAffineTransform xform = CGAffineTransformMakeTranslation(bounds.origin.x, bounds.origin.y);
// Adjust the path bounds vertically because it doesn't seem to be taken into account yet
bounds = CGPathGetBoundingBox(path);
xform = CGAffineTransformTranslate(xform, 0., (self.bounds.size.height - bounds.size.height) / 2.);
// Apply the transform to the path
path = CGPathCreateCopyByTransformingPath(path, &xform);
// Set colors, the fill color should be the background color because we're going
// to draw everything BUT the text, the text will be left clear.
CGContextSetFillColorWithColor(ctx, self.textColor.CGColor);
// Flip and offset things
CGContextScaleCTM(ctx, 1., -1.);
CGContextTranslateCTM(ctx, 0., 0. - self.bounds.size.height);
// Invert the path
CGContextAddRect(ctx, self.bounds);
CGContextAddPath(ctx, path);
CGContextDrawPath(ctx, kCGPathEOFill);
// Discard the path
CFRelease(path);
// Restore gstate
CGContextRestoreGState(ctx);
}
If you create a UILabel in interface builder, set the class, set the background color to clear and the foreground color to your proposed fill color, the background will show through where the text is.
To blur the body showing through the label, I dropped an FXBlurView (https://github.com/nicklockwood/FXBlurView) under the label with sizing constraints to keep them aligned.
You can see it all working here https://github.com/dwb357/TransparentLabel.
The color is not uniform. But it's actually "cut out" to let the blurred and tinted background shine through.
One way to do it is to construct a CGPath for the glyphs, clip the graphics context to them, then clear the context, leaving the area outside black and the area inside fhe glyths translucent.
An alternative approach is to create a pattern color from this large blurred tinted cover art and use this pattern color on a UILabel.

Resources