#import "ViewController.h"
#import "WPhotoViewController.h"
@interface ViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height
{
BOOLisFullScreen;
UIButton*_addBut;
UITableView*_tableView;
NSMutableArray*_photosArr;
NSArray*arr;
//网格
UICollectionViewFlowLayout *layout;
UICollectionView *tv;
}
@property(nonatomic,strong)UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//背景颜色
self.view.backgroundColor = [UIColor whiteColor];
//导航条颜色
self.navigationController.navigationBar.backgroundColor = [UIColor whiteColor];
//标题
self.navigationItem.title = @"日常练习";
[self createTableView];
_imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100,100, 200, 200)];
_imageView.backgroundColor = [UIColor cyanColor];
_imageView.layer.cornerRadius = 100;
_imageView.layer.masksToBounds = YES;
[self.view addSubview:_imageView];
//创建按钮
UIButton *chooseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
chooseBtn.frame=CGRectMake(155,100,100,40);
[chooseBtnaddTarget:self action:@selector(chooseImage:) forControlEvents:UIControlEventTouchUpInside];
[chooseBtnsetBackgroundColor:[UIColor brownColor]];
[chooseBtnsetTitle:@"选择照片"forState:UIControlStateNormal];
[self.viewaddSubview:chooseBtn];
UIButton *btnnn= [[UIButton alloc]initWithFrame:CGRectMake(150, 330, 100, 50)];
[btnnnsetTitle:@"上传" forState:UIControlStateNormal];
btnnn.backgroundColor = [UIColor blueColor];
[btnnnaddTarget:self action:@selector(Click) forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:btnnn];
}
#pragma mark - 保存图片至沙盒
- (void) saveImage:(UIImage*)currentImage withName:(NSString*)imageName
{
NSData*imageData =UIImageJPEGRepresentation(currentImage,0.5);
// 获取沙盒目录
NSString *fullPath = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:imageName];
// 将图片写入文件
[imageDatawriteToFile:fullPathatomically:NO];
}
#pragma mark - image picker delegte
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
[pickerdismissViewControllerAnimated:YES completion:^{}];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self saveImage:image withName:@"currentImage.png"];
NSString *fullPath = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"currentImage.png"];
UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:fullPath];
isFullScreen =NO;
[self.imageViewsetImage:savedImage];
self.imageView.tag =100;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker
{
[self dismissViewControllerAnimated:YES completion:^{}];
}
#pragma mark - actionsheet delegate
-(void) actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(actionSheet.tag==255)
{
NSUIntegersourceType =0;
// 判断是否支持相机
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
switch(buttonIndex)
{
case0:
// 取消
return;
case1:
// 相机
sourceType =UIImagePickerControllerSourceTypeCamera;
break;
case2:
// 相册
UIImagePickerControllerSourceTypePhotoLibrary;
break;
}
}
else
{
if(buttonIndex ==0)
{
return;
}
else{
sourceType =UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
}
// 跳转到相机或相册页面
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate=self;
imagePickerController.allowsEditing=YES;
imagePickerController.sourceType= sourceType;
[selfpresentViewController:imagePickerControlleranimated:YEScompletion:^{}];
}
}
- (void)chooseImage:(UIButton*)btn
{
UIActionSheet *sheet;
// 判断是否支持相机
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
sheet= [[UIActionSheet alloc]initWithTitle:@"选择"delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消"otherButtonTitles:@"拍照",@"从相册选择",nil];
}
else{
sheet = [[UIActionSheet alloc] initWithTitle:@"选择"delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消"otherButtonTitles:@"从相册选择",nil];
}
sheet.tag=255;
[sheetshowInView:self.view];
}
//上传多个照片按钮方法
-(void)Click
{
WPhotoViewController *WphotoVC = [[WPhotoViewController alloc] init];
//选择图片的最大数
WphotoVC.selectPhotoOfMax=9;
[WphotoVCsetSelectPhotosBack:^(NSMutableArray*phostsArr) {
self->_photosArr= phostsArr;
[self->tvreloadData];
}];
[self presentViewController:WphotoVC animated:YES completion:nil];
}
-(void)createTableView
{
//流水布局
layout = [[UICollectionViewFlowLayout alloc]init];
//单元格大小
layout.itemSize = CGSizeMake(90, 90);
//创建网格
tv = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 300, self.view.frame.size.width, 400) collectionViewLayout:layout];
//网格的背景色
tv.backgroundColor = [UIColor yellowColor];
//注册cell
[tv registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
//代理
tv.delegate=self;
tv.dataSource = self;
//将网格加入主视图
[self.view addSubview:tv];
}
////分几组(网格)
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView
{
return 1;
}
//一组有几行(网格)
-(NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section
{
return _photosArr.count;
}
//展示网格每行的数据(网格)
-(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
//重用标识符
UICollectionViewCell *cell = (UICollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
//装图片的那个框的颜色
cell.backgroundColor = [UIColor cyanColor];
UIImageView *imageView = [[UIImageView alloc] init];
imageView.frame=CGRectMake(0,0,90,90);
imageView.image= [[_photosArrobjectAtIndex:indexPath.item]objectForKey:@"image"];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.layer.masksToBounds=YES;
[celladdSubview:imageView];
returncell;
}
@end
网友评论