2014-12-07 39 views
1

我有一個UICollectionView,我想從網站加載圖片到單元格上的UIImageView。 (帶標籤的的UIImageView:100的電池。)IOS CollectionView與UIImage從URL加載

當我使用這個方法的圖片不顯示:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    Img = [NSArray arrayWithObjects:@"http://192.168.1.103:8088/Images/1.png",@"http://192.168.1.103:8088/Images/2.png",nil]; 
} 

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

    static NSString *identifier = @"List"; 
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 
    UIImageView *chImage = (UIImageView *)[cell viewWithTag:100]; 

    NSURL *imageUrl = [NSURL URLWithString:[Img objectAtIndex:indexPath.row]]; 
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]]; 
    chlImage.image = image; 

    NSLog("%@",[Img objectAtIndex:indexPath.row]); //Here can show Img's values correctly 

    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"backgroung.png"]]; 

    return cell; 
} 

但是,當我使用的圖片項目,它可以正確顯示。像這樣:

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

    static NSString *identifier = @"List"; 
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 

    UIImageView *chImage = (UIImageView *)[cell viewWithTag:100]; 
    chImage.image = [UIImages imageNames:@"123.jpg"]; 

    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"backgroung.png"]]; 

    return cell; 
} 

爲什麼在我使用圖片從網頁加載時無法工作? (圖片可以通過「瀏覽器」打開。)

可以給我一些提示或正確的代碼嗎?非常感謝你!

+0

如果您只是在Safari中鍵入這些網址,您能看到圖像嗎? – nielsbot 2014-12-07 17:45:49

+1

你的Img數組將通過你的方法重新創建......但它表示NSArray不是NSArray * ...這是實際的代碼嗎? – nielsbot 2014-12-07 17:47:03

+0

對不起,實際的代碼:NSArray「Img」被放在Viewdidload中,它使用點像NSArray * Img = ..... – 2014-12-07 18:04:24

回答

3

你期望得到來自該行的的UIImageView:

UIImageView *chImage = (UIImageView *)[cell viewWithTag:100]; 

但你不是。你只是得到一個UIView。因此,您必須創建一個UIImageView,然後使用addSubview:方法將此UIImageView添加到backgroudView視圖。

所以,它應該是:

UIImageView *chImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cell.backgroundView.frame.size.width,cell.backgroundView.frame.size.height)]; 

NSURL *imageUrl = [NSURL URLWithString:[Img objectAtIndex:indexPath.row]]; 
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]]; 
chImage.image = image; 

NSLog("%@",[Img objectAtIndex:indexPath.row]); //Here can show Img's values correctly 

[cell.backgroundView addSubview:chImage]; 

最後,我對你的建議。看看SDWebImage庫。從URL獲取圖像是一個非常棒的功能。

+0

我在單元格上使用了帶有標記:100的imageView。 當我在項目中使用圖片時,它可以正確顯示。像這樣: UIImageView * chImage =(UIImageView *)[cell viewWithTag:100]; chImage.image = [UIImages imageNames:@「123.jpg」] 爲什麼當我使用網頁圖片加載時無法工作? – 2014-12-08 09:40:41

+0

我不知道爲什麼,我從來沒有使用過DataWithContents從Web獲取圖像,因爲它不是建議的,因爲它是同步的。試試這個庫,而不是:https://github.com/rs/SDWebImage – Caio 2014-12-08 15:33:33

2

通過繼承UICollectioViewCell創建一個類的ImageCell和創建屬性ImageCell.h:

@interface ImageCell : UICollectionViewCell 
    @property (nonatomic, strong) UIImage *dImage; 
@end 

然後在ImageCell.m從UICollectionView委託添加一個功能現在

- (void)downloadImageFromURL:(NSURL *)imageUrl 
{ 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     dImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]]; 

     dispatch_sync(dispatch_get_main_queue(), ^{ 
      UIImageView *chImageView = (UIImageView *)[cell viewWithTag:100]; 
      chImageView.image = dImage; 
     }); 
    }); 
} 

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *identifier = @"List"; 
    ImageCell *cell = (ImageCell *)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 

    NSURL *imageUrl = [NSURL URLWithString:[Img objectAtIndex:indexPath.row]]; 
    [cell downloadImageFromURL:imageUrl]; 

    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]]; 

    return cell; 
} 

最後請確保您將標籤100設置爲該ImageView,您試圖得到。讓我知道你是否有任何困惑。

+0

在ImageCell.m中,cell未聲明。所以UIImageView * chImageView =(UIImageView *)[cell viewWithTag:100]將會退出錯誤。我將downloadImageFromURL中的代碼複製到UICollectionView委託中,這是行不通的。你能給我更明確的代碼嗎?謝謝! – 2014-12-11 15:38:21

+0

Do UIImageview * chImageView = [self viewWithTag:100] – 2014-12-11 17:33:16

1

只需將您的數據抓取部件從集合視圖的委託移動到其viewDidLoad。

將圖像存儲到數組中。並將其用作收藏視圖的數據源。

ie.Each time圖像被添加到數組剛剛重新加載集合視圖。

或者您可以創建一個自定義單元格,將其設置爲'iOSGeek'建議。