Leo
Leo

Reputation: 1567

How to check if UIImage loaded from a URL is valid

Is there a way I can check if there is valid image in the NSData I've loaded into my UIImage object?

The UIImage is loading in from NSData dataWithContentsOfURL:

UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:MyURL]]];

Thanks

Upvotes: 1

Views: 3101

Answers (2)

Jason Bugs Adams
Jason Bugs Adams

Reputation: 737

see here.
Check if NSURL returns 404
using NSURLConnection you can get the statuscode

Upvotes: 2

Zeppomedio
Zeppomedio

Reputation: 6149

It depends on what you mean by a valid image. If you do

[UIImage imageNamed:]

and the image isn't valid, it will return nil, so you can do:

UIImage *myImage = [UIImage imageNamed:@"yourImageName.png"]

if (myImage != nil) { ... }

Upvotes: 0

Related Questions