How to take a screenshot with the video picture in iOS? - ios

AVPlayer is repeating play a small video in the middle of viewController.
the viewController have a bottomView and a topView, and I use the following method to capture a screenshot
func getScreenshotImage() -> UIImage {
UIGraphicsBeginImageContext(self.view.bounds.size)
self.view.drawViewHierarchyInRect(self.view.bounds, afterScreenUpdates: true)
let viewImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return viewImage
}
func getScreenshotImage() -> UIImage {
UIGraphicsBeginImageContext(window().bounds.size)
window().layer.renderInContext(UIGraphicsGetCurrentContext()!)
let viewImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return viewImage
}
These two methods have the same effect , they can capture the topView and bottomView , but the midpoints of the screen is black.
What can I do to capture the shot with a video screen just like the iPhone effect while pressing the power button and home button
Thanks in advanceļ¼

Related

How can i pass both x and y axis along with height and width of Screen to take screenshot programmatically in swift

I added a UIImageView on an ImageView, so i want to take screenshot of these two imageviews so that they look like one, any other recommendation is also appreciated.
I made a button that helped me take a screen shot, I have added x-axis and y axis,
func takeScreenshot(_ shouldSave: Bool = true) -> UIImage {
var screenshotImage :UIImage?
let layer = UIApplication.shared.keyWindow!.layer
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(CGSize(width: 20, height: 104), false, scale);
guard let context = UIGraphicsGetCurrentContext() else {return screenshotImage ?? UIImage(imageLiteralResourceName: "loading")}
layer.render(in:context)
screenshotImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
if let image = screenshotImage, shouldSave {
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}
return screenshotImage ?? UIImage(named: "loading")!
}
I expect that the screenshot taken, takes the screenshot of the imageview. Screenshot is attached,enter image description here
Put those two image views inside a UIView, constraint them all properly. Then take a screenshot of that UIView. Follow this for screenshot:
How to take screenshot of a UIView in swift?
I believe you're trying to say
You have 2 UIImageViews with different Images and then you want to take a screenshot of those 2 UIImageViews to make 1 image.
If that's the case, the easiest way to do solve it is to wrap those 2 UIImageViews inside a UIView. Then convert that UIView into UIImage. So you can have the Screenshot of those 2 UIImageViews in one.
In case you need code to convert UIView to UIImage:
extension UIView {
func toImage() -> UIImage {
let renderer = UIGraphicsImageRenderer(bounds: bounds)
return renderer.image { rendererContext in
layer.render(in: rendererContext.cgContext)
}
}
}
I'm not sure if this the solution to your problem.

How to save screenshot of a view with its subviews in Swift?

I want to capture a view with its subviews. I'm using this code to capture the screen:
let view = self.faceTrackerContainerView
UIGraphicsBeginImageContext(CGSizeMake(view.bounds.size.width, view.bounds.size.height))
UIGraphicsGetCurrentContext()!
self.view?.drawViewHierarchyInRect(view.frame, afterScreenUpdates: true)
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil)
These codes capture the whole screen including a button that's in front of the view. I tried these codes but it only captures the main view excluding its subviews:
let view = self.faceTrackerContainerView
let scale = UIScreen.mainScreen().scale
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, scale);
view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil)
I also tried the snapshotViewAfterScreenUpdates(true) from here but I don't know how to implement it. I have a collectionView below the view and a button on top of the view (both are not in the view). The first codes capture everything with the button, collectionView, and subviews. The second codes capture the view without the subviews. What am I missing?
This is working for me,
func screenShotMethod() {
//Create the UIImage
UIGraphicsBeginImageContext(faceTrackerContainerView.frame.size)
faceTrackerContainerView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
//Save it to the camera roll
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}
Ref
extension UIView {
func asImage() -> UIImage {
let renderer = UIGraphicsImageRenderer(bounds: bounds)
return renderer.image { rendererContext in
layer.render(in: rendererContext.cgContext)
}
}
}
It makes an UIImage from a UIView. Then you can use that image with UIImageView(image:) method
Why not just call snapshotView(afterScreenUpdates:)?
It isn't very elegant, but hiding views before the screenshot is taken works. You can then make them visible afterwards.
view.isHidden = true
//screenshot
view.isHidden = false
Add all subviews(that you want to include in screenshot) to one
parent view.
Draw parent view hierarchy like this:
let renderer = UIGraphicsImageRenderer(size: mapParentView.bounds.size)
let image = renderer.image { ctx in
mapParentView.drawHierarchy(in: mapParentView.bounds, afterScreenUpdates: true)
}

How do I take a screenshot of a single view object instead of the entire screen?

So I have a UIImageView with some text overlayed on the top and bottom. I want to get a screenshot so I have a new image with the text overlayed on it. This is the code I was working with but I am unable to take a screenshot of the my UIImageView object properly.
func generateImage() -> UIImage
{
// Render view to an image
UIGraphicsBeginImageContext(self.view.frame.size)
view.drawViewHierarchyInRect(self.imageView.frame,
afterScreenUpdates: true)
let memeImage: UIImage =
UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return memeImage
}
Alternatively, I have also tried the code below, though the image saved is the right size/area of the screen, it ends up with blurry version of what I took a screenshot of:
UIGraphicsBeginImageContext(self.imageView.frame.size)
let context = UIGraphicsGetCurrentContext()
imageView.layer.renderInContext(context!) // tried drawInContext but it didn't work at all
let memeImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return memeImage
First, using UIGraphicsBeginImageContextWithOptions will allow you to adjust the scale factor to suit your screen size. Second, passing in the view you want to capture will ensure that you are working with the correct view/subviews.
func generateImage(currentView: UIView) -> UIImage {
UIGraphicsBeginImageContextWithOptions(currentView.frame.size, true, 0.0)
currentView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let memeImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
//UIImageWriteToSavedPhotosAlbum(memeImage, nil, nil, nil)
return memeImage
}

SpriteKit screenshot of view off screen

I am creating a vertical scrolling game in SpriteKit and I would like to take a screenshot of the entire game scene once the game ends including the parts that have scrolled off screen. So far I am only able to capture the part of the screen that is visible using the following:
let bounds = self.scene!.view?.bounds
UIGraphicsBeginImageContextWithOptions(bounds!.size, true, UIScreen.mainScreen().scale)
self.scene?.view!.drawViewHierarchyInRect(bounds!, afterScreenUpdates: true)
screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
Is it possible to get the entire scene?
Thanks.
I was seeing your code and I don't see any problem at all; in fact I tested it (made it a function) and it works perfectly .
func getScreenshot(scene: SKScene) -> UIImage {
let bounds = self.scene!.view?.bounds
UIGraphicsBeginImageContextWithOptions(bounds!.size, true, UIScreen.mainScreen().scale)
self.scene?.view!.drawViewHierarchyInRect(bounds!, afterScreenUpdates: true)
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return screenshot;
}
I've tested it myself as it is and works; hope it helps :)

Crop screenshot and share?

I have done the coding for full screenshot without navigation bar but I also don't want the "Motivate me" button.
Also if anyone know how to share the crop screenshot to Facebook?
Here is the code for screenshot:
UIGraphicsBeginImageContextWithOptions(UIScreen.mainScreen().bounds.size,false,0);
self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Make sure that the motivate me button isn't inside the hierarchy of the view that you are taking the screenshot on. Move it onto a separate view and then use the same code that you already have. This will do exactly what you are after.
Or if you want to crop the whole photo you can use the following, please replace the value in heightOfButton to suit:
var heightOfButton: CGFloat = 100
var size = UIScreen.mainScreen().bounds.size
size.height -= heightOfButton
UIGraphicsBeginImageContextWithOptions(size,false,0);
self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Just hide the UIButton before you take the screenshot and unhide it after you take the screenshot.
func screenshot() {
// Hide button
myButton.alpha = 0.0
// Take screenshot
UIGraphicsBeginImageContextWithOptions(UIScreen.mainScreen().bounds.size,false,0)
self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
// Unhide button
myButton.alpha = 1.0
}
To share your image to Facebook you can use a SLComposeViewController.
On a side note, you don't need to use ; at the end of each line in Swift.

Resources