GiGaMaskTime/GIGA/Modules/Mask/Controller/ShareViewController.m

280 lines
11 KiB
Objective-C

//
// ShareViewController.m
// GIGA
//
// Created by lianxiang on 2018/9/21.
// Copyright © 2018年 com.giga.ios. All rights reserved.
//
#import "ShareViewController.h"
#import "MaskTimeShareViewCell.h"
#import "MaskShareTimeMidViewCell.h"
#import "MaskShareBoootomViewCell.h"
#import "MaskTimeShareActionSheet.h"
#import "GIGaUserFileHelper.h"
#import <AVFoundation/AVFoundation.h>
@interface ShareViewController ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property(nonatomic,strong) UITableView *tab;
@property(nonatomic,strong) UIImageView *backimageview ;
@property(nonatomic) int curentImageIndex ;
@end
@implementation ShareViewController
- (void)viewDidLoad {
[super viewDidLoad];
_curentImageIndex = 0;
UIImageView *backimageview = [[UIImageView alloc] init];
backimageview.contentMode = UIViewContentModeScaleAspectFill;
backimageview.frame = CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height);
backimageview.userInteractionEnabled = YES;
backimageview.image = [GIGaUserFileHelper getUserShareImage:kUSERSHAREA_IMAGENAME];
// self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"sharebg_1"]];
self.backimageview = backimageview;
[self.view addSubview:backimageview];
[self creatTabUI];
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
backBtn.frame = CGRectMake(20,44, 40, 40);
[backBtn setImage:[UIImage imageNamed:@"btn_moment_back"] forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(backBtnAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backBtn];
[self.view insertSubview:backimageview atIndex:0];
[self creatBtns];
}
-(void)creatTabUI{
self.tab = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.tab.separatorStyle = UITableViewCellSelectionStyleNone;
self.tab.delegate = self;
self.tab.dataSource = self;
self.tab.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.tab];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 0) {
MaskTimeShareViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MaskTimeShareViewCell"];
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"MaskTimeShareViewCell" owner:self options:nil] lastObject];
}
return cell;
}else if (indexPath.row == 1){
MaskShareTimeMidViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MaskShareTimeMidViewCell"];
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"MaskShareTimeMidViewCell" owner:self options:nil] lastObject];
}
return cell;
}
else if (indexPath.row == 2){
MaskShareBoootomViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MaskShareBoootomViewCell"];
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"MaskShareBoootomViewCell" owner:self options:nil] lastObject];
}
return cell;
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 0) {
return 295;
}else if (indexPath.row ==1){
return 205;
}
return 158;
}
-(void)backBtnAction{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)creatBtns{
UIButton *changeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
changeBtn.backgroundColor = GIGARGB(181, 14, 14, 0.6);
NSAttributedString *titl = [GiGaHelper stringWithText:@"更换背景" textColor:[UIColor whiteColor] textFont:GIGA_TEXTFONTBOLD(18) leterSpace:0];
[changeBtn setAttributedTitle:titl forState:UIControlStateNormal];
[changeBtn addTarget:self action:@selector(changeAction) forControlEvents:UIControlEventTouchUpInside];
changeBtn.layer.masksToBounds = YES;
changeBtn.layer.cornerRadius = 4;
[self.view addSubview:changeBtn];
[changeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(120, 40));
make.bottom.mas_equalTo(self.view.mas_bottom).mas_offset(-45);
make.centerX.mas_equalTo(self.view.mas_centerX).offset(-72);
}];
UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
shareBtn.backgroundColor = GIGARGB(181, 14, 14, 0.6);
NSAttributedString *titl2 = [GiGaHelper stringWithText:@"分享" textColor:[UIColor whiteColor] textFont:GIGA_TEXTFONTBOLD(18) leterSpace:0];
[shareBtn setAttributedTitle:titl2 forState:UIControlStateNormal];
[shareBtn addTarget:self action:@selector(shareBtnAction) forControlEvents:UIControlEventTouchUpInside];
shareBtn.layer.masksToBounds = YES;
shareBtn.layer.cornerRadius = 4;
[self.view addSubview:shareBtn];
[shareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(120, 40));
make.bottom.mas_equalTo(self.view.mas_bottom).mas_offset(-45);
make.centerX.mas_equalTo(self.view.mas_centerX).offset(72);
}];
}
-(void)changeAction{
MaskTimeShareActionSheet * sheet = [[MaskTimeShareActionSheet alloc] initWithFrame:CGRectMake(0, 0, KMainW, KMainH)];
[sheet show:^(NSUInteger index) {
switch (index) {
case 0:
{//手机
[self photosAlbum];
}
break;
case 1:
{
//系统
self->_curentImageIndex ++;
if (self->_curentImageIndex == 4) {
self->_curentImageIndex = 0;
}
NSArray *images = @[@"sharebg_2",@"sharebg_3",@"sharebg_4",@"sharebg_5",@"sharebg_1"];
[self.backimageview setImage:[UIImage imageNamed: images[self->_curentImageIndex]]];
}
break;
case 2:
{
//自拍
[self takePhoto];
}
break;
default:
break;
}
}];
[self.view addSubview:sheet];
}
#pragma mark 相册
-(void)photosAlbum{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.delegate = self; imagePickerController.allowsEditing = YES;
//相册
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
//权限
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePickerController animated:YES completion:nil];
}else{
NSString *errorStr = @"应用相权册限受限,请在设置中启用";
[self jxt_showAlertWithTitle:@"" message:errorStr appearanceProcess:^(JXTAlertController * _Nonnull alertMaker) {
alertMaker.addActionCancelTitle(@"取消");
alertMaker.addActionDefaultTitle(@"去开启");
} actionsBlock:^(NSInteger buttonIndex, UIAlertAction * _Nonnull action, JXTAlertController * _Nonnull alertSelf) {
if (buttonIndex == 1) {
NSURL *url = [NSURL URLWithString:@"prefs:root=Photos"];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
}
}];
}
}
-(void)takePhoto{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.delegate = self; imagePickerController.allowsEditing = YES;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
//权限判断应用是否有使用相机的权限
NSString *mediaType = AVMediaTypeVideo;//读取媒体类型
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];//读取设备授权状态
if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){
NSString *errorStr = @"应用相机权限受限,请在设置中启用";
[self jxt_showAlertWithTitle:@"" message:errorStr appearanceProcess:^(JXTAlertController * _Nonnull alertMaker) {
alertMaker.addActionCancelTitle(@"取消");
alertMaker.addActionDefaultTitle(@"去开启");
} actionsBlock:^(NSInteger buttonIndex, UIAlertAction * _Nonnull action, JXTAlertController * _Nonnull alertSelf) {
if (buttonIndex == 1) {
NSString *urlStr = @"App-Prefs:root=Photos";
if (@available(iOS 10.0, *)) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr] options:@{} completionHandler:nil];
} else {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];
// Fallback on earlier versions
}
}
}];
return;
}
// 设置数据源
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imagePickerController animated:YES completion:nil];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
GILog(@"info:%@",info);
[picker dismissViewControllerAnimated:YES completion:nil]
;
UIImage*img=[info objectForKey:UIImagePickerControllerEditedImage];
UIImage *small = [GIGaUserFileHelper zipScaleWithImage:img];
[GIGaUserFileHelper savaShareImange:kUSERSHAREA_IMAGENAME image:small];
ShareViewController *share = [[ShareViewController alloc] init];
[self presentViewController:share animated:NO completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissViewControllerAnimated:YES completion:nil];
}
-(void)shareBtnAction{
}
@end