0

因此,在将 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 的类来包装单元格。

4

1 回答 1

0

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

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

我希望它会奏效。

于 2013-12-09T12:39:33.350 回答