iOS Picture.todata gives NilObjectexception

I cannot get a picture to a memory block.
Keep getting NilObjectexception.
the first 2 lines below work fine where I make a new picture, but as soon as I try with an actual image it fails.
Am I missing something really obvious here?

dim tp as new Picture(32, 32) 
dim tmb as MemoryBlock = tp.ToData(Picture.Formats.PNG)

Dim mb as xojo.Core.MemoryBlock = pp.ToData(picture.Formats.PNG)
dim tp as new Picture(32, 32) 
dim tmb as MemoryBlock = tp.ToData(Picture.Formats.PNG)

Var mb As MemoryBlock
mb = pp.ToData(Picture.Formats.BMP     )

'mb = pic1.ToData(Picture.Formats.JPEG, Picture.QualityMedium    )


Dim mb as xojo.Core.MemoryBlock = pp.ToData(picture.Formats.PNG)

Have a look at the MemoryBlock in the debugger. It will be nil. So you are missing the new:

dim mb as NEW MemoryBlock

Thanks tried that but it doesn’t seem to be it, (tried lots of things like that). It seems that if the image is an asset that’s loaded then the graphics are seen and have. a value of nil.

The best solution I can find is copying the image to a new picture, which works fine.

As is usual with me, not sure if this is a feature or an error

'Where pic1 is an image asset in the project - 
dim mb as   xojo.Core.MemoryBlock = pic1.ToData(picture.Formats.PNG)
'get nil object error

dim tpic1 as picture
tpic1 = pic1
dim mb as   xojo.Core.MemoryBlock = tpic1.ToData(picture.Formats.PNG)
'get same error, but on looking at tpic1 in the debugger, graphics have a value of nil, otherwise the width height etc are there.

' so this works, make a new picture, copy the image from pic1 to tp
dim tp as new Picture(pic1.Width , pic1.Height),0,0,200,200,0,0,pic1.Width,pic1.Height)
dim mb2 as   xojo.Core.MemoryBlock = tp.ToData(picture.Formats.PNG)

I found that photo metadata was lost when using just plain ToData so I use the below Declare to put images into a Memoryblock


Public Function ToDataMBPNG(image as iOSImage) as xojo.Core.MemoryBlock
Declare function UIImagePNGRepresentation lib UIKitLib (obj_id as ptr) as ptr
dim d as new Foundation.NSData(UIImagePNGRepresentation(image.Handle))

dim mb as Xojo.Core.MemoryBlock
mb = d.DataMb

return mb

End Function


1 Like

Then yes, absolutely. Image assets are immutable because the system will automatically reload the image as necessary as needed. If you need to access the graphics, you’ll need to make a new picture of the same size and copy the original to the new one to make it mutable.

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