を受け取ってPHAssetCollectionに渡すとPHAsset.fetchAssetsInAssetCollection:options、 のコレクションが返されPHAssetます。私に返されるたびにUIImage、返されるゼロの値もあります。
以下は私の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!;
}
nil チェックを入れないと
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 の例からのビットです。私が気づいたことの 1 つは、セルが再利用されている場合に上書きされないように、セルにタグを付けることです。
- (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;
}