Check whether image exists on remote URL - iphone SDK3.0
In my iPhone I need to show a num开发者_运维问答ber of images from remote URL. Some of the URL may download some amount of data and but it may not contain actual image data. So what I need to do to check whether the downloaded data is an image data or not?
Thnx in advance, Syam S
Do you mean that you're given the actual URLs of the images (e.g. http://sstatic.net/so/img/logo.png), and need to determine whether they're valid images, or do you mean that you're given the URL of a page which may or may not contain images?
Assuming the former (which seems more likely, and is easier than the other case), you'll want to look at the Content-type
header returned by the server when you request the URL. Its value will generally be something self-explanatory, like text/html
, image/png
, image/gif
, and so on. If you want image data, it should suffice to check whether the string starts with image/
— although you might want to refine that method if it turns out not to be accurate enough for your needs.
To get this header in an iPhone app, you'll probably want to use the methods described in Apple's Communicating with HTTP Servers guide on their iPhone developer site. If you're only interested in the headers, you might consider using CFReadStreamCreateForStreamedHTTPRequest
(instead of what the guide suggests, which is CFReadStreamCreateForHTTPRequest
) to read the response. This will avoid buffering all the response data, and will let you download what you need.
精彩评论