如何使用动态内容填充集合视图
How to populate collection view with dynamic content
如果问题有点含糊,我深表歉意。
我遇到的问题是:
我有一个包含 9 个单元格的集合视图。每个单元格都包含 UIImage 的过滤版本和过滤器的名称。由于某种原因,索引 7(代码中的案例 7)没有被使用,看起来索引 8(案例 8)显示了两次。
相关方法如下:
-(UIImage *)filteredImageFromImage:(UIImage *)image withFilterIndex:(NSUInteger)filterIndex{
CIImage *beginImage =
[CIImage imageWithCGImage:image.CGImage];
// 1
CIContext *context = [CIContext contextWithOptions:nil];
CIFilter *filter;
switch (filterIndex) {
case 0:
filter = [CIFilter filterWithName:@"CIPhotoEffectNoir" keysAndValues: kCIInputImageKey, beginImage, nil];
break;
case 1:
filter = [CIFilter filterWithName:@"CIPhotoEffectMono" keysAndValues: kCIInputImageKey, beginImage, nil];
break;
case 2:
filter = [CIFilter filterWithName:@"CIPhotoEffectTonal" keysAndValues: kCIInputImageKey, beginImage, nil];
break;
case 3:
filter = nil;
break;
case 4:
filter = [CIFilter filterWithName:@"CIPhotoEffectFade" keysAndValues: kCIInputImageKey, beginImage, nil];
break;
case 5:
filter = [CIFilter filterWithName:@"CIPhotoEffectChrome" keysAndValues: kCIInputImageKey, beginImage, nil];
break;
case 6:
filter = [CIFilter filterWithName:@"CIPhotoEffectProcess" keysAndValues: kCIInputImageKey, beginImage, nil];
break;
case 7:
filter = [CIFilter filterWithName:@"CIPhotoEffectTransfer" keysAndValues: kCIInputImageKey, beginImage, nil];
case 8:
filter = [CIFilter filterWithName:@"CIPhotoEffectInstant" keysAndValues: kCIInputImageKey, beginImage, nil];
break;
default:
filter = nil;
break;
}
CIImage *outputImage;
if (!filter) {
outputImage = beginImage;
}else{
outputImage = [filter outputImage];
}
// 2
CGImageRef cgimg =
[context createCGImage:outputImage fromRect:[beginImage extent]];
// 3
UIImage *newImage = [UIImage imageWithCGImage:cgimg ];
// 4
CGImageRelease(cgimg);
return newImage;
}
-(NSString *)effectNameforIndex:(NSInteger )index{
NSString *name;
switch (index) {
case 0:
name = @"noir";
break;
case 1:
name = @"mono";
break;
case 2:
name = @"tonal";
break;
case 3:
name = @"original";
break;
case 4:
name = @"fade";
break;
case 5:
name = @"chrome";
break;
case 6:
name = @"process";
break;
case 7:
name = @"transfer";
case 8:
name = @"instant";
break;
default:
name = @"original";
break;
}
return name;
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 9;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
FilterCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"filterPreviewCell" forIndexPath:indexPath];
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.synchronous = YES;
[[PHImageManager defaultManager] requestImageForAsset:self.firstAsset targetSize:cell.imageView.frame.size contentMode:PHImageContentModeAspectFill options:options resultHandler:^(UIImage *result, NSDictionary *info) {
if (result) {
cell.imageView.image = [self filteredImageFromImage:result withFilterIndex:indexPath.row];
cell.nameLabel.text = [self effectNameforIndex:indexPath.row];
}
}];
cell.layer.cornerRadius = 10.0;
cell.layer.borderColor = [[UIColor colorWithWhite:1.0 alpha:0.72]CGColor];
cell.layer.borderWidth = 2.0;
cell.clipsToBounds = YES;
return cell;
}
这是结果的图像:
任何帮助将不胜感激
谢谢
您忘记在案例 7 中添加 break;
。这就是单元格 7 和 8 显示相同数据的原因。
如果问题有点含糊,我深表歉意。
我遇到的问题是: 我有一个包含 9 个单元格的集合视图。每个单元格都包含 UIImage 的过滤版本和过滤器的名称。由于某种原因,索引 7(代码中的案例 7)没有被使用,看起来索引 8(案例 8)显示了两次。
相关方法如下:
-(UIImage *)filteredImageFromImage:(UIImage *)image withFilterIndex:(NSUInteger)filterIndex{
CIImage *beginImage =
[CIImage imageWithCGImage:image.CGImage];
// 1
CIContext *context = [CIContext contextWithOptions:nil];
CIFilter *filter;
switch (filterIndex) {
case 0:
filter = [CIFilter filterWithName:@"CIPhotoEffectNoir" keysAndValues: kCIInputImageKey, beginImage, nil];
break;
case 1:
filter = [CIFilter filterWithName:@"CIPhotoEffectMono" keysAndValues: kCIInputImageKey, beginImage, nil];
break;
case 2:
filter = [CIFilter filterWithName:@"CIPhotoEffectTonal" keysAndValues: kCIInputImageKey, beginImage, nil];
break;
case 3:
filter = nil;
break;
case 4:
filter = [CIFilter filterWithName:@"CIPhotoEffectFade" keysAndValues: kCIInputImageKey, beginImage, nil];
break;
case 5:
filter = [CIFilter filterWithName:@"CIPhotoEffectChrome" keysAndValues: kCIInputImageKey, beginImage, nil];
break;
case 6:
filter = [CIFilter filterWithName:@"CIPhotoEffectProcess" keysAndValues: kCIInputImageKey, beginImage, nil];
break;
case 7:
filter = [CIFilter filterWithName:@"CIPhotoEffectTransfer" keysAndValues: kCIInputImageKey, beginImage, nil];
case 8:
filter = [CIFilter filterWithName:@"CIPhotoEffectInstant" keysAndValues: kCIInputImageKey, beginImage, nil];
break;
default:
filter = nil;
break;
}
CIImage *outputImage;
if (!filter) {
outputImage = beginImage;
}else{
outputImage = [filter outputImage];
}
// 2
CGImageRef cgimg =
[context createCGImage:outputImage fromRect:[beginImage extent]];
// 3
UIImage *newImage = [UIImage imageWithCGImage:cgimg ];
// 4
CGImageRelease(cgimg);
return newImage;
}
-(NSString *)effectNameforIndex:(NSInteger )index{
NSString *name;
switch (index) {
case 0:
name = @"noir";
break;
case 1:
name = @"mono";
break;
case 2:
name = @"tonal";
break;
case 3:
name = @"original";
break;
case 4:
name = @"fade";
break;
case 5:
name = @"chrome";
break;
case 6:
name = @"process";
break;
case 7:
name = @"transfer";
case 8:
name = @"instant";
break;
default:
name = @"original";
break;
}
return name;
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 9;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
FilterCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"filterPreviewCell" forIndexPath:indexPath];
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.synchronous = YES;
[[PHImageManager defaultManager] requestImageForAsset:self.firstAsset targetSize:cell.imageView.frame.size contentMode:PHImageContentModeAspectFill options:options resultHandler:^(UIImage *result, NSDictionary *info) {
if (result) {
cell.imageView.image = [self filteredImageFromImage:result withFilterIndex:indexPath.row];
cell.nameLabel.text = [self effectNameforIndex:indexPath.row];
}
}];
cell.layer.cornerRadius = 10.0;
cell.layer.borderColor = [[UIColor colorWithWhite:1.0 alpha:0.72]CGColor];
cell.layer.borderWidth = 2.0;
cell.clipsToBounds = YES;
return cell;
}
这是结果的图像:
任何帮助将不胜感激 谢谢
您忘记在案例 7 中添加 break;
。这就是单元格 7 和 8 显示相同数据的原因。