显示URL中的图像 [英] Display an image from URL
问题描述
我需要从URL获取图像并在视图中显示。怎么能在Cincom Smalltalk中做到这一点?
例如,我有这个图片网址:
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.
但它不起作用。 Cincom Smalltalk说:
But it doesn't work. Cincom Smalltalk says:
图像不完整或部分损坏预期的JFIF标记。
Image incomplete or partially corrupted JFIF marker expected.
执行时: JPEGImageReader>> parseFirstMarker
。
推荐答案
byteSource方法返回已经定位到文件末尾的流。当JPEG阅读器开始阅读时,我点击结束并中止。
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.
你真的不应该使用byteSource方法。如果URI的内容被压缩,您将无法正确解压缩它们。您应该获取byteContents并在该结果上打开一个readStream,如下所示:
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
这篇关于显示URL中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!