ios 相册 look

#import "ViewController.h"
#import "SelectPhotoManager.h"
#import "WPhotoViewController.h"

#define phoneScale [UIScreen mainScreen].bounds.size.width/720.0
@interface ViewController ()
{
    UICollectionView *_collectionView;
    UICollectionViewFlowLayout * _layout;
    NSMutableArray *_photosArr;
}
@property (weak, nonatomic) IBOutlet UIImageView *headerImage;

@property (nonatomic, strong)SelectPhotoManager *photoManager;

@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self setHead];
    
    _layout=[[UICollectionViewFlowLayout alloc]init];
    
    _collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 350, self.view.frame.size.width, 400) collectionViewLayout:_layout];
    
    _collectionView.backgroundColor=[UIColor whiteColor];
    _collectionView.dataSource=self;
    _collectionView.delegate=self;
//    _collectionView.minimumZoomScale = 5;
    
    _layout.itemSize=CGSizeMake(100, 100);
    //
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellId"];
    [self.view addSubview:_collectionView];
    
}
- (IBAction)btn1:(id)sender {
    if (!_photoManager) {
        _photoManager =[[SelectPhotoManager alloc]init];
    }
    [_photoManager startSelectPhotoWithImageName:@"选择头像"];
    __weak typeof(self)mySelf=self;
    //选取照片成功
    _photoManager.successHandle=^(SelectPhotoManager *manager,UIImage *image){
        
        mySelf.headerImage.image = image;
        //保存到本地
        NSData *data = UIImagePNGRepresentation(image);
        [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"headerImage"];
    };
}

-(void)setHead{
    _headerImage.userInteractionEnabled=YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
    [_headerImage addGestureRecognizer:tap];
    //这里是从本地取的,如果是上线项目一定要从服务器取头像地址加载
    UIImage *img = [UIImage imageWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"headerImage"]];
    if (img) {
        _headerImage.image = img;
    }
}
    
   
- (IBAction)btn2:(id)sender {

}
- (IBAction)btn3:(id)sender {
}
- (IBAction)btn4:(id)sender {
    
    WPhotoViewController *WphotoVC = [[WPhotoViewController alloc] init];
    //选择图片的最大数
    WphotoVC.selectPhotoOfMax = 9;
    [WphotoVC setSelectPhotosBack:^(NSMutableArray *phostsArr) {
        self->_photosArr = phostsArr;
        [self->_collectionView reloadData];
    }];
    [self presentViewController:WphotoVC animated:YES completion:nil];
}

#pragma mark collectionView代理方法
//返回section个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
    
}
//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _photosArr.count;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = (UICollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor yellowColor];
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.frame = CGRectMake(0, 0, 100, 100);
    imageView.image = [[_photosArr objectAtIndex:indexPath.row] objectForKey:@"image"];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.layer.masksToBounds = YES;
    [cell addSubview:imageView];
    
            return cell;
}
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
//    return CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width);
//}
@end
屏幕快照 2018-10-22 上午7.59.43.png

你可能感兴趣的:(ios 相册 look)