Store a png in a class variable

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Store a png in a class variable

Leonardo Silva
Hi,

I load a PNG file using the code below:

pngPath :=  '/Users/Somefolder/UI-Theme.png' asFileReference.
forms := pngPath readStreamDo: [ :in | (PNGReadWriter on: in) nextImage ].

...

But I don't want to create this dependency with a file path.
I would like to be able to encode the image into a string and store it in a class variable.
This way, every time I need the image, I decode the class variable back to an image and use it. Something like that, I think.

Is it possible to do it? Do you have an example?

Thank you!!

Leonardo

Reply | Threaded
Open this post in threaded view
|

Re: Store a png in a class variable

stepharo
Look at the icons because they are managed like an encoded byte array.


Le 15/5/15 21:11, Leonardo Silva a écrit :
Hi,

I load a PNG file using the code below:

pngPath :=  '/Users/Somefolder/UI-Theme.png' asFileReference.
forms := pngPath readStreamDo: [ :in | (PNGReadWriter on: in) nextImage ].

...

But I don't want to create this dependency with a file path.
I would like to be able to encode the image into a string and store it in a class variable.
This way, every time I need the image, I decode the class variable back to an image and use it. Something like that, I think.

Is it possible to do it? Do you have an example?

Thank you!!

Leonardo


Reply | Threaded
Open this post in threaded view
|

Re: Store a png in a class variable

Peter Uhnak
You can also use this utility to simplify generation of the array.

Peter

On Fri, May 15, 2015 at 9:37 PM, stepharo <[hidden email]> wrote:
Look at the icons because they are managed like an encoded byte array.


Le 15/5/15 21:11, Leonardo Silva a écrit :
Hi,

I load a PNG file using the code below:

pngPath :=  '/Users/Somefolder/UI-Theme.png' asFileReference.
forms := pngPath readStreamDo: [ :in | (PNGReadWriter on: in) nextImage ].

...

But I don't want to create this dependency with a file path.
I would like to be able to encode the image into a string and store it in a class variable.
This way, every time I need the image, I decode the class variable back to an image and use it. Something like that, I think.

Is it possible to do it? Do you have an example?

Thank you!!

Leonardo



Reply | Threaded
Open this post in threaded view
|

Re: Store a png in a class variable

MerwanOuddane
You can encore your image in base64
    anImage := 'holidays.png' asFileReference readStream binary.
    encodedImage := Base64MimeConverter mimeEncode: anImage.


Then you can decode it with:
    ImageReadWriter formFromStream: (Base64MimeConverter mimeDecodeToBytes: encodedImage).


Is this what you were searching for ?

2015-05-15 22:47 GMT+02:00 Peter Uhnák <[hidden email]>:
You can also use this utility to simplify generation of the array.

Peter

On Fri, May 15, 2015 at 9:37 PM, stepharo <[hidden email]> wrote:
Look at the icons because they are managed like an encoded byte array.


Le 15/5/15 21:11, Leonardo Silva a écrit :
Hi,

I load a PNG file using the code below:

pngPath :=  '/Users/Somefolder/UI-Theme.png' asFileReference.
forms := pngPath readStreamDo: [ :in | (PNGReadWriter on: in) nextImage ].

...

But I don't want to create this dependency with a file path.
I would like to be able to encode the image into a string and store it in a class variable.
This way, every time I need the image, I decode the class variable back to an image and use it. Something like that, I think.

Is it possible to do it? Do you have an example?

Thank you!!

Leonardo




Reply | Threaded
Open this post in threaded view
|

Re: Store a png in a class variable

Leonardo Silva

On Sat, May 16, 2015 at 5:28 PM, Merwan Ouddane <[hidden email]> wrote:
You can encore your image in base64
    anImage := 'holidays.png' asFileReference readStream binary.
    encodedImage := Base64MimeConverter mimeEncode: anImage.


Then you can decode it with:
    ImageReadWriter formFromStream: (Base64MimeConverter mimeDecodeToBytes: encodedImage).


Is this what you were searching for ?

Yes, this is cool!
I understand that encodedImage is a ReadWriteStream. So, I use 'encodedImage contents' to get the code as string.
Then I assigned the code to a variable, like that:

classVariable := 'iVBORw0KGgoAAAANSUhEUgAAAmAAAAIHCAIAAAC3HdQEQUJUQQJEcEAFRVE...'

But I don't know how to transform it back. I tried the following:

test := ReadWriteStream on: classVariable.
ImageReadWriter formFromStream: (Base64MimeConverter mimeDecodeToBytes: test).


But it doesn't work. It gives me an 'Error: image format not recognized'

Do you know what I am missing here ?

Thank you

Reply | Threaded
Open this post in threaded view
|

Re: Store a png in a class variable

Sven Van Caekenberghe-2
"Quick and easy way to get an image from a URL"
ZnEasy getPng: 'http://www.pharo.org/files/pharo.png'.

"Turn bytes into an image inside Pharo"
(PNGReadWriter on: 'http://www.pharo.org/files/pharo.png' asUrl retrieveContents readStream) nextImage.

"Store image bytes as Base64 and recreate the image"
| base64 |
base64 := ZnBase64Encoder new encode: 'http://www.pharo.org/files/pharo.png' asUrl retrieveContents.
(PNGReadWriter on: (ZnBase64Encoder new decode: base64) readStream) nextImage.

> On 17 May 2015, at 08:55, Leonardo Silva <[hidden email]> wrote:
>
>
> On Sat, May 16, 2015 at 5:28 PM, Merwan Ouddane <[hidden email]> wrote:
> You can encore your image in base64
>     anImage := 'holidays.png' asFileReference readStream binary.
>     encodedImage := Base64MimeConverter mimeEncode: anImage.
>
>
> Then you can decode it with:
>     ImageReadWriter formFromStream: (Base64MimeConverter mimeDecodeToBytes: encodedImage).
>
>
> Is this what you were searching for ?
>
> Yes, this is cool!
> I understand that encodedImage is a ReadWriteStream. So, I use 'encodedImage contents' to get the code as string.
> Then I assigned the code to a variable, like that:
>
> classVariable := 'iVBORw0KGgoAAAANSUhEUgAAAmAAAAIHCAIAAAC3HdQEQUJUQQJEcEAFRVE...'
>
> But I don't know how to transform it back. I tried the following:
>
> test := ReadWriteStream on: classVariable.
> ImageReadWriter formFromStream: (Base64MimeConverter mimeDecodeToBytes: test).
>
> But it doesn't work. It gives me an 'Error: image format not recognized'
>
> Do you know what I am missing here ?
>
> Thank you
>