【发布时间】:2015-10-06 03:19:51
【问题描述】:
当我将PHAssetCollection 交给PHAsset.fetchAssetsInAssetCollection:options 时,我得到了PHAssets 的集合。对于返回给我的每个UIImage,也会返回一个 nil 值。
以下是我的cellForItemAtIndexPath:indexPath方法。
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 (image == nil) {
return;
}
然后 UICollectionView 什么也不渲染。一旦我添加了 nil 检查,集合视图就会开始渲染照片库资产集合中的图像。
为什么返回给我的 nil 数量总是与 UIImage 对象数量相同?我的请求中有什么东西我搞砸了吗?
这是我上面的print 调用的输出。
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
更新:
这似乎发生在我返回的每个 PHAssetCollection 中。
更新 2:
这是我试图复制的 WWDC 示例中的部分内容。我确实注意到它确实的一件事,但我不是,它是标记单元格,以防止在重复使用时覆盖它。
- (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;
}
【问题讨论】:
标签: ios swift uiimage phasset icloud-photo-library