ios - 使用 URLWithString NSURL 总是 nil

标签 ios objective-c ios7 nsurl

我正在从 API 接收图像的 url,我想在 Collection View 中显示这些图像。

这是我的代码

NSString *str = [imgArray objectAtIndex:indexPath.item];

frstUrl=[NSURL URLWithString:str]; // <-- (APP Crash here )

[imageView setImageWithURL:frstUrl
          placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
[imageView setImageWithURL:frstUrl];

错误消息:

[__NSArrayI length]: unrecognized selector sent to instance 0xd769560


str 对象的 NSLog 返回:

str=( ( "hello.com/projects/newapp/uploads/…;, "hello.com/projects/newapp/uploads/…;,

最佳答案

该错误告诉您,您从 imgArray 检索到的对象不是 NSString,而是它本身是一个 NSArray .检查您检索到的内容(NSLog 或在调试器中检查),您会发现它是一个数组。

例如,如果 imgArray 中的关联对象本身返回另一个数组,那么您必须从中获取详细信息。例如,如果该数组中的第一项是图像 URL 字符串,那么您将执行如下操作:

NSArray *imgDetailsArray = imgArray[indexPath.item];

// let's assume that the first item in that array was the URL string, so let's grab the first item

NSString *str = imgDetailsArray[0];

// now we can use that string

frstUrl=[NSURL URLWithString:str];

[imageView setImageWithURL:frstUrl
          placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
[imageView setImageWithURL:frstUrl];

关于ios - 使用 URLWithString NSURL 总是 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22573290/

相关文章:

ios - "Invalid Image Path [...]",尽管 CFBundleIcons 不存在于 .plist 文件中

ios - 从 iphone 流媒体

ios - UICollectionView 离屏截图

ios - 检测应用程序是否从推送通知启动/打开,然后将其重定向到特定的 View Controller

ios7 - 如何使用 Xcode 6 模拟器在 ios 7 上测试我的应用程序

iphone - iOS - 旋转 View 显示背景

ios - 如何在 Xcode 中获得一个标签来显示星期几 (Swift/Swift 2)

objective-c - 不推荐使用SetText

ios - UIImageView 没有正确更新边界

objective-c - 如何将 Scintilla 包含在 XCode 3.2.5 项目中?