NSError *error = nil; NSURL *url = [NSURL URLWithString:[imageLinks objectAtIndex:0]]; NSData *tdata = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&error]; if (error) { NSLog(@"%@",[error localizedDescription]); }else { // no error,this one is being called within my app NSLog(@"Data loaded successfully"); } self.pictureView1.image = [UIImage imageWithData:tdata];
我有一个jpeg文件,我可以确认URL内容是否成功收集,但是当我尝试将我的图像数据放入UIImage时,我的应用程序失败.我想知道是否有任何检查NSData确认它可用于UIImage.
我也不知道是什么原因导致这个失败,以及如何防止它.任何帮助将不胜感激.
解决方法
如所述
in the documentation imageWithData:如果UIImage无法从数据创建图像,则返回nil.在运行时处理此方法的正确方法是在该方法返回nil时提供占位符映像.
至于诊断,首先看看控制台消息,他们有时提供有用的信息.如果没有,最好的运气是将NSData转储到磁盘,并将结果与存储在服务器上的文件进行比较.你有时会得到损坏的数据,你有时只是得到一个html错误消息,你期望jpeg二进制数据.