我怎么能从NSArray加载URL

时间:2016-03-03 03:01:58

标签: ios url nsarray

我使用random()创建一个NSMutableArray并在NSMutableArray中使用NSUrl来加载图像以粘贴到每个tableview标题

NSMutableArray *imagearray = [[NSMutableArray alloc]init];

for (int i = 0;i < 10; i++)
{
    [imagearray addObject:[NSString stringWithFormat:@"http://flicksbank.console360.net/images/%@/default.jpg",randomArray[i][@"id"]]];
}

[imagearray setImageWithURL:[NSURL URLWithString:url1] placeholderImage:[UIImage imageNamed:@"video-bg.png"]];

2 个答案:

答案 0 :(得分:0)

我认为你在AFNetworking图像缓存方法中使用过,在这里你的错误是

你为setImageWithURL NSMutableArray打了100%错误。

[imagearray setImageWithURL:[NSURL URLWithString:url1] placeholderImage:[UIImage imageNamed:@"video-bg.png"]];

你需要做的事情

 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *teamview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
    /* Create custom view to display section header... */
      UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
      [imageView setImageWithURL:[NSURL URLWithString:url1] placeholderImage:[UIImage imageNamed:@"video-bg.png"]];
    [teamview addSubview:imageView];
    [teamview setBackgroundColor:[UIColor colorWithRed:166/255.0 green:177/255.0 blue:186/255.0 alpha:1.0]]; //your background color...
    return teamview;
}

答案 1 :(得分:0)

如果有很多网址,你可以存储在数组中,并通过objectatindex存储NSString中的对象

但是如果有单个URL,那么您不需要将URL存储到数组中。您可以使用以下功能直接获取图片格式网址

从网址获取图片声明以下功能

-(UIImage *) getImageFromURL:(NSString *)fileURL  {
    NSURL *url = [NSURL URLWithString:
                  fileURL];
    UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];

    return image;
     }

您必须将网址存储到

字符串中
  

NSString * imageurl =   @&#34;您的URL路径在这里用于图像&#34 ;;

您拥有自己的imageview并调用以下方法从url

分配该图片
  

自我。&#34; Yourimageview&#34; .image = [self getImageFromURL:imageurl];

希望这对你有用

相关问题