Reputation: 217
How can I take a screenshot with Xamarin.iOS and stored this image in UIImage.
I have seen several examples of Obj-C, but no C #
Upvotes: 5
Views: 3889
Reputation: 395
This code will take a screenshot and save the photo into the camera roll.
UIGraphics.BeginImageContext(UIScreen.MainScreen.Bounds.Size);
try
{
var mainLayer = UIApplication.SharedApplication.KeyWindow.Subviews[0].Layer;
mainLayer.RenderInContext(UIGraphics.GetCurrentContext());
var img = UIGraphics.GetImageFromCurrentImageContext();
img.SaveToPhotosAlbum((iRef, status) =>
{
if (status != null)
{
new UIAlertView("Problem", status.ToString(), null, "OK", null).Show();
}
else
{
new UIAlertView("Saved", "Saved", null, "OK", null).Show();
}
});
}
finally
{
UIGraphics.EndImageContext();
}
But to make sure your app didn't crash is to ask the user for the permission to save the images to the camera roll. Add this into info.plist
<key>NSPhotoLibraryAddUsageDescription</key>
<string>This app requires read and write permission from the user.</string>
if you're adding it from the generic editor then "Privacy - Photo Library Additions Usage Description" will be the given option you will find out instead of "NSPhotoLibraryAddUsageDescription".
Upvotes: 0
Reputation: 460
More elegant:
public static class UIViewExtensions {
public static UIImage AsImage(this UIView view) {
UIGraphics.BeginImageContextWithOptions(view.Bounds.Size, view.Opaque, 0);
view.Layer.RenderInContext(UIGraphics.GetCurrentContext());
UIImage img = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
return img;
}
public static UIImage TakeScreenshot() {
return UIApplication.SharedApplication.KeyWindow.AsImage();
}
}
Call UIViewExtensions.TakeScreenshot() to take a screenshot of the whole screen or you can call AsImage() to any view to get an UIImage representation of the view. It would be better to put the TakeScreenshot() method somewhere else as it is not an extension of the UIView class.
Upvotes: 3
Reputation: 89179
from Craig Dunn's site:
public void ScreenCapture()
{
var documentsDirectory = Environment.GetFolderPath
(Environment.SpecialFolder.Personal);
Console.WriteLine("start capture of frame: " + this.View.Frame.Size);
UIGraphics.BeginImageContext(View.Frame.Size);
var ctx = UIGraphics.GetCurrentContext();
if (ctx != null)
{
View.Layer.RenderInContext(ctx);
UIImage img = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
// Set to display in a UIImage control _on_ the view
imageLogo.Image = img;
// Save to Photos
img.SaveToPhotosAlbum(
(sender, args)=>{Console.WriteLine("image saved to Photos");}
);
// Save to application's Documents folder
string png = Path.Combine (documentsDirectory, "Screenshot.png");
// HACK: overwrite the splash screen. iSOFlair is the application name
//string png = Path.Combine (documentsDirectory, "../iSOFlair.app/Default.png");
NSData imgData = img.AsPNG();
NSError err = null;
if (imgData.Save(png, false, out err))
{
Console.WriteLine("saved as " + png);
} else {
Console.WriteLine("NOT saved as" + png +
" because" + err.LocalizedDescription);
}
}
else
{
Console.WriteLine("ctx null - doesn't seem to happen");
}
}
Upvotes: 3