AVCaptureVideoPreviewLayer add overlays and capture photo in iOS
You can load your overlay into a CIImage
, then use transformed(by matrix: CGAffineTransform)
to move it to the face position, and finally use composited(over dest: CIImage)
to blend it over the CIImage
from the video buffer.
You probably have to put in some work to transfer between the different coordinate spaces.
There are also a lot of more complex compositing filters available. Check out the filters in the CICategoryCompositeOperation
category.
How to customize the camera view and capture the part in the overlay?
Actually the answer from Nishant Bhindi needs some corrections. The code below will do the work:
func cropToBounds(image: UIImage) -> UIImage
{
let contextImage: UIImage = UIImage(cgImage: image.cgImage!)
let contextSize: CGSize = contextImage.size
let widthRatio = contextSize.height/UIScreen.main.bounds.size.height
let heightRatio = contextSize.width/UIScreen.main.bounds.size.width
let width = (self.imgOverlay?.frame.size.width)!*widthRatio
let height = (self.imgOverlay?.frame.size.height)!*heightRatio
let x = (contextSize.width/2) - width/2
let y = (contextSize.height/2) - height/2
let rect = CGRect(x: x, y: y, width: width, height: height)
let imageRef: CGImage = contextImage.cgImage!.cropping(to: rect)!
let image: UIImage = UIImage(cgImage: imageRef, scale: 0, orientation: image.imageOrientation)
return image
}
Still Image using AVCaptureDevice with overlay image
hi this will help you as it works for me
- (void)didTakePicture:(UIImage *)picture
{
UIImage *frm = self.overlayViewController.imgOverlay.image;
NSLog(@"width %f height %f",picture.size.width,picture.size.height);
UIGraphicsBeginImageContext(picture.size);
[picture drawAtPoint:CGPointMake(0,0)];
[frm drawInRect:CGRectMake(0, 0, picture.size.width, picture.size.height)];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSLog(@"view %@",viewImage.debugDescription);
[self.capturedImages addObject:viewImage];
}
Related Topics
How to Pass a Swift Object to JavaScript (Wkwebview/Swift)
Swift Swipe Navigation Table Views
Wkwebview Decidepolicyfornavigationaction Being Call After the Long Press Recogniser Ended
Pass Data from Tableview to Tab Bar View Controller in Swift
How to Get Camera Calibration Data on iOS? Aka Avcameracalibrationdata
Xcode Constraint Break When Switching Between Text Fields Without Dismissing Keyboard
Attempting to Segue from Objective-C to Swift Vc
Update Storyboard Constraint Using Code
Displaying Instances of View Controllers Within Xcode Without Disturbing the Current Hierarchy
Is Weak Self Needed for Table View Cell Button Closure
Running an iOS7 Project Under Xcode 6 - Image Assets Don't Show Up
How to Make Playground Execution Time Is as Fast as If We Run in iOS Application
Square Video Using Avfoundation
Animated View Floats into Place Weirdly Swiftui
Showing Hidden View Really Slow
The Array Value Should Be Sort Like (Alphabetic, Numbers and Special Characters)
Match the Data Type of a Object in Swift
Viewcontroller.Type Does Not Have a Member Names 'Answerone' - Swift