2013-07-22 48 views
0

我有一個UICollectionViewCell子類AlbumCVC包含單個IBOutlet --- UIImageView稱爲cellView。我以下方法內的每個小區設置的cellView值:UICollectionViewCell子類包含UIImageView插座不顯示圖像

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

    UICollectionViewCell *cell; 

    cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"albumPhotoCell" forIndexPath:indexPath]; 
    cell.backgroundColor = [UIColor blueColor]; 
    if ([cell isKindOfClass:[AlbumCVC class]]){ 
     AlbumCVC *albumCVC = (AlbumCVC *)cell; 
     ALAsset *thisImage = [self.albumPhotos objectAtIndex:indexPath.item];   

     albumCVC.imageView.frame = albumCVC.contentView.frame; 
     albumCVC.contentView.contentMode = UIViewContentModeScaleAspectFit; 
     albumCVC.imageView.image = [UIImage imageWithCGImage:[thisImage aspectRatioThumbnail]]; 

     } 
    } 
    return cell; 
} 

其中albumPhotosALAsset秒的的NSMutableArray。我確信該物業正在設置正確,因爲當我登錄albumCVC.cellImage.image.bounds.size時,我得到了明智的結果。單元格大小也適當,因爲當我設置背景顏色時,框架可見。但由於某些原因,cellImage將不會顯示。我需要在collectionView:cellForItemAtIndexPath:內部進行另一種方法調用,才能顯示圖像?

更新:在一個非常聰明的朋友的建議下,我嘗試移動UIImageView出細胞,把它在其他地方的主要觀點,和一切工作可愛。這個問題似乎與UIImageView的框架/範圍有關。我認爲我需要做一個方法調用,以便在調用collectionView:layout:sizeForItemAtIndexPath:之後,單元格的子視圖擴展爲適合新調整大小的單元格。現在的問題是,UIImageView.image.size是隻讀屬性,所以我無法直接調整它的大小。

更新2:在另一個忠告我看着框架和電池的contentViewcellImage的界限,發現他們不匹配起來。添加了另一個方法調用以使它們相等,甚至將contentMode更改爲UIViewContentModeScaleAspectFit以試圖讓單元格正確地呈現縮略圖。不幸的是,我仍然在巨大的單元格內獲得小縮略圖。任何想法爲什麼?上面和下面更新了代碼。

爲了完整起見,這裏的整個類實現:

#import "AlbumViewController.h" 
#import "AlbumCVC.h" 
#import <AssetsLibrary/AssetsLibrary.h> 

@interface AlbumViewController() 
@end 

@implementation AlbumViewController 


#pragma constants 

const int IPHONE_WIDTH = 320; 

#pragma delegate methods 

- (NSInteger)collectionView:(UICollectionView *)collectionView 
    numberOfItemsInSection:(NSInteger)section{ 

    // Get the count of photos in album at index albumIndex in the PhotoHandler 
    NSInteger numCells = [self.group numberOfAssets]; 
    return numCells; 

} 

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

    UICollectionViewCell *cell; 

    cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"albumPhotoCell" forIndexPath:indexPath]; 
    cell.backgroundColor = [UIColor blueColor]; 
    if ([cell isKindOfClass:[AlbumCVC class]]){ 
     AlbumCVC *albumCVC = (AlbumCVC *)cell; 
     ALAsset *thisImage = [self.albumPhotos objectAtIndex:indexPath.item]; 
     } 

     albumCVC.imageView.frame = albumCVC.contentView.frame; 
     albumCVC.contentView.contentMode = UIViewContentModeScaleAspectFit; 
     albumCVC.imageView.image = [UIImage imageWithCGImage:[thisImage aspectRatioThumbnail]]; 

    } 
    return cell; 
} 

- (CGSize)collectionView:(UICollectionView *)collectionView 
        layout:(UICollectionViewLayout*)collectionViewLayout 
    sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ 


    //Copy a pointer to an asset from the album 
    ALAsset *thisImage = [self.albumPhotos objectAtIndex:indexPath.item]; //Zen - are you sure thisImage represents a valid image? 

    //Copy that asset's size and create a new size struct 
    CGSize thisSize = thisImage.defaultRepresentation.dimensions; 
    CGSize returnSize; 

    // force all previews to be full width 
    returnSize.width = IPHONE_WIDTH; 
    returnSize.height = IPHONE_WIDTH * thisSize.height/thisSize.width; 

    return returnSize; 
} 


#pragma lifecycle methods 

- (void)viewWillAppear:(BOOL)animated{ 
    [self.albumPhotos removeAllObjects]; 

//"handler" is a class that manages calls to the ALAssetLibrary. self.albumIndex is an integer that gets set on segue. As far as I can tell, everything in the below method is working fine --- cells are sized properly. 

    self.group = self.albumDelegate.handler.groups[self.albumIndex]; 
    [self.group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) { 
     if (result) { 
      NSLog(@"Just added an object to albumPhotos."); 
      [self.albumPhotos addObject:result]; 
      NSLog(@"The item in albumPhotos is class %@", [self.albumPhotos[0] class]); 
     } 
    }]; 

} 


#pragma instantiation 

- (ALAssetsGroup *)group{ 
    if (!_group) { 
     _group = [[ALAssetsGroup alloc]init]; 
    } 
    return _group; 
} 

- (NSMutableArray *)albumPhotos{ 
    if (!_albumPhotos) { 
     _albumPhotos = [[NSMutableArray alloc]init]; 
    } 
    return _albumPhotos; 
} 

@end 

更新3:我不能確定的問題是什麼開始,但我知道它現在用下面的工作cellForItem實現:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView 
        cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    UICollectionViewCell *cell; 
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"albumPhotoCell" forIndexPath:indexPath]; 
    if ([cell isKindOfClass:[AlbumCVC class]]) { 
     AlbumCVC *albumCVC = (AlbumCVC *)cell; 
     albumCVC.albumImageView.image = [[UIImage alloc] initWithCGImage:[self.albumAssets[[self reverseAlbumIndexForIndex:indexPath.item]] thumbnail]]; 
    } 
    cell.alpha = [self alphaForSelected:self.selectedItems[@(indexPath.item)]]; 
    return cell; 
} 

有沒有用框架或界限胡鬧的任何地方,一切都只是工作。也許這是[[UIImage alloc]initWithCGImage][UIImage imageWithCGImage]之間的區別?

+0

你使用storyboard嗎?你怎麼註冊cell? –

+0

是的,我使用故事板。這個單元格被註冊了'albumPhotoCell'。 – jefflovejapan

+0

你是否檢查過執行進入'if([cell isKindOfClass:[AlbumCVC class]]){...}'block? - 你是否檢查過「albumCVC.cellImage'不是'nil'? –

回答

1

我有過類似的問題,並通過設置UICollectionViewCell框架屬性與UIImageView的框架相同來解決它。我不是100%確定這是你的問題,我只是在代碼中構建集合(沒有Storyboard)

+0

謝謝,這絕對是它的一部分,但縮略圖仍然拒絕正確尺寸。 – jefflovejapan

相關問題