Forum Xamarin.iOS

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Screanshot

alipishkarialipishkari USMember ✭✭

Hi, I want to make an screanshot in IOS. not from my app,
I want to minimize my app and then make an screanshot.
Please help me.

Tagged:

Answers

  • LucasZhangLucasZhang Member, Xamarin Team Xamurai

    We could not capture the screenshot of HomePage in code manually because of apple policy . But we could get the image when user click the lock button and volume + button .

    in AppDelegate

    Firstly, you need to add the permission string in the info.plist:

    <key>NSPhotoLibraryUsageDescription</key>
    <string> photos description. </string>
    

    Then request the dynamic authorization in the method FinishedLaunching like:

    PHPhotoLibrary.RequestAuthorization((status) =>
    {
       switch (status)
       {
           case PHAuthorizationStatus.Authorized:
               {
                   Debug.WriteLine(“Authorized to access”);
                   break;
               }
           case PHAuthorizationStatus.Denied: case PHAuthorizationStatus.Restricted:
               {
                   Debug.WriteLine(“Not allowed”);
                   break;
               }
           default:
               {
                   Debug.WriteLine(“Not determined”);
                   break;
               }
       }
    });
    

    Add the notification in the method FinishedLaunching so that the app will get the notification when user capture the screen shot .

    using ObjCRuntime;
    using AssetsLibrary;
    using Photos;
    
     NSNotificationCenter.DefaultCenter.AddObserver(this,new Selector("IsScreenShot:"),UIApplication.UserDidTakeScreenshotNotification,null);
    NSNotificationCenter.DefaultCenter.AddObserver(this, new Selector("GetScreenShot:"), UIApplication.UserDidTakeScreenshotNotification, null);
    
    
            bool isScreenShot;  
    
            [Export("IsScreenShot:")]
            void IsScreenShot(NSNotification notification)
            {
                ALAuthorizationStatus kStatus = ALAssetsLibrary.AuthorizationStatus;
                if (kStatus == ALAuthorizationStatus.Authorized)
                {
                    isScreenShot = true;
                }
            }
    
            //get screenshot (the first photo in album)
            [Export("GetScreenShot:")]
            void GetScreenShot(NSNotification notification)
            {
                if(isScreenShot)
                {
                    PHImageRequestOptions options = new PHImageRequestOptions();
                    options.ResizeMode = PHImageRequestOptionsResizeMode.Fast;
                    options.DeliveryMode = PHImageRequestOptionsDeliveryMode.FastFormat;
                    options.Synchronous = true;
    
                    var assets = PHAsset.FetchAssets(PHAssetMediaType.Image, null);
                    PHImageManager manager = new PHImageManager();
    
                    var LastAsset = assets[0] as PHAsset;
                    manager.RequestImageForAsset(LastAsset, PHImageManager.MaximumSize, PHImageContentMode.Default, options,
                                                   (image, info) =>
                                                   {
                                                         //image is the screen shot , do something you want 
                                                   });
                }
    
                isScreenShot = false;
    
            }
    
Sign In or Register to comment.