1

我的代码分为两部分:

第一个是我的表格视图的“cellForRowAtIndexPath”方法。在这个表格视图中,我应该在每个单元格中显示 3 个按钮,每个按钮都是不同的图像。图像位于 NSMutableArray 中,称为“photosArray”。它看起来像这样。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    if (photosArray.count > 0) {
        for (int i = 0; i < 3; i++) {
            if (photosArray.count > photoCounter) {

                //position the UiButtons in the scrollView
                CGRect frame = CGRectMake((i*100) + 30, 5, 60, 60);

                //Create the UIbuttons
                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                [button setTag:i];
                [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
                button.frame = frame;
                button.layer.borderWidth = 2;
                button.layer.cornerRadius = 5;
                button.clipsToBounds = YES;

                //Add images to the button

                NSLog(@"PhotosArray count is: %d",photosArray.count);
                if ([[photosArray objectAtIndex:0] isKindOfClass:[UIImage class]]) {
                    NSLog(@"UIImage class true");
                }
                if ([[photosArray objectAtIndex:0] isMemberOfClass:[UIImage class]]) {
                    NSLog(@"UIImage class true");
                }


                UIImage *btnImg = [self.photosArray objectAtIndex:photoCounter];
                //UIImage *btnImg2 = [UIImage imageNamed:@"GameOver"];
                photoCounter++;

                [button setBackgroundImage:btnImg forState:UIControlStateNormal];

                [cell.contentView addSubview:button];
            }
        }
    }else{
        UILabel *noImages = [[UILabel alloc] initWithFrame:CGRectMake(0, 25, 320, 20)];
        noImages.text = @"No images";
        noImages.textAlignment = NSTextAlignmentCenter;
        [cell.contentView addSubview:noImages];
    }

    return cell;
}

第二部分是我将图片加载到“photosArray”中的地方。我正在使用 WSAssetPicker 从我的资产库中加载多张照片。这是代码:

- (void)assetPickerController:(WSAssetPickerController *)sender didFinishPickingMediaWithAssets:(NSArray *)assets
{
    // Dismiss the WSAssetPickerController.
    [self dismissViewControllerAnimated:YES completion:^{

    arep = [[ALAssetRepresentation alloc] init];

        for (ALAsset *asset in assets) {            
            UIImage *imageA = [[UIImage alloc] initWithCGImage:asset.defaultRepresentation.fullScreenImage];

            [self.photosArray addObject:imageA];
        }
        NSLog(@"%d",photosArray.count);

        photoCounter = 0;

        [self.tableView reloadData];
    }];
}

现在对于这个问题,创建的按钮中没有图像。我只得到一个带有边框和圆角的透明按钮。

这是为什么?

4

1 回答 1

1

最近引起我注意的是,我从自述文件(现已更新)中省略了一个重要的部分。

dismissViewControllerAnimated:completion:如果您在完成块中访问 ALAsset 对象,则必须持有 WSAssetPickerController 的强引用。

如果您在调用选择器控制器之前没有对选择器控制器进行强引用,dismissViewControllerAnimated:completion:则将在执行完成块之前释放 ALAssetsLibrary(在 WSAssetPicker 代码中内部使用)在您尝试访问 ALAssets 之前被释放在委托方法assets中传递的数组。assetPickerController:didFinishPickingMediaWithAssets:

如果 ALAssetsLibrary 被释放,则 ALAssetsLibrary 拥有的 ALAssets 将过期并且调用asset.defaultRepresentation.fullScreenImage将返回 null。

以下将在关闭选取器控制器的视图时保留 ALAssetsLibrary:

- (void)assetPickerController:(WSAssetPickerController *)sender didFinishPickingMediaWithAssets:(NSArray *)assets
{
    // Hang on to the picker to avoid ALAssetsLibrary from being released.
    self.picker = sender;

    __block id weakSelf = self; 
    // Note: Instead of `id` you could specify the class of `self` order to access properties with dot notation.

    // Dismiss the WSAssetPickerController.
    [self dismissViewControllerAnimated:YES completion:^{

        // Do something with the assets here.


        // Release the picker.
        [weakSelf setPicker:nil];
    }];
}

另一种方法是在调用之前处理 ALAssets 数组dismissViewControllerAnimated:completion:,但这可能会导致选择器控制器的解除延迟,从而导致糟糕的用户体验。

于 2012-11-27T03:59:36.863 回答