How can I tell if a stream contains a picture or not? I am working with Delphi xe8 FMX developing an iOS application. I have a listbox and am loading pictures into the items. I can do this:
如何判断流是否包含图片?我正在使用Delphi xe8 FMX开发iOS应用程序。我有一个列表框,正在将图片加载到项目中。我可以做这个:
if not Assigned(S) then
s:=TMemoryStream.Create;
if not Assigned(clHTTP) then
clHTTP := TIDHTTP.Create;
with clHTTP do
begin
clHTTP.HandleRedirects := True;
clHTTP.AllowCookies := True;
clHTTP.RedirectMaximum := 110000;
clHTTP.Get(someimageURL,s);
end;
s.Seek(0,soFromBeginning);
try
LItem.ItemData.Bitmap.LoadFromStream(s);
except
clHTTP.Get(DefaultImageURL,s);
s.Seek(0,soFromBeginning);
LItem.ItemData.Bitmap.LoadFromStream(s);
end;
s.Free;
clHTTP.Free;
I would prefer not to use a try-except block because it appears this causes loading of the bitmaps to be inconsistent. For example, I have to scroll the listbox items out of view, then back into view to see the pictures.
我宁愿不使用try-except块,因为它看起来会导致位图的加载不一致。例如,我必须将列表框项目滚动到视图之外,然后返回视图以查看图片。
1 个解决方案
#1
A quick way would be to check for the file signature. For instance, here are some common image format signatures:
一种快速方法是检查文件签名。例如,以下是一些常见的图像格式签名:
- PNG: 89 50 4E 47 0D 0A 1A 0A
- JPEG: FF D8 FF E0
- GIF87a: 47 49 46 38 37 61
- GIF89a: 47 49 46 38 39 61
PNG:89 50 4E 47 0D 0A 1A 0A
JPEG:FF D8 FF E0
GIF87a:47 49 46 38 37 61
GIF89a:47 49 46 38 39 61
A comprehensive list can be found here: http://en.wikipedia.org/wiki/List_of_file_signatures
可在此处找到完整列表:http://en.wikipedia.org/wiki/List_of_file_signatures
This approach will not prove that the rest of the stream is valid, but it is a good start and will allow you at least to reject obvious ringers.
这种方法不能证明流的其余部分是有效的,但它是一个良好的开端,并且至少可以让你拒绝明显的铃声。
#1
A quick way would be to check for the file signature. For instance, here are some common image format signatures:
一种快速方法是检查文件签名。例如,以下是一些常见的图像格式签名:
- PNG: 89 50 4E 47 0D 0A 1A 0A
- JPEG: FF D8 FF E0
- GIF87a: 47 49 46 38 37 61
- GIF89a: 47 49 46 38 39 61
PNG:89 50 4E 47 0D 0A 1A 0A
JPEG:FF D8 FF E0
GIF87a:47 49 46 38 37 61
GIF89a:47 49 46 38 39 61
A comprehensive list can be found here: http://en.wikipedia.org/wiki/List_of_file_signatures
可在此处找到完整列表:http://en.wikipedia.org/wiki/List_of_file_signatures
This approach will not prove that the rest of the stream is valid, but it is a good start and will allow you at least to reject obvious ringers.
这种方法不能证明流的其余部分是有效的,但它是一个良好的开端,并且至少可以让你拒绝明显的铃声。