如何从标签获取图像?

时间:2022-11-27 18:18:44

When I want to render a picture, I send a request to the server with the associated picture to render. What's odd is how it's returned:

当我想渲染图片时,我向服务器发送请求以及相关图片进行渲染。奇怪的是它是如何返回的:

<img src="https://blargh.com/displayTemplate?templateid=template1">

Where that link is supposed to be the image data.

该链接应该是图像数据。

Using this, how can I transform that into an image that I can display to the user? This is for a facebook app, I can't just embed the HTML. It needs to be displayed inside my AS App as a Bitmap or Sprite or anything, really. Trying to convert it to a Bitmap or BitmapData have failed...

使用此功能,如何将其转换为可以向用户显示的图像?这是一个Facebook应用程序,我不能只嵌入HTML。它需要作为Bitmap或Sprite或其他任何东西显示在我的AS App中。试图将其转换为Bitmap或BitmapData失败了......

The only other information I can give is that my templateLoader is a Loaderand its .data is supposed to carry the HTML.

我能给出的唯一其他信息是我的templateLoader是一个Loaderand,它的.data应该带有HTML。

3 个解决方案

#1


3  

Use e4x. I'm not sure if you're getting a string or the result format on your service call is already XML, but if it's a string, you'd do something like this:

使用e4x。我不确定你是否收到字符串或服务调用的结果格式已经是XML,但如果它是一个字符串,你会做这样的事情:

var imgXML:XML = XML(yourString);//yourString contains <img src="https://blargh.com/displayTemplate?templateid=template1"> 
link = imgXML.@src;

Then, look at the code Zevan posted for how to use a Loader if you're using just AS, or use it as the source for an Image control in Flex.

然后,如果您只使用AS,请查看Zevan发布的有关如何使用Loader的代码,或者将其用作Flex中Image控件的源代码。

#2


5  

Something like this:

像这样的东西:

var data:String = '<img src="https://blargh.com/displayTemplate?templateid=template1">';
// grab the src attribute
var url:Array = data.match(/<img src=\"(.*?)\">/);

if (url.length > 1){
  var loader:Loader = new Loader();
  loader.load(new URLRequest(url[1]));
  addChild(loader);
}

#3


1  

Looks like the server is providing you with a link that dynamically looks up the image based on the GET data you're passing to the server (the ?templateid=template1). Too bad you didn't paste in the real link so that this theory could be proven. Take the real link and copy out the http:// portion, enter it into your browser and if the image appears then this is indeed the case.

看起来服务器正在为您提供一个链接,该链接根据您传递给服务器的GET数据动态查找图像(?templateid = template1)。太糟糕了,你没有粘贴在真正的链接中,这样就可以证明这个理论。获取真实链接并复制出http://部分,将其输入浏览器,如果图像出现,则情况确实如此。

If this is true, then you want to extract the link from the tag. You could do this with a regular expression, like so:

如果是这样,那么您想从标记中提取链接。您可以使用正则表达式执行此操作,如下所示:

/\?)"(.?)"(.*)/

If you ran that regex against the full tag like you've provided above, then capture group 2 will contain just the HTTP link. You can then use a Loader object to fetch the image so you're actually downloading and presenting the binary image data instead of embedding HTML.

如果您像上面提供的那样针对完整标记运行该正则表达式,则捕获组2将仅包含HTTP链接。然后,您可以使用Loader对象来获取图像,这样您实际上就可以下载并呈现二进制图像数据,而不是嵌入HTML。

If you're going to be using Regex in AS3, then you absolutely must have the RegExr tool by grantskinner.com: http://gskinner.com/RegExr/desktop/.

如果你打算在AS3中使用Regex,那么你必须拥有grantskinner.com的RegExr工具:http://gskinner.com/RegExr/desktop/。

Also, to get the data from capture group 2 we do this:

另外,要从捕获组2获取数据,我们这样做:

var imageTag:String = '<img src="https://blargh.com/displayTemplate?templateid=template1">'
var myHttpRegex:Regex = /\<img(.*?)"(.*?)"(.*)/;
var result:Object;

result = myHttpRegex.exec(imageTag);
if(result != null) {
   var imgUrl:String = result[1];
}

Code is untested, but the concept is there.

代码未经测试,但概念就在那里。

#1


3  

Use e4x. I'm not sure if you're getting a string or the result format on your service call is already XML, but if it's a string, you'd do something like this:

使用e4x。我不确定你是否收到字符串或服务调用的结果格式已经是XML,但如果它是一个字符串,你会做这样的事情:

var imgXML:XML = XML(yourString);//yourString contains <img src="https://blargh.com/displayTemplate?templateid=template1"> 
link = imgXML.@src;

Then, look at the code Zevan posted for how to use a Loader if you're using just AS, or use it as the source for an Image control in Flex.

然后,如果您只使用AS,请查看Zevan发布的有关如何使用Loader的代码,或者将其用作Flex中Image控件的源代码。

#2


5  

Something like this:

像这样的东西:

var data:String = '<img src="https://blargh.com/displayTemplate?templateid=template1">';
// grab the src attribute
var url:Array = data.match(/<img src=\"(.*?)\">/);

if (url.length > 1){
  var loader:Loader = new Loader();
  loader.load(new URLRequest(url[1]));
  addChild(loader);
}

#3


1  

Looks like the server is providing you with a link that dynamically looks up the image based on the GET data you're passing to the server (the ?templateid=template1). Too bad you didn't paste in the real link so that this theory could be proven. Take the real link and copy out the http:// portion, enter it into your browser and if the image appears then this is indeed the case.

看起来服务器正在为您提供一个链接,该链接根据您传递给服务器的GET数据动态查找图像(?templateid = template1)。太糟糕了,你没有粘贴在真正的链接中,这样就可以证明这个理论。获取真实链接并复制出http://部分,将其输入浏览器,如果图像出现,则情况确实如此。

If this is true, then you want to extract the link from the tag. You could do this with a regular expression, like so:

如果是这样,那么您想从标记中提取链接。您可以使用正则表达式执行此操作,如下所示:

/\?)"(.?)"(.*)/

If you ran that regex against the full tag like you've provided above, then capture group 2 will contain just the HTTP link. You can then use a Loader object to fetch the image so you're actually downloading and presenting the binary image data instead of embedding HTML.

如果您像上面提供的那样针对完整标记运行该正则表达式,则捕获组2将仅包含HTTP链接。然后,您可以使用Loader对象来获取图像,这样您实际上就可以下载并呈现二进制图像数据,而不是嵌入HTML。

If you're going to be using Regex in AS3, then you absolutely must have the RegExr tool by grantskinner.com: http://gskinner.com/RegExr/desktop/.

如果你打算在AS3中使用Regex,那么你必须拥有grantskinner.com的RegExr工具:http://gskinner.com/RegExr/desktop/。

Also, to get the data from capture group 2 we do this:

另外,要从捕获组2获取数据,我们这样做:

var imageTag:String = '<img src="https://blargh.com/displayTemplate?templateid=template1">'
var myHttpRegex:Regex = /\<img(.*?)"(.*?)"(.*)/;
var result:Object;

result = myHttpRegex.exec(imageTag);
if(result != null) {
   var imgUrl:String = result[1];
}

Code is untested, but the concept is there.

代码未经测试,但概念就在那里。