ALAsset图像问题

时间:2012-11-26 19:12:21

标签: iphone xcode

我的代码分为两部分:

第一个是我的表视图的“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];
    }];
}

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

为什么?

1 个答案:

答案 0 :(得分:1)

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

如果要访问dismissViewControllerAnimated:completion:完成块中的ALAsset对象,则必须保留WSAssetPickerController的强引用。

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

如果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];
    }];
}

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