2014-09-04 23 views
0

我是新来的ios,我读了这个伟大的教程,从数组加载本地图片: http://www.techotopia.com/index.php/An_iOS_7_Storyboard-based_Collection_View_TutorialiOS7/xCode5/CollectionView:如何从url而不是本地文件加载图片?

它的所有工作顺利,但这只是静态的数据,我需要从URL列表加载图像。

我改变了这种阵列:(内侧:MyCollectionViewController.m/-viewDidLoad)

_carImages = [@[@"IMG_2473.jpg", 
       @"IMG_2507.jpg", 
       @"IMG_2527.jpg", 
       @"IMG_2529.jpg", 
       @"IMG_2532.jpg", 
       @"IMG_2533.jpg", 
       @"IMG_2551.jpg", 
       @"IMG_2552.jpg", 
       @"IMG_2694.jpg" 
       ] mutableCopy]; 

到这一个:

_carImages = [@[@"http://img.webmd.com/dtmcms/live/webmd/consumer_assets/site_images/articles/health_tools/baby_skin_care_slideshow/getty_rm_photo_of_babies_shopping.jpg", 
       @"http://www.northernillinoiscouponing.com/wp-content/uploads/2013/01/Colorful_Shopping_bags.jpg", 
       @"https://origin.ih.constantcontact.com/fs057/1105082123206/img/34.jpg", 
       @"http://swipetelecom.com/blog/wp-content/uploads/shopping.jpg", 
       @"http://1000thriftythings.com/wp-content/uploads/2012/11/goodwill-pet-section.jpg", 
       @"http://msophiapr.com/wp-content/uploads/2009/10/new-illustration-flat-girls-only1.jpg", 
       @"http://swipetelecom.com/blog/wp-content/uploads/shopping.jpg", 
       @"http://www.gobol.in/blog/wp-content/uploads/2013/12/online-shopping.jpg", 
       @"http://www.birth.com.au/Birth/files/9c/9c4ed300-0dc0-4c62-a638-d1afb102fcae.jpg", 
       @"http://www.islandnutznews.com/upload_pic/resize_1338925187.jpg" 
       ] mutableCopy]; 

这是为了myCell代码:(内侧:MyCollectionViewController。 m/-collectionView:cellForItemAtIndexPath :)

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
MyCollectionViewCell *myCell = [collectionView 
           dequeueReusableCellWithReuseIdentifier:@"MyCell" 
           forIndexPath:indexPath]; 

UIImage *image; 
long row = [indexPath row]; 

image = [UIImage imageNamed:_carImages[row]]; 

myCell.imageView.image = image; 
//myCell.imageView.contentMode = UIViewContentModeScaleAspectFit; 

//change width of frame 
CGRect frame = myCell.imageView.frame; 
frame.size.width = 160; 
myCell.imageView.frame = frame; 


return myCell; 
} 

但它不工作o呃,可能我在这里错过了一些东西。

理想情况下,得到这个排序后,我将使用JSON填充_carImages数组。

请指教,谢谢!

------------------------------

UPDATE 按照答案通过@ AntonStremovskiy下面,这里是我更新的代码(柜面如果有人想):

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"MyCell"; 
MyCollectionViewCell *collectionCell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; 

if (!collectionCell) { 
    collectionCell = [[MyCollectionViewCell alloc] init]; 
} 

collectionCell.imageView.image = nil; 
NSString *url = [_carImages objectAtIndex:indexPath.item]; 
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]] 
            queue:[NSOperationQueue mainQueue] 
         completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 
          collectionCell.imageView.image = [UIImage imageWithData:data]; 
         }]; 
return collectionCell; 
} 

回答

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

    static NSString *CellIdentifier = @"imageCell"; 

    ImageCollectionViewCell* collectionCell = [self.imageCollectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; 

    if (!collectionCell) { 
     collectionCell = [[ImageCollectionViewCell alloc] init]; 
    } 

     collectionCell.imageFromInstagram.image = nil; 

     NSString *url = [imagesArray objectAtIndex:indexPath.item]; 
     [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]] 
              queue:[NSOperationQueue mainQueue] 
              completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 
               collectionCell.imageFromInstagram.image = [UIImage imageWithData:data]; 
              }]; 

     UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; 
     view.backgroundColor = [UIColor colorWithRed:0.062 green:0.369 blue:0.689 alpha:0.8f]; 
     collectionCell.selectedBackgroundView = view; 

     return collectionCell; 
} 

类似的东西

+0

Anton的完全正确。在你的代码中,你基本上是告诉单元使用url字符串加载图像。它会查看包并且无法找到它(imageNamed:通过包查找具有该名称的图像)。由于无法找到该图像,因此它将为零,并且不会显示图像。加载图像的另一个选项是从NSData加载它,这是NSURL连接将返回的内容。如果你想要真的很喜欢,你可以添加一个活动监视器,并开始在异步连接之上旋转,并在数据加载后停止它。你也想要处理这个错误。 – 2014-09-04 17:08:15

+0

谢谢你,这工作,我更新了我的问题与你的编辑代码。有一些奇怪的事情发生,我必须提到,在我的模拟器上测试时,图像显示正常,但稍后有一些单元格变为空白时,当我继续向上/向下滚动时,它们再次出现,然后再次熄灭过了一会儿。看起来有点儿马车,这是否必须对图像做些什么?或者代码有一些滞后?谢谢! – user3550458 2014-09-04 17:39:20

+0

@JakeHargus感谢您的评论,我真的想要显示加载图片时加载微调,这真的会帮助用户看到一些活动正在进行。你可以与我分享代码/链接吗?谢谢! – user3550458 2014-09-04 17:40:54

相关问题