【问题标题】:How to load UIImage to UICollectionView from an array of UIImage如何从 UIImage 数组将 UIImage 加载到 UICollectionView
【发布时间】:2013-08-27 04:13:22
【问题描述】:

因此,在将 UIImage 从 ImagePickerController 保存到 NSMutableArray 后,我想将图像加载到 UICollectionView,但是即使数组不为零,单元格也不会显示任何内容。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
  {
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [_imgs addObject:image];
    [picker dismissModalViewControllerAnimated:YES];
    [self.collectionView reloadData];

    NSLog(@"%d", [_imgs count]);

    //[self viewDidLoad];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    Icon *icon = [collectionView dequeueReusableCellWithReuseIdentifier:@"ICON" forIndexPath:indexPath];

    UIImageView *recipeImageView = (UIImageView *)[icon viewWithTag:100];
    recipeImageView.image = [UIImage imageNamed:[self.imgs objectAtIndex:indexPath.row]];

    icon.image = (UIImage*)[self.imgs objectAtIndex:indexPath.row];
    [icon.deleteButton addTarget:self action:@selector(delete:) forControlEvents:UIControlEventTouchUpInside];
    return icon;
}

@implementation Icon
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        UIView *insetView = [[UIView alloc] initWithFrame:CGRectInset(self.bounds, 300, 
400)];
        self.image = [[UIImage alloc]init];
        UIImageView *img =[[UIImageView alloc]initWithImage:self.image];
        [self.contentView addSubview:insetView];
        [insetView addSubview:img];
        self.layer.shouldRasterize = YES;
    }
}

我创建了一个名为 ICON 的类来包装单元格。

【问题讨论】:

  • 你的collection view的dataSource属性设置了吗?如果是,您是否检查了相关数据源方法的返回值,即numberOfSectionsInCollectionViewcollectionView:numberOfItemsInSection:collectionView:cellForItemAtIndexPath:?线索应该在那里。

标签: ios ios6 uicollectionview uicollectionviewcell uicollectionviewlayout


【解决方案1】:

我认为您没有为您的UIImageView 对象设置标签,因此当您尝试使用viewWithTag 访问您的UIImageView 时,它不会返回任何内容。

UIImageView *img =[[UIImageView alloc]initWithImage:self.image];
img.tag = 100;
[insetView addSubview:img];
self.layer.shouldRasterize = YES;

我希望它会起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多