UiCollectionView - 从网址加载图片

时间:2016-11-15 07:22:33

标签: ios objective-c uicollectionview

我试图创建UicollectionView并在该单元格中加载图像。我只得到细胞,而不是图像。

- (void)viewDidLoad    
{
   [super viewDidLoad];  
   self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
    collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(10, 30, self.view.frame.size.width-20, self.view.frame.size.height)collectionViewLayout:layout];
;
    [collectionView setDataSource:self];
    [collectionView setDelegate:self];

    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
    [collectionView setBackgroundColor:[UIColor blackColor]];

    [self.view addSubview:collectionView];

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 8;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = @"cookcell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

   if (!cell) { 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
   }

   NSURL *url = [NSURL URLWithString:@"http://img.auctiva.com/imgdata/1/2/8/3/1/8/7/webimg/611377020_tp.jpg"];

   NSData *data = [[NSData alloc] initWithContentsOfURL:url];
   UIImage *tmpImage = [[UIImage alloc] initWithData:data];

   cell.imageView.image = tmpImage ;
   return  cell;;  
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

   UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];

   cell.backgroundColor=[UIColor whiteColor];

   return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{ 
    return CGSizeMake(80, 80);
}

- (void)didReceiveMemoryWarning {

     [super didReceiveMemoryWarning];

     // Dispose of any resources that can be recreated.
}

4 个答案:

答案 0 :(得分:0)

  • 确认数据源的委托,并将collectionView的委托委托给viewController,无论您在何处使用它。

  • 在委托中,我们有与UITableView相同的cellForRowAtIndex。

  • 获取该函数中与indexpath相对应的单元格。

  • 将图像分配给该单元格,(单元格应该具有UIImageView)。如果您想要静态图像,可以在每个索引路径的委托方法中直接分配给相应的单元格类。

答案 1 :(得分:0)

看到你的代码,你已经为集合视图设置了数据源协议,但这不是从url加载图像的方法。从swift 3开始,您必须实现以下数据源协议方法。

func collectionView(_ collectionView:UICollectionView,numberOfItemsInSection section:Int) - > INT {

 return datalistArr.count

}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collecionViewCell", for: indexPath) as! CustomCell           
    cell.imageViewIncell.af_setImage(withURL: URL(string: imageurl), placeholderImage: UIImage(named: "placeholderImage"))
    return cell
}

如果你使用过Alamofire,它有UIImageView的扩展,它可以很容易地从url加载图像,例如,cell.imageViewIncell.af_setImage(withURL:URL(string:imageurl),placeholderImage:UIImage(named:" placeholderImage& #34))

答案 2 :(得分:0)

您需要在cellForRowAtIndexPath中设置图像:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"receipeCell" forIndexPath:indexPath];
    NSURL *url = [NSURL URLWithString:@"http://img.auctiva.com/imgdata/1/2/8/3/1/8/7/webimg/611377020_tp.jpg"];

    NSData *data = [[NSData alloc] initWithContentsOfURL:url];

    UIImage *tmpImage = [[UIImage alloc] initWithData:data];
    cell.imageView.image = tmpImage ;

    return  cell;;

}

答案 3 :(得分:0)

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return imageURl.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
    let imageview:UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
    do {
        let data = try Data.init(contentsOf: URL.init(string: "your URL")!)
        let img = UIImage.init(data: data)
        DispatchQueue.main.async {
            imageview.image = img
            cell.contentView.addSubview(imageview)
        }
    } catch let erroer {
        print(erroer.localizedDescription)
    }
    //If you load image using api use cache-policy or pod 'SDWebImage', '~> 5.0'. 
    //imageView.sd_setImage(with: URL(string: "your URL"), placeholderImage: UIImage(named: ""your placeholder image""))
    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize.init(width: 200, height: 200)
}

这种使用URL显示图像的简单方法。