简体   繁体   English

如何使用Xamarin.iOS(C#)以编程方式截取iPhone / iPad?

[英]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.

相关问题 如何将此Swift功能转换为Xamarin.iOS / C# - How to translate this Swift func to Xamarin.iOS/C# 如何实现推送通知 firebase xamarin.ios c# - How implement Push Notifications firebase xamarin.ios c# 如何在Xamarin.iOS(C#)中预加载纹理图集(SKTextureAtlas)? - How to preload texture atlas (SKTextureAtlas) in Xamarin.iOS (in C#)? 如何禁用视频压缩-UIImagePickerController,Xamarin.IOS,C# - How to disable video compression - UIImagePickerController, Xamarin.IOS, C# iPhone移动应用程序(Xamarin.Ios / C#)中的圆形图像范围 - Circle Image Scope in Iphone Mobile Application ( Xamarin.Ios / C# ) 如何在Android / iOS移动测试-C#中截屏? - How to take screenshot in android/iOS mobile testing - C#? Xamarin.ios:从Objective-C到C# - Xamarin.ios: Objective-C to C# 如何在Xamarin.iOS应用程序中从iPhone播放铃声? - How to play a ringtone from the iPhone in my Xamarin.iOS application? C#Monotouch / Xamarin.iOS - 可滚动的UIView - C# Monotouch/Xamarin.iOS - Scrollable UIView C#Monotouch / Xamarin.iOS - 不支持推送导航控制器 - C# Monotouch/Xamarin.iOS - Pushing A Navigation Controller Is Not Supported
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM