我的代码分为两部分:
第一个是我的表视图的"cellForRowAtIndexPath“方法。在这个表视图中,我应该在每个单元格中显示3个按钮,每个按钮都是一个不同的图像。图像位于名为"photosArray“的NSMutableArray中。它看起来像这样。
-(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];
}];
}
现在,对于这个问题,创建的按钮中没有图像。我只得到了一个带有边框和圆角的透明按钮。
为什么会这样呢?
发布于 2012-11-27 11:59:37
我最近注意到,我在自述文件(现已更新)中遗漏了一项重要内容。
如果要访问WSAssetPickerController完成块中的ALAsset对象,则必须持有dismissViewControllerAnimated:completion:
的强引用。
如果在调用dismissViewControllerAnimated:completion:
之前没有持有对选取器控制器的强引用,则将在执行完成块之前释放选取器控制器,从而导致ALAssetsLibrary (在WSAssetPicker代码内部使用)在您尝试访问传递给assetPickerController:didFinishPickingMediaWithAssets:
委托方法的assets
数组中的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数组,但这可能会导致选择器控制器的解除延迟,从而导致用户体验不佳。
https://stackoverflow.com/questions/13571475
复制相似问题