Store a png in a class variable
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
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
You can also use this utility to simplify generation of the array. http://smalltalkhub.com/#!/~peteruhnak/IconFactory Peter On Fri, May 15, 2015 at 9:37 PM, stepharo <stepharo@free.fr> 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
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 <i.uhnak@gmail.com>:
You can also use this utility to simplify generation of the array. http://smalltalkhub.com/#!/~peteruhnak/IconFactory
Peter
On Fri, May 15, 2015 at 9:37 PM, stepharo <stepharo@free.fr> 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
On Sat, May 16, 2015 at 5:28 PM, Merwan Ouddane <merwanouddane@gmail.com> 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
"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 <leonardo.humberto@gmail.com> wrote:
On Sat, May 16, 2015 at 5:28 PM, Merwan Ouddane <merwanouddane@gmail.com> 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
participants (5)
-
Leonardo Silva -
Merwan Ouddane -
Peter Uhnák -
stepharo -
Sven Van Caekenberghe