How to take Snap shot of AGSMapView for ios? - ios

UIGraphicsBeginImageContextWithOptions(self.AgsMapView.bounds.size,false, 0.0)
self.view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
snapShot = UIGraphicsGetImageFromCurrentImageContext()
He is my code, from above code getting blank image (white)

Try this code!!
let layer = self.AgsMapView.layer
let scale = UIScreen.mainScreen().scale
UIGraphicsBeginImageContextWithOptions(self.AgsMapView.frame.size, false, scale);
layer.renderInContext(UIGraphicsGetCurrentContext()!)
let snapShot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil)

Related

Take a screenshot of an area covered by an UIImageView object

I am trying to take a screenshot of an area covered by a UIImageview object. I have overlaid the image with some UILabel objects and want to save the shot of just the area
I have tried
UIGraphicsBeginImageContext(self.view.frame.size)
view.drawHierarchy(in: self.view.frame, afterScreenUpdates: true)
let memedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
but it is giving my a shot of the whole screen
I have also tried
UIGraphicsBeginImageContext(imageHolder.frame.size)
view.drawHierarchy(in: self.view.frame, afterScreenUpdates: true)
let memedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
That is giving me a rectangle of the top half of the screen
If you add the UILabel as a subview to the UIImageView you can use an extension function like this to capture an image context of the image view and its subviews (the labels).
extension UIImageView {
func renderSubviewsToImage() -> UIImage? {
guard let image = image else { return nil }
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(image.size, false, scale)
guard let context = UIGraphicsGetCurrentContext() else { return nil }
self.layer.render(in: context)
let renderedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return renderedImage
}
}
view.drawHierachy actually accept the boundary as bounds rather than frame
i will assume that image is actually the UIImageView
var bounds= CGRect(x: -image.frame.minX,y: -image.frame.minY,width: view.bounds.size.width,height: view.bounds.size.height)
UIGraphicsBeginImageContext(image.frame.size)
view.drawHierarchy(in: bounds, afterScreenUpdates: true)
let outputImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()

screenshot is not in proper size IOS

In my IOS App Code I am taking screenshot of UIimageView but when I take it it's not taken properly by following code.
func captureView() -> UIImage {
// let rect: CGRect = self.imageView.bounds
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, false, 0.0)//add this line
let context: CGContextRef = UIGraphicsGetCurrentContext()!
self.view.layer.renderInContext(context)
let img: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img
}
I want the exect screen shot of the image in imageView because there are more images on it.
Kindly suggest me proper code for getting exact UIImageView Screenshot
How about you add UIScreen.mainScreen().scale instead of 0.0 in UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.mainScreen().scale)
func captureView() -> UIImage {
UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.mainScreen().scale)
drawViewHierarchyInRect(bounds, afterScreenUpdates: true)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img!
}

DrawView save combined image (multiply)

I have 2 UIImageViews - one is at the bottom and shows a default image (like an Photo) - on the second UIImageView where you can draw.
I would like to create an UIImage from both images, and save it as new image.
How can i do that? Ill tried with:
func saveImage() {
print("save combined image")
let topImage = self.canvasView.image
let bottomImage = self.backgroundImageView.image
let size = CGSizeMake(topImage!.size.width, topImage!.size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
[topImage!.drawInRect(CGRectMake(0,0,size.width, topImage!.size.height))];
[bottomImage!.drawInRect(CGRectMake(0,0,size.width, bottomImage!.size.height))];
let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIImageWriteToSavedPhotosAlbum(newImage, self, #selector(CanvasViewController.image(_:didFinishSavingWithError:contextInfo:)), nil)
}
But the result is not correct (stretched and no overlay)
Any ideas?
Ok ill found an solution for that, just need to add "multiply" as blend mode.
let topImage = self.canvasView.image
let bottomImage = self.backgroundImageView.image
let size = CGSizeMake(bottomImage!.size.width, bottomImage!.size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
[bottomImage!.drawInRect(CGRectMake(0,0,size.width, bottomImage!.size.height))];
[topImage!.drawInRect(CGRectMake(0,0,size.width, bottomImage!.size.height), blendMode: CGBlendMode.Multiply , alpha: 1.0)];
let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

Why does the screenshot show just white when I'm trying to take a screenshot of my app programmatically in Swift?

On my app, I have a view which holds a camera. I want to take a screenshot of this view which holds the camera. However when I do this with the following code:
let layer = UIApplication.sharedApplication().keyWindow!.layer
let scale = UIScreen.mainScreen().scale
UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);
layer.renderInContext(UIGraphicsGetCurrentContext()!)
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil)
the screenshot which was saved to photos is just blank and doesn't show the camera view.
Render and capture an UIView named view:
UIGraphicsBeginImageContextWithOptions(view.frame.size, false, 0.0)
view.drawViewHierarchyInRect(view.frame, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
Remember that UIWindow is a view too.
You can test in the simulator saving to the desktop with this function:
public func saveImageToDesktop(image : UIImage, name : String)
{
let basePath = NSString(string: "~/Desktop").stringByExpandingTildeInPath
let path = "\(basePath)/\(name).png"
UIImagePNGRepresentation(image)?.writeToFile(path, atomically: true)
}

Retina screenshot (Swift)

I am trying to take a screenshot of the UIView using the code below but it is creating a non-retina screenshot at half of the sizes of the actual device screen size.
Where am I going wrong?
Also view.frame.size is returning half of the actual values.
let scale = UIScreen.mainScreen().scale
UIGraphicsBeginImageContextWithOptions(view.frame.size, false, scale)
view.layer.renderInContext(UIGraphicsGetCurrentContext())
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil)
Thanks

Resources