我正在从 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/