iOS - 多选相册

来源:互联网 发布:linux网络编程看什么书 编辑:程序博客网 时间:2024/05/29 17:43

//

//  FirViewController.m

//  多选Demo

//



#import "ShowViewController.h"

#import "NSObject+YFPhoto.h"

#import "YFShowGroupAlbumVC.h"

#import "ShowCell.h"

#import "YFSelfImage.h"

#import "ALAssetsLibrary+YF.h"

#import <AssetsLibrary/AssetsLibrary.h>

#define K_WIDTH [UIScreen mainScreen].bounds.size.width

#define K_HEIGHT [UIScreen mainScreen].bounds.size.height

static NSString *const FIRCELL = @"ShowCell";

@interface ShowViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate>

{


    NSMutableArray *imageArray;//数组

    YFShowGroupAlbumVC *showVC;//显示相册分组的控制器

    UINavigationController *navShow;//因为相册分组是模态出来的,所以给他一个导航,让相册详细界面可以导航出来

}

@property(nonatomic,strong)showBigImage *showView;//大图

//左边按钮

- (IBAction)clickLeftBtn:(id)sender;

//右边按钮

- (IBAction)clickBtn:(id)sender;

@property (weak,nonatomic) IBOutletUICollectionView *collectionView;


@end


@implementation ShowViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    imageArray = [NSMutableArrayarray];

    showVC = [[YFShowGroupAlbumVCalloc]init];

    navShow = [[UINavigationControlleralloc]initWithRootViewController:showVC];


    [_collectionViewregisterNib:[UINibnibWithNibName:FIRCELLbundle:nil]forCellWithReuseIdentifier:FIRCELL];

    _collectionView.backgroundColor = [UIColorwhiteColor];

    [selfsetCollectionLayOut];

    //接受通知一个是把宰相册详情选择的图片传过来,一个是把相机照的图片传过来

    [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(notShow:)name:@"pushImage"object:nil];

    [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(saveImageSure:)name:@"SAVEIMAGE"object:nil];

}

/**

 *  获得选中的图片数组

 *

 *  @param user <#user description#>

 */

- (void)notShow:(NSNotification *)user{


    [imageArrayremoveAllObjects];

    NSDictionary *dic = user.userInfo;

    [imageArrayaddObjectsFromArray:dic[@"cellImage"]];

    [_collectionViewreloadData];

}

/**

 *  获得相机图片

 *

 *  @param user <#user description#>

 */

- (void)saveImageSure:(NSNotification *)user{


    NSDictionary *dic = user.userInfo;

    YFSelfImage *image = dic[@"saveImage"];

    [imageArrayaddObject:image];

    [_collectionViewreloadData];

}

/**

 *  设置布局

 */

- (void)setCollectionLayOut{


    UICollectionViewFlowLayout *layOut = [[UICollectionViewFlowLayoutalloc]init];

    layOut.itemSize =CGSizeMake(K_WIDTH /3, K_WIDTH /3);

    layOut.minimumLineSpacing =0;

    layOut.minimumInteritemSpacing=0;

    [_collectionViewsetCollectionViewLayout:layOut];

}

/**

 *  左边按钮事件

 *

 *  @param sender <#sender description#>

 */

- (IBAction)clickLeftBtn:(id)sender {


    //弹出选择视图

    UIActionSheet *sheet = [[UIActionSheetalloc]initWithTitle:@"选择方式"delegate:selfcancelButtonTitle:@""destructiveButtonTitle:@""otherButtonTitles:@"",nil];

    [sheet showInView:self.view];


}

/**

 *  右边按钮事件

 *

 *  @param sender <#sender description#>

 */

- (IBAction)clickBtn:(id)sender {

    showVC.showAlbumStyle =ENUM_PhotoAndCamera;

    showVC.albumColor = [UIColorwhiteColor];

    showVC.listCount =4;

    [selfpresentViewController:navShowanimated:YEScompletion:nil];

}

#pragma mark  actionSheet代理

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{


    switch (buttonIndex) {

        case0:

            //打开相机

            [selfshowCamera];

            break;

        case1:

            //打开相册

            showVC.showAlbumStyle =ENUM_AllOfPhoto;

            showVC.albumColor = [UIColorwhiteColor];

            showVC.listCount =5;

            [selfpresentViewController:navShowanimated:YEScompletion:nil];

            break;

        case2:

            NSLog(@"2");

            break;

        default:

            break;

    }

}


#pragma mark   九宫格代理

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{


    returnimageArray.count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{


    ShowCell *cell = [collectionViewdequeueReusableCellWithReuseIdentifier:FIRCELLforIndexPath:indexPath];

    cell.deleBtn.tag = indexPath.row;

    //删除

    __weaktypeof(self)weakSelf =self;

    cell.deleBlock = ^(NSInteger index){

    

       //删除数组中对应的元素

        [imageArrayremoveObjectAtIndex:index];

        [weakSelf.collectionViewreloadData];

    };

    YFSelfImage *image =imageArray[indexPath.row];

    cell.imageView.image = image;

    return cell;

}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{


    YFSelfImage *image =imageArray[indexPath.row];

    //获取图片的详细资源

    ALAssetRepresentation *represention = [image.assetdefaultRepresentation];

    //获取高清图

    UIImage *bigImage = [[UIImagealloc]initWithCGImage:[representionfullResolutionImage]];

    //显示大图

    if (!_showView) {

        _showView = [[showBigImagealloc]initWithFrame:CGRectMake(0,64, K_WIDTH,K_HEIGHT - 64)];

        _showView.bigImageView.image = bigImage;

        _showView.alpha =1.0;

        __weaktypeof(self)weakSelf =self;

        _showView.hiddenBlock = ^{

        

            weakSelf.showView.alpha =0.0;

        };

        [self.viewaddSubview:_showView];

    }else{

    

        _showView.bigImageView.image = bigImage;

        _showView.alpha =1.0;

    }

}

- (void)dealloc{


    [[NSNotificationCenterdefaultCenter]removeObserver:self];

}

@end


/***************************大图显示view******************************/

@implementation showBigImage


- (instancetype)initWithFrame:(CGRect)frame{


    self = [superinitWithFrame:frame];

    if (self) {

        _bigImageView = [[UIImageViewalloc]initWithFrame:self.bounds];

        [selfaddSubview:_bigImageView];

        _bigImageView.userInteractionEnabled =YES;

        UITapGestureRecognizer *tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(hiddenView)];

        [self.bigImageViewaddGestureRecognizer:tap];

    }

    returnself;

}

- (void)hiddenView{


    if (self.hiddenBlock) {

        self.hiddenBlock();

    }

}

@end


0 0
原创粉丝点击