Crop screenshot and share? - ios

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.

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.

is it possible to set image size while taking screenshot of uiview?

in my app i am taking screenshot of uiview. but i want to set that image size. like size of uiview is 100*100 and i want to take that screenshot and set the image size to 50*50 so how can it possible?
here is my code:
func screenshot(vieww : UIView)-> UIImage {
UIGraphicsBeginImageContextWithOptions(vieww.frame.size, vieww.isOpaque, 0.0)
vieww.layer.render(in: UIGraphicsGetCurrentContext()!)
let img = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return img
}
Yes It's possible please try this
UIGraphicsBeginImageContext(CGSizeMake(320,480));
in place of
UIGraphicsBeginImageContextWithOptions(vieww.frame.size, vieww.isOpaque, 0.0)
Please let me know if it's work
Thank you

How to take a screenshot with the video picture in 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ļ¼

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 :)

Resources