實用小技巧(十二):頭像圖片縮放以及保存到相冊簡單功能的實現(xiàn)

版本記錄

版本號 時間
V1.0 2017.07.02

前言

在app中,我們經(jīng)常需要點擊別人分享發(fā)布的圖片或者頭像,然后放大縮小等,還可以保存到本地相冊等。感興趣的可以看看我寫的其他小技巧。
1. 實用小技巧(一):UIScrollView中上下左右滾動方向的判斷

2. 實用小技巧(二):屏幕橫豎屏的判斷和相關(guān)邏輯
3.實用小技巧(三):點擊手勢屏蔽子視圖的響應(yīng)
4.實用小技巧(四):動態(tài)的增刪標(biāo)簽視圖
5.實用小技巧(五):通過相冊或者相機(jī)更改圖標(biāo)
6.實用小技巧(六):打印ios里所有字體
7. 實用小技巧(七):UITableViewCell自適應(yīng)行高的計算
8. 實用小技巧(八):數(shù)字余額顯示的分隔
9.實用小技巧(九):類頭條模糊背景的實現(xiàn)
10.實用小技巧(十):晃動手機(jī)換后臺服務(wù)器網(wǎng)絡(luò)
11.實用小技巧(十一):scrollView及其子類顯示的一些異常處理

功能需求

??我們項目中經(jīng)常需要有這樣的需求:需要點擊好友或者別人發(fā)布的圖片,并可捏合放大縮小圖片,最后還有保存到本地相冊的功能。

功能實現(xiàn)

下面我們就直接看代碼實現(xiàn)。

#import "JJAvatarSaveVC.h"
#import "Masonry.h"

@interface JJAvatarSaveVC ()

@property (nonatomic, strong) UIImageView *avatarImageView;
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, assign) double lastScale;

@end

@implementation JJAvatarSaveVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setupUI];
}

- (void)setupUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    //頭像
    UIImageView *avatarImageView = [[UIImageView alloc] init];
    avatarImageView.image = [UIImage imageNamed:@"sea"];
    avatarImageView.userInteractionEnabled = YES;
    avatarImageView.layer.cornerRadius = 50.0;
    avatarImageView.layer.masksToBounds = YES;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureDidTapped)];
    [avatarImageView addGestureRecognizer:tapGesture];
    [self.view addSubview:avatarImageView];
    self.avatarImageView = avatarImageView;
    
    [self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.height.width.equalTo(@100);
    }];
}

- (void)loadBackgroundView
{
    UIView *backView = [[UIView alloc] initWithFrame:self.view.frame];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backViewTapGestureDidTapped)];
        [backView addGestureRecognizer:tapGesture];
    UIPinchGestureRecognizer *pinGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(imagePinGestureDidTapped:)];
        [backView addGestureRecognizer:pinGesture];
    UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(imageLongGestureDidTapped)];
        [backView addGestureRecognizer:longGesture];

    backView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:backView];
    self.backView = backView;
    
    CGSize imageSize = self.avatarImageView.image.size;
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:@"sea"];
    imageView.userInteractionEnabled = YES;
    [backView addSubview:imageView];
    self.imageView = imageView;
    
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = imageSize.height / imageSize.width * width;
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(backView);
        make.width.equalTo(@(width));
        make.height.equalTo(@(height));
    }];
}

- (void)saveImageToAlbum
{
    UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}

- (void)image: (UIImage *) image didFinishSavingWithError:(NSError *) error contextInfo:(void *) contextInfo
{
    NSString *msg = nil;
    
    if (error != NULL) {
        msg = @"保存圖片失敗";
    }
    else {
        msg = @"保存圖片成功";
    }
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *ensureAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self.backView removeFromSuperview];
    }];
    
    [alertVC addAction:ensureAction];
    
    [self presentViewController:alertVC animated:YES completion:nil];
}

#pragma mark - Action && Notification

- (void)tapGestureDidTapped
{
    self.navigationController.navigationBarHidden = YES;
    [UIApplication sharedApplication].statusBarHidden = YES;
    [self loadBackgroundView];
}

- (void)backViewTapGestureDidTapped
{
    self.navigationController.navigationBarHidden = NO;
    [UIApplication sharedApplication].statusBarHidden = NO;
    [self.backView removeFromSuperview];
}

//捏合手勢放大縮小圖片

- (void)imagePinGestureDidTapped:(UIPinchGestureRecognizer *)sender
{
    
    if([sender state] == UIGestureRecognizerStateEnded) {
        self.lastScale = 1.0;
        return;
    }
    
    CGFloat scale = 1.0 - (self.lastScale - sender.scale);
    CGAffineTransform currentTransform = self.imageView.transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
    
    [self.imageView setTransform:newTransform];
    self.lastScale = [sender scale];
}

//長按保存到相冊

- (void)imageLongGestureDidTapped
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *saveAction = [UIAlertAction actionWithTitle:@"保存到相冊" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self saveImageToAlbum];
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alertVC addAction:saveAction];
    [alertVC addAction:cancelAction];
    
    [self presentViewController:alertVC animated:YES completion:nil];
}

@end

功能效果

下面就運行代碼,并查看結(jié)果。

效果1
效果2
效果3
效果4
效果5
效果6
效果7

可見實現(xiàn)了需求功能。

后記

未完,待續(xù)~~~

心白
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容