iOS Question Capture panel to image in b4i

F.Campanella

Member
Licensed User
Longtime User
Hi,
the follow Sub capture panel to image in B4A

B4X:
'Capture Panel to imageview
Sub PanelCapture(pnl AsPanel, Img2 AsImageView)
Dim Obj1, Obj2 AsReflector
Dim bmp    AsBitmap
Dim c AsCanvas
Obj1.Target = Obj1.GetActivityBA
Obj1.Target = Obj1.GetField("vg")
bmp.InitializeMutable(pnl.left + pnl.Width, pnl.Top + pnl.Height)
c.Initialize2(bmp)

Dim args(1) As Object
Dim types(1) AsString
Obj2.Target = c
Obj2.Target = Obj2.GetField("canvas")
args(0) = Obj2.Target
types(0) = "android.graphics.Canvas"
Obj1.RunMethod4("draw", args, types)'draw from image to canavas
Dim canvas1 As Canvas
canvas1.Initialize(Img2)
Dim scrt As Rect
scrt.Initialize(pnl.left, pnl.top, pnl.left + pnl.Width, pnl.Top + pnl.Height)
Dim rectPanel1 As Rect
rectPanel1.Initialize(0, 0,Img2.Width, Img2.Height)
canvas1.DrawBitmap(bmp, scrt , rectPanel1)
Img2.Invalidate
End Sub

What is the appropriate procedure in B4i? Thank for your helps!!!!
 

F.Campanella

Member
Licensed User
Longtime User
I Erel, if I use TakeScreenshot, it capture entire screen. I would like to capture a specific panel.

I tried this:

B4X:
public  Sub PanelCapture(pnl As View, Img2 As ImageView)
   
   Dim no As NativeObject = Me
  Img2.Bitmap = no.RunMethod("imageWithView:", Array(pnl))

End Sub

#If OBJC
- (UIImage*) imageWithView:(UIView *)view
{
  [view drawViewHierarchyInRect:CGRectMake(0, 0, view.bounds.size.width, view.bounds.size.height) afterScreenUpdates:false];
  UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  return image;
}

#End If

but don't work... return the NSObject error not initialized.

How can I do?

Thanks!!!
 
Upvote 0

F.Campanella

Member
Licensed User
Longtime User
I Solved! I've used following code...

B4X:
#If OBJC

-(UIImage *) imageWithView:(UIView *)view
{
  UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0f);
  [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
  UIImage * snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  return snapshotImage;
}

#End If
 
Upvote 0
Top