【问题标题】:How to take a screenshot of the iPhone/iPad programmatically with Xamarin.iOS (C#)?如何使用 Xamarin.iOS (C#) 以编程方式截取 iPhone/iPad 的屏幕截图?
【发布时间】:2013-08-05 21:08:57
【问题描述】:

如何使用 Xamarin.iOS 截取屏幕截图并将此图像存储在 UIImage 中。

我看过几个 Obj-C 的例子,但没有 C#

【问题讨论】:

    标签: c# ios xamarin.ios screenshot xamarin


    【解决方案1】:

    更优雅:

    UIScreen.MainScreen.Capture ();
    

    【讨论】:

    【解决方案2】:

    来自Craig Dunn's网站:

    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");
       }
    }
    

    【讨论】:

      【解决方案3】:

      更优雅:

      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();
          }
      
      }
      

      调用 UIViewExtensions.TakeScreenshot() 以截取整个屏幕,或者您可以调用 AsImage() 到任何视图以获取视图的 UIImage 表示。最好将 TakeScreenshot() 方法放在其他地方,因为它不是 UIView 类的扩展。

      【讨论】:

        【解决方案4】:

        此代码将截取照片并将照片保存到相机胶卷中。

         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();
                }
        

        但要确保您的应用没有崩溃,请向用户请求将图像保存到相机胶卷的权限。 将此添加到 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".
        

        【讨论】:

          猜你喜欢
          • 2011-04-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多