我有一個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;
}
爲什麼在我使用圖片從網頁加載時無法工作? (圖片可以通過「瀏覽器」打開。)
可以給我一些提示或正確的代碼嗎?非常感謝你!
如果您只是在Safari中鍵入這些網址,您能看到圖像嗎? – nielsbot 2014-12-07 17:45:49
你的Img數組將通過你的方法重新創建......但它表示NSArray不是NSArray * ...這是實際的代碼嗎? – nielsbot 2014-12-07 17:47:03
對不起,實際的代碼:NSArray「Img」被放在Viewdidload中,它使用點像NSArray * Img = ..... – 2014-12-07 18:04:24