你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發基礎 >> 頭像點擊查看大圖和保存功能實現(保存的細節處理)

頭像點擊查看大圖和保存功能實現(保存的細節處理)

編輯:IOS開發基礎

原文

  • code example

  • 分類處理,百行代碼.(提示的文本控件,替換為當前項目的HUB即可),方便使用.

點擊查看他人頭像

  • 點擊後呈現,系統動畫漸變過度

  • 點擊圖片或者背景,漸變消失

保存頭像圖片的處理

  • 當前用戶已給予訪問權限,直接保存.

  • 當前用戶未給予訪問權限,告知暫無權限訪問您的相冊.

  • 當前用戶第一次,系統級別的對話框彈出,用戶點擊不,則不保存,點擊了允許,則保存照片,不需要用戶給予權限後再次點擊保存.

how to user

Swift:

avatarImageView = UIImageView()
avatarImageView.LFLHeadimageBrowser() 

Objective-C:

avatarImageView = [UIImageView new];
[avatarImageView LFLHeadimageBrowser];

點擊查看大圖部分

#import "UIImageView+LFLHelper.h"
#import #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];
        });
    }];
}
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved