实用小技巧(十二):头像图片缩放以及保存到相册简单功能的实现

版本记录

版本号 时间
V1.0 2017.07.02

前言

在app中,我们经常需要点击别人分享发布的图片或者头像,然后放大缩小等,还可以保存到本地相册等。感兴趣的可以看看我写的其他小技巧。
1. 实用小技巧(一):UIScrollView中上下左右滚动方向的判断

2. 实用小技巧(二):屏幕横竖屏的判断和相关逻辑
3.实用小技巧(三):点击手势屏蔽子视图的响应
4.实用小技巧(四):动态的增删标签视图
5.实用小技巧(五):通过相册或者相机更改图标
6.实用小技巧(六):打印ios里所有字体
7. 实用小技巧(七):UITableViewCell自适应行高的计算
8. 实用小技巧(八):数字余额显示的分隔
9.实用小技巧(九):类头条模糊背景的实现
10.实用小技巧(十):晃动手机换后台服务器网络
11.实用小技巧(十一):scrollView及其子类显示的一些异常处理

功能需求

  我们项目中经常需要有这样的需求:需要点击好友或者别人发布的图片,并可捏合放大缩小图片,最后还有保存到本地相册的功能。

功能实现

下面我们就直接看代码实现。

#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

功能效果

下面就运行代码,并查看结果。

实用小技巧(十二):头像图片缩放以及保存到相册简单功能的实现_第1张图片
效果1
实用小技巧(十二):头像图片缩放以及保存到相册简单功能的实现_第2张图片
效果2
实用小技巧(十二):头像图片缩放以及保存到相册简单功能的实现_第3张图片
效果3
实用小技巧(十二):头像图片缩放以及保存到相册简单功能的实现_第4张图片
效果4
实用小技巧(十二):头像图片缩放以及保存到相册简单功能的实现_第5张图片
效果5
实用小技巧(十二):头像图片缩放以及保存到相册简单功能的实现_第6张图片
效果6
实用小技巧(十二):头像图片缩放以及保存到相册简单功能的实现_第7张图片
效果7

可见实现了需求功能。

后记

未完,待续~~~

实用小技巧(十二):头像图片缩放以及保存到相册简单功能的实现_第8张图片
心白

你可能感兴趣的:(实用小技巧(十二):头像图片缩放以及保存到相册简单功能的实现)