[英]How to take a screenshot of the iPhone/iPad programmatically with Xamarin.iOS (C#)?
How can I take a screenshot with Xamarin.iOS and stored this image in UIImage. 如何使用Xamarin.iOS截取屏幕截图并将此图像存储在UIImage中。
I have seen several examples of Obj-C, but no C # 我见过几个Obj-C的例子,但没有C#
更优雅:
UIScreen.MainScreen.Capture ();
from Craig Dunn's site: 来自Craig Dunn的网站:
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");
}
}
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. 调用UIViewExtensions.TakeScreenshot()来获取整个屏幕的屏幕截图,或者可以将AsImage()调用到任何视图以获取视图的UIImage表示。 It would be better to put the TakeScreenshot() method somewhere else as it is not an extension of the UIView class.
最好将TakeScreenshot()方法放在其他地方,因为它不是UIView类的扩展。
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
将其添加到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".
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.