Modified ScreenshotXC

Hi @Jeremie_L , you modified Jason’s Screenshot method last year for me and I was hoping you could update it to work on Mobilescreen, is this possible or do I need to stick with IOSView?

//Mod of Jason King’s ScreenShot function from iOSKit

Declare Function mainScreen Lib UIKitLib selector “mainScreen” (clsRef As ptr) As ptr

declare function bounds lib UIKitLib selector “bounds” (obj_id as Ptr) as CGRect64

dim sz as CGSize64 = bounds(aControl.handle).rsize

If not Retina then

declare sub UIGraphicsBeginImageContext lib UIKitLib (mSize as CGSize64)

UIGraphicsBeginImageContext(sz)

Else

declare sub UIGraphicsBeginImageContextWithOptions lib UIKitLib (mSize as CGSize64, opaque as boolean, scale as double)

UIGraphicsBeginImageContextWithOptions(sz, true, 0.0) //0.0 uses current screen scale
End If

dim viewRef as ptr = (aControl.handle)

declare function layer lib UIKitLib selector “layer” (obj_id as ptr) as ptr
declare sub renderInContext lib UIKitLib selector “renderInContext:” (obj_id as ptr, ctx as ptr)
declare function UIGraphicsGetCurrentContext lib UIKitLib () as ptr
renderInContext(layer(viewRef), UIGraphicsGetCurrentContext)

declare function UIGraphicsGetImageFromCurrentImageContext lib UIKitLib () as ptr
dim newUIImage as Ptr = UIGraphicsGetImageFromCurrentImageContext

declare sub UIGraphicsEndImageContext lib UIKitLib ()
UIGraphicsEndImageContext

Return iOSImage.FromHandle(newUIImage)

Hello Martin,

This should work:

Public Function ScreenShot(extends aView as MobileScreen, Retina as Boolean = False) As Picture
  Declare Function mainScreen Lib UIKitLib selector "mainScreen" (clsRef As ptr) As ptr
  
  declare function bounds lib UIKitLib selector "bounds" (obj_id as Ptr) as CGRect64
  
  
  
  dim sz as CGSize64 = bounds(mainScreen(NSClassFromString("UIScreen"))).rsize
  
  
  If not Retina then
    
    declare sub UIGraphicsBeginImageContext lib UIKitLib (mSize as CGSize64)
    
    UIGraphicsBeginImageContext(sz)
    
  Else
    
    
    declare sub UIGraphicsBeginImageContextWithOptions lib UIKitLib (mSize as CGSize64, opaque as boolean, scale as double)
    
    UIGraphicsBeginImageContextWithOptions(sz, true, 0.0) //0.0 uses current screen scale
  End If
  
  declare function view_ lib UIKitLib selector "view" (obj_id as ptr) as ptr
  dim viewRef as ptr = view_(aView.ViewControllerHandle)
  
  declare function window_ lib UIKitLib selector "window" (obj_id as ptr) as ptr
  dim windowRef as ptr = window_(viewRef)
  
  declare function layer lib UIKitLib selector "layer" (obj_id as ptr) as ptr
  declare sub renderInContext lib UIKitLib selector "renderInContext:" (obj_id as ptr, ctx as ptr)
  declare function UIGraphicsGetCurrentContext lib UIKitLib () as ptr
  renderInContext(layer(windowRef), UIGraphicsGetCurrentContext)
  
  declare function UIGraphicsGetImageFromCurrentImageContext lib UIKitLib () as ptr
  dim newUIImage as Ptr = UIGraphicsGetImageFromCurrentImageContext
  
  declare sub UIGraphicsEndImageContext lib UIKitLib ()
  UIGraphicsEndImageContext
  
  Return Picture.FromHandle(newUIImage)
  
End Function

Hi @Jeremie_L , thanks for the quick reply. The code you sent through is for a full screen shot but the last one you did was for an individual control, is this possible?

extends aControl as iOSControl, Retina as Boolean = True

Hi @Jeremie_L , I have continued to tinker with the method but can’t get it to show up as an autocomplete option.
MyScreen is defined as Mobilescreen
To call it MyScreen.Screenshot doesn’t appear??

I’ll just run with grabbing rects from your screenshot code

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.