頭像點選檢視大圖和儲存功能實現
- code example
- 分類處理,百行程式碼.(提示的文字控制元件,替換為當前專案的HUB即可),方便使用.
點選檢視他人頭像
- 點選後呈現,系統動畫漸變過度
- 點選圖片或者背景,漸變消失
儲存頭像圖片的處理
- 當前使用者已給予訪問許可權,直接儲存.
- 當前使用者未給予訪問許可權,告知暫無許可權訪問您的相簿.
- 當前使用者第一次,系統級別的對話方塊彈出,使用者點選不,則不儲存,點選了允許,則儲存照片,不需要使用者給予許可權後再次點選儲存.
how to user
Swift:
avatarImageView = UIImageView()
avatarImageView.LFLHeadimageBrowser()
Objective-C:
avatarImageView = [UIImageView new];
[avatarImageView LFLHeadimageBrowser];複製程式碼
點選檢視大圖部分
#import "UIImageView+LFLHelper.h"
#import <Photos/Photos.h>
#define LFLANIMATEDURATION 0.3
static 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];
});
}];
}複製程式碼