ALAssetsLibrary的使用问题:获取到ALAsset的thumbnail或者其他属性保存数组,在别地地方为nil的问题处理

我们在block方法中通过UIImage *image = [UIImageimageWithCGImage:asset.thumbnail];能拿到图片,但在其他地方再次调用时却为nil了,

解决办法只需添加单例ALAssetsLibrary

- (void)getAllPhoto{
    
    ALAssetsLibraryAccessFailureBlock failureblock =
    ^(NSError *myerror)
    {
        NSLog(@"相册访问失败 =%@", [myerror localizedDescription]);
        if ([myerror.localizedDescription rangeOfString:@"Global denied access"].location!=NSNotFound) {
            NSLog(@"无法访问相册.请在'设置->定位服务'设置为打开状态.");
        }else{
            NSLog(@"相册访问失败.");
        }
    };
    
    NSMutableArray *groupArray = [NSMutableArray array];
    _assetArrays2D = [NSMutableArray array];
    //1
    MyALAssetsLibrary *assetsLibrary = [MyALAssetsLibrary defaultAssetsLibrary];
    [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        
        if (group) {
            [groupArray addObject:group];
//            NSLog(@"%@",group);
            //2
            
            NSMutableArray *assetArrays = [NSMutableArray array];
            [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                if (result) {
//                    NSLog(@"%@",result);
//                    NSString *type = [result valueForProperty:ALAssetPropertyType];
                    if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
                        [assetArrays addObject:result];
                        
                        if (self.headCutImgView.image == nil) {
                            //获取资源图片的高清图
                            ALAssetRepresentation* representation = [result defaultRepresentation];
                            CGImageRef cgImage = [representation fullResolutionImage];
                            UIImage *image = [UIImage imageWithCGImage:cgImage];
                            self.headCutImgView.image = image;
                        }
                    }
                }
            }];
            ///将assetArrays两两分组
            NSMutableArray *assets = nil,*assets2D = [NSMutableArray array];
            for (int i = 0; i < assetArrays.count; i++) {
                if (i%2 == 0) {
                    assets = [NSMutableArray array];
                    [assets2D addObject:assets];
                }
                ALAsset *asset = assetArrays[i];
                [assets addObject:asset];//cell显示时拿不到image不知道怎么回事,后来使MyALAssetsLibrary单例创建后问题解决 见代码
//                [assets addObject:image];
            }
            //
            [_assetArrays2D addObject:assets2D];
        }else{
            [_tableView reloadData];
        }
        
        
    } failureBlock:^(NSError *error) {
        NSLog(@"Group not found!\n");
    }];
    
    
}

出现的问题:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *indentifier = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
        cell.contentView.transform = CGAffineTransformMakeRotation(M_PI_2);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        cell.contentView.backgroundColor = [UIColor blackColor];
        //
        for (int i = 0; i < 2; i++) {
            MyCookbookIconImgButton *imgButton = [[MyCookbookIconImgButton alloc] initWithFrame:CGRectMake(0, middle_img_height*i, middle_img_height, middle_img_height)];
            imgButton.tag = 100+i;
            [imgButton addTarget:self action:@selector(selectedImageViewActions:) forControlEvents:UIControlEventTouchUpInside];
            [cell.contentView addSubview:imgButton];
//            self.headImgView.image = imageView.image;
        }
        
    }
    
    NSMutableArray *assets2D = _assetArrays2D[indexPath.section];
    NSMutableArray *assets = assets2D[indexPath.row];
    for (int i = 0; i < 2; i++) {
        MyCookbookIconImgButton *imgButton = (MyCookbookIconImgButton *)[cell.contentView viewWithTag:100+i];
        
        imgButton.indexPath = indexPath;
        if (self.lastIconImgButton == imgButton && self.lastIconIndexPath.section == indexPath.section && self.lastIconIndexPath.row == indexPath.row) {
            imgButton.layer.borderWidth = 2;
        }else{
            imgButton.layer.borderWidth = 0;
        }
//
        if (i < assets.count) {
            ALAsset *asset = assets[i];
            imgButton.asset = asset;
            UIImage *image = [UIImage imageWithCGImage:asset.thumbnail];
            if (!image) {
                NSLog(@"image is nil! ALAssetsLibrary Game Over?");
            }
            [imgButton setBackgroundImage:image forState:UIControlStateNormal];
        }
        
    }
    
    return cell;
}



<strong><em><span style="font-size:18px;">cell显示时拿不到image不知道怎么回事,后来使MyALAssetsLibrary单例创建后问题解决 见代码</span></em></strong>

@interface MyALAssetsLibrary : ALAssetsLibrary



@end

@implementation MyALAssetsLibrary

+ (MyALAssetsLibrary *)defaultAssetsLibrary {
    static dispatch_once_t pred = 0;
    static MyALAssetsLibrary *library = nil;
    dispatch_once(&pred, ^{
        library = [[MyALAssetsLibrary alloc] init];
    });
    return library;
}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值