0

I'm on Xamarin, iOS, and am messing with UIImage and CGImage. The docs don't quite state what is going on with how exactly a UIImage is created, when you pass in a CGImage. It doesn't state if a copy is made of the CGImage, or if it merely uses the CGImage ptr.

So I'm curious about this:

        IntPtr p1;
        IntPtr p2;
        
        using (var image = tempContext.CreateCGImage(ciImage, ciImage.Extent))
        {
            m_uiImage = new UIImage(image);
            CGImage cgi = m_uiImage.CGImage;
            p1 = m_uiImage.CGImage.GetHandle();
            p2 = image.GetHandle();
        }

        IntPtr p3 = m_uiImage.CGImage.GetHandle();

I don't know Xamarin/C# that well, but I think Dispose() is called on 'image' inside the using clause. if UIImage was in fact created from simply setting a pointer from image, then after the using statement exits, you'd think m_uiImage's CGImage would be disposed already? However, all three p1, p2, and p3, are still exactly the same after the p3 = call. Why? what is GetHandle() returning? I thought it was a pointer to the native object.

eric frazer
  • 1,348
  • 1
  • 10
  • 17
  • The `using` statement will call `Dispose` method on the `image` at correct time (maybe after the block), and it never affect on the variables(p1,p2) defined outside the block . `GetHandle` returns `The unmanaged handle for the specified Objective-C class`, and you can use this method to get the managed object for a handle :`ObjCRuntime.Runtime.GetNSObject (handle);` ,check https://stackoverflow.com/questions/34762042/get-xamarin-ios-nsobject-new-or-cached-from-intptr-handle . – ColeX - MSFT Jun 09 '21 at 08:24

0 Answers0