10

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?

iAviator
  • 1,310
  • 13
  • 31
  • Don't know why the question is downvoted? Can the person who did down vote explain this? – iAviator Jul 27 '17 at 10:31
  • I downvoted this question because I can't vote to close it. Yeah I think you are asking for implementation of feature and didn't even show how you tried to solve it by yourself. – sage444 Jul 27 '17 at 10:40
  • I have just asked for the idea/ways to implement if you can again read my question. There is nowhere written for the implementation. So it doesn't make sense to downvote the question. – iAviator Jul 27 '17 at 11:14
  • Did you try to add AVCaptureVideoPreviewLayer as circular or any other irregular shape? – Taras Chernyshenko Jul 27 '17 at 11:39
  • @TarasChernyshenko circular is possible but not any irregular shape. – iAviator Jul 27 '17 at 12:52
  • @iAviator What exactly issues do you have with irregular shapes? – Taras Chernyshenko Jul 28 '17 at 08:15
  • @TarasChernyshenko Im able to set the camera layer mask property with the image mask. But the issue now i face is the camera layer is shown in the image where there are colors in the image but i need to show the camera layer in the transparent area of the image. Any idea? – iAviator Jul 29 '17 at 12:30
  • Im able to put the videoPreviewlayer in image transparent area. Now the problem i face is to save the image with the preview layer merged in image.? Any ideas on this guys? – iAviator Aug 01 '17 at 12:43

2 Answers2

4

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
Vitaly Migunov
  • 4,297
  • 2
  • 19
  • 23
  • I have not used any path for the layer. And as answered above in comments i was able to achieve the layer mask through imageview lask property. Now the issue is when i mask the image of camera and the mask image the code returns the original image. I referred at https://www.innofied.com/implementing-image-masking-in-ios/ – iAviator Aug 03 '17 at 06:16
  • Not sure what you mean by "Now the issue is when i mask the image of camera and the mask image the code returns the original image." Can you provide a screenshot? – Vitaly Migunov Aug 03 '17 at 12:18
1

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

Taras Chernyshenko
  • 2,729
  • 14
  • 27