When I take a PHAssetCollection
and give it to PHAsset.fetchAssetsInAssetCollection:options
I am given back a collection of PHAsset
s. For every UIImage
given back to me, there is a nil value given back as well.
The following is my cellForItemAtIndexPath:indexPath
method.
public override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("AssetCellIdentifier", forIndexPath: indexPath) as? AssetCollectionViewCell;
let asset = self.assetFetchResult!.objectAtIndex(indexPath.row);
self.imageManager.requestImageForAsset(
asset as! PHAsset,
targetSize: CGSize.init(width: asset.pixelWidth, height: asset.pixelHeight),
contentMode: PHImageContentMode.AspectFill,
options: nil) { (image, info) -> Void in
print (image);
if (image == nil) {
return;
}
cell?.assetThumbnail.contentMode = UIViewContentMode.ScaleAspectFit;
cell?.assetThumbnail.image = image;
}
return cell!;
}
If I don't put the nil check
if (image == nil) {
return;
}
Then the UICollectionView just renders nothing. As soon as I add the nil check, the collection view starts rendering the images from the photo library's asset collection.
Why are there always the same number of nils given back to me as there are UIImage
objects? Is there something in my request that I am messing up?
This is the output of my print
invocation above.
Optional(<UIImage: 0x12f8263d0>, {40, 30})
Optional(<UIImage: 0x12e5265a0>, {40, 30})
Optional(<UIImage: 0x12e664c90>, {40, 30})
Optional(<UIImage: 0x12e74c860>, {40, 30})
Optional(<UIImage: 0x12e58c580>, {40, 30})
Optional(<UIImage: 0x12e60b6f0>, {40, 30})
Optional(<UIImage: 0x12e7657d0>, {40, 30})
Optional(<UIImage: 0x12e6655e0>, {40, 30})
Optional(<UIImage: 0x12e743ca0>, {40, 30})
Optional(<UIImage: 0x12e5fb6e0>, {40, 30})
Optional(<UIImage: 0x12e5fb8e0>, {40, 30})
Optional(<UIImage: 0x12f85f3b0>, {40, 30})
nil
nil
nil
nil
nil
nil
nil
nil
nil
nil
nil
nil
Update:
It seems that this happens across every PHAssetCollection that I am given back.
Update 2:
This is the bits from the WWDC example I am trying to replicate. The one thing I did notice that it does, that I am not, is tagging the cells in order to prevent overriding it if it's being reused.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
AAPLGridViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellReuseIdentifier forIndexPath:indexPath];
// Increment the cell's tag
NSInteger currentTag = cell.tag + 1;
cell.tag = currentTag;
PHAsset *asset = self.assetsFetchResults[indexPath.item];
[self.imageManager requestImageForAsset:asset
targetSize:AssetGridThumbnailSize
contentMode:PHImageContentModeAspectFill
options:nil
resultHandler:^(UIImage *result, NSDictionary *info) {
// Only update the thumbnail if the cell tag hasn't changed. Otherwise, the cell has been re-used.
if (cell.tag == currentTag) {
cell.thumbnailImage = result;
}
}];
return cell;
}