Display an image from URL

2019-08-01 06:23发布

I need get an image from a URL and display in a view. How can do this in Cincom Smalltalk? For example, I have this image URL:

http://news.bbcimg.co.uk/media/images/64001000/jpg/_64001280_63976026.jpg

And I wish display the image. I try:

|req content reader|
req := HttpRequest
    get:'http://news.bbcimg.co.uk/media/images/64001000/jpg/_64001280_63976026.jpg' asURI.
content:=req execute.
reader := JPEGImageReader new.
reader from: content byteSource.

But it doesn't work. Cincom Smalltalk says:

Image incomplete or partially corrupted JFIF marker expected.

when I execute: JPEGImageReader>>parseFirstMarker.

1条回答
霸刀☆藐视天下
2楼-- · 2019-08-01 06:38

The "byteSource" method is returning a stream that's already positioned to the end of file. When the JPEG reader starts to read, I hits the end and aborts.

You really shouldn't be using the byteSource method. If the contents of the URI are compressed, you won't get them properly decompressed. You should fetch the byteContents and open a readStream on that result as follows:

|req content reader image |
req := HttpRequest
    get:'http://news.bbcimg.co.uk/media/images/64001000/jpg/_64001280_63976026.jpg' asURI.
content:=req execute.
reader := JPEGImageReader new.
image := (reader from: content byteContents readStream) image
查看更多
登录 后发表回答