Note that there are some explanatory texts on larger screens.

plurals
  1. POUICollectionViewCell subclass containing UIImageView outlet not displaying image
    primarykey
    data
    text
    <p>I have a <code>UICollectionViewCell</code> subclass called <code>AlbumCVC</code> that contains a single <code>IBOutlet</code> --- a <code>UIImageView</code> called <code>cellView</code>. I'm setting the value of <code>cellView</code> for each cell inside the following method:</p> <pre><code>- (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; } </code></pre> <p>where <code>albumPhotos</code> is an NSMutableArray of <code>ALAsset</code>s. I'm sure that the property is getting set correctly because I get sensible results when I log the <code>albumCVC.cellImage.image.bounds.size</code>. Cells are also sized properly as the frames are visible when I set the background color. But for some reason, <code>cellImage</code> won't display. Is there another method call I need to make inside <code>collectionView:cellForItemAtIndexPath:</code> in order to get the image to show up?</p> <p><strong>Update:</strong> On the advice of a very smart friend, I tried moving the <code>UIImageView</code> out of the cell, putting it elsewhere in the main view, and everything worked lovely. The problem appears to have something to do with the frame / bounds of the <code>UIImageView</code>. I think there's a method call I need to make so that the cell's subview expands to fit the newly-resized cell following the call to <code>collectionView:layout:sizeForItemAtIndexPath:</code>. The problem now is that <code>UIImageView.image.size</code> is a read-only property, so I can't resize it directly.</p> <p><strong>Update 2:</strong> On another piece of advice I looked at the frame and bounds of the cell's <code>contentView</code> and <code>cellImage</code> and found that they weren't matching up. Added another method call to make them equal, and even changed <code>contentMode</code> to <code>UIViewContentModeScaleAspectFit</code> in order to try and get the cell to render the thumbnail properly. Unfortunately, I'm still getting tiny thumbnails inside huge cells. Any idea why? Updated code above and below.</p> <p>For the sake of completeness, here's the entire class implementation:</p> <pre><code>#import "AlbumViewController.h" #import "AlbumCVC.h" #import &lt;AssetsLibrary/AssetsLibrary.h&gt; @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 </code></pre> <p><strong>Update 3:</strong> I can't be certain what the problem was initially, but I know that it now works with the following <code>cellForItem</code> implementation:</p> <pre><code>- (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; } </code></pre> <p>There's no screwing around with frames or bounds anywhere, everything just works. Maybe it's the difference between <code>[[UIImage alloc]initWithCGImage]</code> and <code>[UIImage imageWithCGImage]</code>?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload