本文介绍了如何检查NSData是否对UIImage中的使用有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a jpeg file, I can confirm that the URL content is gathered successfully, but when I try to put my image data into the UIImage my app fails. I wonder if there is any check for NSData to confirm it is usable for UIImage.

我也不知道知道导致这种失败的原因,以及如何防止它。任何帮助将不胜感激。

I also don't know what causes this failure, and how to prevent it. Any help would be appreciated.

推荐答案

如上所述 imageWithData:如果UIImage无法从数据创建图像,则返回nil。在运行时处理此问题的正确方法是在该方法返回nil时提供占位符图像。

As stated in the documentation imageWithData: returns nil if UIImage could not create an image from the data. The correct way to handle this at runtime is to provide a placeholder image when that method returns nil.

对于诊断,首先查看控制台消息,它们有时会提供有用的信息。如果没有,您最好的运气是将NSData转储到磁盘并将结果与​​存储在服务器上的文件进行比较。您有时会收到损坏的数据,有时您只会收到一条html错误消息,其中包含jpeg二进制数据。

As for the diagnostic, first look at the console messages, they sometimes provide useful info. If not, your best luck is to dump the NSData to disk and compare the result to the file stored on the server. You sometimes get corrupted data, you sometimes just get an html error message where you were expecting jpeg binary data.

这篇关于如何检查NSData是否对UIImage中的使用有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

查看更多