我正在尝试使用 URL 为 imageview 设置图像
我试过下面的代码......
NSString *imgURL = [NSString stringWithFormat:@"http://sahhtna.hardtask.org/Files/Clients/thumb/2e10ec86-f323-4889-a996-00cf6758f354.JPG"];
NSLog(@"imageURL===%@", imgURL);
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgURL]];
NSLog(@"=====%@", data);
[myImageView setImage:[UIImage imageWithData:data]];
这是完美的工作。
现在我尝试使用来自提要的数据的 URL 并在下面尝试。
NSString *imgURL = [NSString stringWithFormat:@"%@", [[feeds objectAtIndex:indexPath.row] objectForKey:@"Image"]];
NSLog(@"imageURL===%@", imgURL);
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgURL]];
NSLog(@"=====%@", data);
[myImageView setImage:[UIImage imageWithData:data]];
这给了我如下输出。
2013-08-15 11:22:56.725 Sahhtna[7555:11303] imgURL==http://sahhtna.hardtask.org/Files/Clients/thumb/2e10ec86-f323-4889-a996-00cf6758f354.JPG
2013-08-15 11:22:56.726 Sahhtna[7555:11303] =====(null)
当字符串相同时,我不明白为什么我将数据设为(null)?由于数据为空,我的 UIImageView 中没有任何图像。
知道出了什么问题吗?
当我这样做时,NSLog(@"[NSURL URLWithString:imgURL]==%@", [NSURL URLWithString:imgURL]);
我得到输出[NSURL URLWithString:imgURL]==(null)
我明白了原因。在字符串的末尾,有 Enter(新行)。我怎样才能删除它?
我得到了解决方案。
有输入和空白。
NSString *imgURL = [NSString stringWithFormat:@"%@", [[feeds objectAtIndex:indexPath.row] objectForKey:@"Image"]];
imgURL = [imgURL stringByReplacingOccurrencesOfString:@"\n" withString:@""];
imgURL = [imgURL stringByReplacingOccurrencesOfString:@" " withString:@""];