繁体   English   中英

如何从UIImage数组将UIImage加载到UICollectionView

[英]How to load UIImage to UICollectionView from an array of UIImage

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

- (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的类来包装单元格。

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

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

我希望它能起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM