参考微信自定制相册功能

来源:互联网 发布:萌安安ann微博全数据 编辑:程序博客网 时间:2024/05/03 06:54

需要选择单张图片,可以直接调用imagePickerViewController

但是往往我们需要同时上传很多张

我们公司的系统要求是iOS8.0以上都支持,所以就基于AssetsLibrary自己做了一个

先气看代码麻烦可以直接看我GitHub上的demo,编码辛苦,觉得有用还望给个star以作鼓励

https://github.com/gofey/PhoneAlum


效果如下







必须导入

#import <AssetsLibrary/AssetsLibrary.h>


GFPhotoAlumController.m文件


先遍历所有相册分组 

   //self.assetLib是一个可变数组,将照片数大于0的ALAssetsGroup放进去

   [self.assetLibenumerateGroupsWithTypes:ALAssetsGroupAllusingBlock:^(ALAssetsGroup *group,BOOL *stop) {

        if (group) {

            [group setAssetsFilter:[ALAssetsFilterallPhotos]];

            if (group.numberOfAssets >0) {

                [self.assetGroupArrayaddObject:group];

            }

        }else{

            if (self.assetGroupArray.count > 0) {

                [self.tableViewreloadData];

                

            }else{

                //no photo

            }

            

        }

    } failureBlock:^(NSError *error) {

        NSLog(@"enumerateGroupsError:%@",error);

    }];


在tableView的cell中显示

    UITableViewCell *cell =       [tableView dequeueReusableCellWithIdentifier:_reuseIdentifier];

    ALAssetsGroup *group =self.assetGroupArray[indexPath.row];

    cell.imageView.clipsToBounds =YES;

    cell.imageView.contentMode =UIViewContentModeScaleAspectFill;

    cell.imageView.image = [UIImageimageWithCGImage:group.posterImage];

    cell.textLabel.text = [NSStringstringWithFormat:@"%@    %@", [groupvalueForProperty:ALAssetsGroupPropertyName],[NSStringstringWithFormat:@"(%ld)", (long)group.numberOfAssets]];

    return cell;



GFPhotosController.m文件


由于系统ALAsset是没有这个照片是否被选中状态的,所以需要自己做一个

如下


@interface GFAsset :NSObject

@property(nonatomic)BOOL isSelected;

@property(nonatomic,strong)ALAsset *asset;


- (GFAsset *)initWithAsset:(ALAsset *)asset;


+ (GFAsset *)assetWithAsset:(ALAsset *)asset;



@end



选择的group

遍历Group,获取GFAsset

   [group enumerateAssetsWithOptions:NSEnumerationReverseusingBlock:^(ALAsset *result,NSUInteger index, BOOL *stop) {

        if (result) {

            

            [self.assetArrayaddObject:[GFAssetassetWithAsset:result]];

        }else{

            if (self.assetArray.count == 0) {

                NSLog(@" no photo");

            }

            [self.collectionViewreloadData];

        }

    }];

将照片付给imageView

   self.photoImgView.image = [UIImageimageWithCGImage:asset.asset.aspectRatioThumbnail];


当然这只是展示照片的主要代码,详细逻辑和内部实现,以及其他操作,请参考https://github.com/gofey/PhoneAlum


1 0
原创粉丝点击