1

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

cynistersix
  • 1,215
  • 1
  • 16
  • 30
Leo
  • 1,547
  • 3
  • 24
  • 40

2 Answers2

2

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

Community
  • 1
  • 1
Jason Bugs Adams
  • 737
  • 1
  • 5
  • 11
0

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) { ... }

Zeppomedio
  • 5,911
  • 3
  • 18
  • 18
  • Sorry Mike I haven't made my question quite clear. I am initializing an image with a URLString. In my case sometimes there would be an image on remote site and sometimes not. I have read the data by converting it into string and it says in title '404 Not Found'. I have also tried using NSPredicate to see if return string contains '404 Not Found' in it so I can put in place proper validation but it is not working. I would really appreciate if someone could help me in right direction. Thanks – Leo Aug 04 '10 at 07:23