博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
头像点击查看大图和保存功能实现(保存的细节处理)
阅读量:6955 次
发布时间:2019-06-27

本文共 5302 字,大约阅读时间需要 17 分钟。

头像点击查看大图和保存功能实现

  • 分类处理,百行代码.(提示的文本控件,替换为当前项目的HUB即可),方便使用.

点击查看他人头像

  • 点击后呈现,系统动画渐变过度
  • 点击图片或者背景,渐变消失

保存头像图片的处理

  • 当前用户已给予访问权限,直接保存.
  • 当前用户未给予访问权限,告知暂无权限访问您的相册.
  • 当前用户第一次,系统级别的对话框弹出,用户点击不,则不保存,点击了允许,则保存照片,不需要用户给予权限后再次点击保存.

how to user

Swift:avatarImageView = UIImageView()avatarImageView.LFLHeadimageBrowser() Objective-C:avatarImageView = [UIImageView new];[avatarImageView LFLHeadimageBrowser];复制代码

点击查看大图部分

#import "UIImageView+LFLHelper.h"#import 
#define LFLANIMATEDURATION 0.3static CGRect originFrame;@implementation UIImageView (LFLHelper)- (void)LFLHeadimageBrowser{ self.userInteractionEnabled = YES; [self addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapHeadimageHandle)]];}- (void)tapHeadimageHandle{ UIWindow *window = [UIApplication sharedApplication].keyWindow; UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)]; originFrame = [self convertRect:self.bounds toView:window]; NSLog(@"oldframe%@",NSStringFromCGRect(originFrame)); backgroundView.backgroundColor =[UIColor blackColor]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:originFrame]; imageView.tag = 1111; [imageView setImage:self.image]; UIButton *saveButton = [[UIButton alloc] init]; [saveButton setTitle:@"保存" forState:UIControlStateNormal]; [saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; saveButton.backgroundColor = [UIColor colorWithRed:0.1f green:0.1f blue:0.1f alpha:0.90f]; saveButton.layer.cornerRadius = 5; saveButton.titleLabel.font = [UIFont systemFontOfSize:16.0]; saveButton.clipsToBounds = YES; saveButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2 - 25, [UIScreen mainScreen].bounds.size.height - 60, 50, 25); [saveButton addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(saveCurrentImageClick)]]; [backgroundView addSubview:saveButton]; [backgroundView addSubview:imageView]; [window addSubview:backgroundView]; [backgroundView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissAction:)]]; // view big eadimageBrowser [UIView animateWithDuration:LFLANIMATEDURATION animations:^{ CGFloat y,width,height; y = ([UIScreen mainScreen].bounds.size.height - self.image.size.height * [UIScreen mainScreen].bounds.size.width / self.image.size.width) * 0.5; width = [UIScreen mainScreen].bounds.size.width; height = self.image.size.height * [UIScreen mainScreen].bounds.size.width / self.image.size.width; [imageView setFrame:CGRectMake(0, y, width, height)]; } completion:nil];}- (void)dismissAction:(UITapGestureRecognizer *)tapRecognizer{ UIView *backgroundView= tapRecognizer.view; [UIView animateWithDuration:LFLANIMATEDURATION animations:^{ [[backgroundView viewWithTag:1111] setFrame:originFrame]; } completion:^(BOOL finished) { [backgroundView removeFromSuperview]; }];}复制代码

保存图片

  • 如果针对第三种情况再写保存代码有重复,所以我采取了递归处理,避免了重复代码的出现,考虑到实际情况(因为系统框询问只弹出一次),递归一次就终结.
- (void)saveCurrentImageClick{    __weak typeof(self) weakSelf = self;    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{        PHAssetChangeRequest *req = [PHAssetChangeRequest creationRequestForAssetFromImage:weakSelf.image];        req = nil;    } completionHandler:^(BOOL success, NSError * _Nullable error) {        dispatch_async(dispatch_get_main_queue(), ^{            // tips message            UIWindow *window = [UIApplication sharedApplication].keyWindow;            UILabel *tipsLabel = [[UILabel alloc] init];            tipsLabel.textColor = [UIColor whiteColor];            tipsLabel.backgroundColor = [UIColor colorWithRed:0.1f green:0.1f blue:0.1f alpha:0.90f];            tipsLabel.layer.cornerRadius = 5;            tipsLabel.clipsToBounds = YES;            tipsLabel.bounds = CGRectMake(0, 0, 200, 30);            tipsLabel.center = window.center;            tipsLabel.textAlignment = NSTextAlignmentCenter;            tipsLabel.font = [UIFont boldSystemFontOfSize:17];            [window addSubview:tipsLabel];            [window bringSubviewToFront:tipsLabel];            if (success) {                tipsLabel.text = @"保存成功!";            }else{                if ([PHPhotoLibrary authorizationStatus] == PHAuthorizationStatusAuthorized) {                    tipsLabel.text = @"保存成功!";                }else{                // 处理第三种情况,监听用户第一次授权情况                          if ([PHPhotoLibrary authorizationStatus] == PHAuthorizationStatusNotDetermined) {                        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {                            if (status == PHAuthorizationStatusAuthorized) {                            // 递归处理一次 , 因为系统框只弹出这一次                                [weakSelf saveCurrentImageClick];                                return ;                            }                        }];                    }else{                        tipsLabel.text = @"暂无权限访问您的相册!";                    }                }            }            [tipsLabel performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1.0];        });    }];}复制代码

转载地址:http://ewxil.baihongyu.com/

你可能感兴趣的文章
我的mysql的学习记录
查看>>
Codeforces Round #416 (Div. 2)(A,思维题,暴力,B,思维题,暴力)
查看>>
CTF---密码学入门第七题 杯酒人生
查看>>
NYOJ 题目77 开灯问题(简单模拟)
查看>>
在MVC中进行排序
查看>>
IOS开发-几种截屏方法
查看>>
如何使用cocos2d制作基于tile地图的游戏教程:第一部分
查看>>
模式识别
查看>>
设置文本编辑器的按回车时触发的事件
查看>>
关于android手机不能打印Log日志
查看>>
hdu 2157 How many ways?? **
查看>>
xcode8 更新cocoapods
查看>>
习题:海盗船(广搜)
查看>>
auto_ptr
查看>>
机器学习模型 bias 和 variance 的直观判断
查看>>
Windows系统的消息机制
查看>>
asp.net中textbox获得焦点后清空默认文本
查看>>
第二阶段冲刺第三天,6月2日。
查看>>
博文分类索引--Python
查看>>
nginx 快速查看配置文件的方法
查看>>