Display an image from URL

╄→гoц情女王★ 提交于 2019-12-13 01:53:57

问题


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:


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


来源:https://stackoverflow.com/questions/23173980/display-an-image-from-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!