Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add camera layer on irregular shape images IOS

I need to add the camera layer on any irregular shaped image i.e. lets say i have a image which is having irregular shape and inside image there is a circular or any other irregular shape in which i want to embed the live camera.

Any idea how i can achieve this functionality?

like image 284
iAviator Avatar asked Apr 15 '17 12:04

iAviator


2 Answers

You can use UIBezierPath to draw irregular share for a mask CAShapeLayer

let size = 200.0

Create a CAShapeLayer and draw shape in which you wanna embed a cameraPreviewLayer.

let maskLayer = CAShapeLayer()
let maskPath = UIBezierPath()
maskPath.move(to: .zero)
maskPath.addLine(to: CGPoint(x: 10, y: -size))
maskPath.addLine(to: CGPoint(x: size/2, y: -size))
maskPath.addLine(to: CGPoint(x: size*2, y: size))
maskPath.close()
maskLayer.anchorPoint = .zero

Set the mask positon

maskLayer.position = CGPoint(x: 100, y: 400)
maskLayer.path = maskPath.cgPath

self.yourVideoPreviewLayer.mask = maskLayer
self.yourVideoPreviewLayer.masksToBounds = true

Or you can make an image with a shape in which you wanna embed a cameraPreviewLayer. Or if your image's inner shape have an alpha value = 0 you can reverse alpha of your original image and use it as a mask.

let maskLayer = CAShapeLayer()
maskLayer.anchorPoint = .zero
maskLayer.frame = videoPreviewLayer.bounds
maskLayer.contents = YourReversedImage.cgImage

self.videoPreviewLayer.mask = maskLayer
self.videoPreviewLayer.masksToBounds = true
like image 163
Vitaly Migunov Avatar answered Sep 22 '22 13:09

Vitaly Migunov


Add additional UIView upon of your UIImageView with same frames(width, height and position). it shouldn't be a subview of UIImageView!

Set background of this UIView to clearColor and create whatever layer you want.

Now you can use this layer as AVCaptureVideoPreviewLayer instead of using UIImageView layers

like image 24
Taras Chernyshenko Avatar answered Sep 23 '22 13:09

Taras Chernyshenko