如何判断NSData是否返回nil,如果没有Internet连接或者图像不再可用?NSData加载UIImage
这是代码:
NSString *str = @"http://p.twimg.com/A76-I-PCYAA75YH.jpg";//URL is broken
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
image = [UIImage imageWithData:imgData];
NSData的是零在两种情况下:
- 如果thers没有互联网连接。
- 或者url中的图片不再可用。
使用[这](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html#//apple_ref/occ/clm/NSData/dataWithContentsOfURL:选项:错误:) – nhahtdh 2013-02-13 10:00:29
当然,这很容易检查imgData ==零...?你一定是真的在问更多。你需要重新提出你的问题吗? – Todd 2013-02-13 10:03:03
no tod,我知道如何检查对象是否为零,问题是我想知道为什么它是零,是因为它们没有连接,或者是因为图像不再可用 – Aziz 2013-02-13 10:09:42