2013-08-05 7 views
5

मैं Xamarin.iOS के साथ एक स्क्रीनशॉट कैसे ले सकता हूं और इस छवि को UIImage में संग्रहीत कर सकता हूं।Xamarin.iOS (सी #) के साथ प्रोग्रामिक रूप से आईफोन/आईपैड का स्क्रीनशॉट कैसे लें?

मैं Obj सी के कई उदाहरण देखा है, लेकिन कोई सी #

उत्तर

14

इससे भी अधिक सुरुचिपूर्ण:

UIScreen.MainScreen.Capture(); 
+0

बहुत बढ़िया! मुझे यह नहीं पता था;) – rFlex

+0

यह बाइट [] क्या देता है? या छवि? –

+0

यूआईएममेज: https://developer.xamarin.com/api/member/UIKit.UIScreen.Capture()/ –

3

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

अधिक सुरुचिपूर्ण:

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() के लिए पूरी स्क्रीन का एक स्क्रीनशॉट लें या दृश्य के UIImage प्रतिनिधित्व प्राप्त करने के लिए आप किसी भी दृश्य में AsImage() को कॉल कर सकते हैं। TakeScreenshot() विधि को कहीं और रखना बेहतर होगा क्योंकि यह UIView क्लास का विस्तार नहीं है।

संबंधित मुद्दे