iOS实现点击图片放大和长按保存图片的示例

 更新时间:2018年03月06日 16:02:50   作者:FBY展菲  
本篇文章主要介绍了iOS实现点击图片放大和长按保存图片的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

一:简介

在项目中免不了会遇到,实名认证上传身份证、绑定银行卡等功能。在实际操作中呢,会涉及到上传图片,在页面布局时,可能图片不是一张,考虑到布局的美观等因素,显示图片的位置变得很小,如果想查看上传的图片是否清晰,内容是否完整,可能就需要放大才能实现,下面就和大家分享一下我封装的一类,完美的实现了图片的缩放功能。

另外,这些博文都是来源于我日常开发中的技术总结,在时间允许的情况下,我会针对技术点分别分享iOS、Android两个版本,尽量附上demo以供大家参考,如果有其他技术点需要,可在文章后留言,我会尽全力帮助大家。

二:实现思路分析

  1. 给UIImageView添加手势
  2. 封装一个继承NSObject的FBYImageZoom类
  3. 写一个函数用来接收出入的UIImageView
  4. 根据传入的UIImageView重新绘制在Window中
  5. 添加放大后背景视图的颜色和透明度
  6. 使用动画放大展示ImageView
  7. 添加恢复ImageView原始尺寸的tap点击事件
  8. 完成之后将背景视图删掉

三:实现源码分析

根据实现思路分析,一步步进行编码实现:

1. 给UIImageView添加手势

self.myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 150, SCREEN_WIDTH-100, SCREEN_WIDTH-100)];
self.myImageView.image = [UIImage imageNamed:@"bankcard"];
//添加点击事件
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scanBigImageClick:)];
[_myImageView addGestureRecognizer:tapGestureRecognizer];
[_myImageView setUserInteractionEnabled:YES];
[self.view addSubview:_myImageView];

2. 封装一个继承NSObject的FBYImageZoom类

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface FBYImageZoom : NSObject
@end

3. 写一个函数用来接收出入的UIImageView

/**
 * @param contentImageview 图片所在的imageView
 */
+(void)ImageZoomWithImageView:(UIImageView *)contentImageview;

4. 根据传入的UIImageView重新绘制在Window中

+(void)ImageZoomWithImageView:(UIImageView *)contentImageview{  
  UIWindow *window = [UIApplication sharedApplication].keyWindow;
  [self scanBigImageWithImage:contentImageview.image frame:[contentImageview convertRect:contentImageview.bounds toView:window]];
}

5. 添加放大后背景视图的颜色和透明度

//当前视图
  UIWindow *window = [UIApplication sharedApplication].keyWindow;
  //背景
  UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
  [backgroundView setBackgroundColor:[UIColor colorWithRed:107/255.0 green:107/255.0 blue:99/255.0 alpha:0.6]];

6. 使用动画放大展示ImageView

  //动画放大所展示的ImageView
  [UIView animateWithDuration:0.4 animations:^{
    CGFloat y,width,height;
    y = ([UIScreen mainScreen].bounds.size.height - image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width) * 0.5;
    //宽度为屏幕宽度
    width = [UIScreen mainScreen].bounds.size.width;
    //高度 根据图片宽高比设置
    height = image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width;
    [imageView setFrame:CGRectMake(0, y, width, height)];
    //重要! 将视图显示出来
    [backgroundView setAlpha:1];
  } completion:^(BOOL finished) {
    
  }];

7. 添加恢复ImageView原始尺寸的tap点击事件

//添加点击事件同样是类方法 -> 作用是再次点击回到初始大小
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideImageView:)];
[backgroundView addGestureRecognizer:tapGestureRecognizer];

/**
 * 恢复imageView原始尺寸
 */
+(void)hideImageView:(UITapGestureRecognizer *)tap{
  UIView *backgroundView = tap.view;
  //原始imageview
  UIImageView *imageView = [tap.view viewWithTag:1024];
  //恢复
  [UIView animateWithDuration:0.4 animations:^{
    [imageView setFrame:oldframe];
    [backgroundView setAlpha:0];
  } completion:^(BOOL finished) {
    [backgroundView removeFromSuperview];
  }];
}

8. 完成之后将背景视图删掉

//完成后操作->将背景视图删掉
[backgroundView removeFromSuperview];

四:项目实际使用

1. 引入封装类FBYImageZoom

#import "FBYImageZoom.h"

2. 给UIImageView添加手势

//添加点击事件
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scanBigImageClick:)];

3. 调用封装类函数

//浏览大图点击事件
-(void)scanBigImageClick:(UITapGestureRecognizer *)tap{
  NSLog(@"点击图片");
  UIImageView *clickedImageView = (UIImageView *)tap.view;
  [FBYImageZoom ImageZoomWithImageView:clickedImageView];
}

好了,到这里点击图片放大到全屏就完成了

4. 长按保存图片

另外就是实现长按保存图片的功能,这个功能很简单

首先增加长按手势

  //创建长按手势
  UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(imglongTapClick:)];
  //添加手势
  [_myImageView addGestureRecognizer:longTap];

然后长按手势弹出警告视图确认

-(void)imglongTapClick:(UILongPressGestureRecognizer*)gesture
{  
  if(gesture.state==UIGestureRecognizerStateBegan)    
  {    
    UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"保存图片" message:nil preferredStyle:UIAlertControllerStyleAlert];    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
      
      NSLog(@"取消保存图片");
    }];
    
    UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
      
      NSLog(@"确认保存图片");      
      // 保存图片到相册
      UIImageWriteToSavedPhotosAlbum(self.myImageView.image,self,@selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:),nil);
      
    }];    
    [alertControl addAction:cancel];
    [alertControl addAction:confirm];    
    [self presentViewController:alertControl animated:YES completion:nil];
    
  }  
}

最后保存图片后的回调

- (void)imageSavedToPhotosAlbum:(UIImage*)image didFinishSavingWithError: (NSError*)error contextInfo:(id)contextInfo

{
  NSString *message;  
  if(!error) {    
    message =@"成功保存到相册";    
    UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
      
    }];
    [alertControl addAction:action];    
    [self presentViewController:alertControl animated:YES completion:nil];    
  }else
    
  {
    message = [error description];      
    UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
      
    }];    
    [alertControl addAction:action];    
    [self presentViewController:alertControl animated:YES completion:nil];    
  }  
}

到这里实现点击图片放大和长按保存图片功能就都是实现了,demo源码已经放在github上。

五:项目展示

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • iOS省市二级联动的数据组织PHP版

    iOS省市二级联动的数据组织PHP版

    这篇文章主要为大家详细介绍了iOS开发之"省市"二级联动的数据组织PHP版,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • XCode编译速度慢的处理方法

    XCode编译速度慢的处理方法

    本文给大家介绍的是在IOS开发中XCode编译速度慢的3种解决办法,十分的实用,有需要的小伙伴可以参考下。
    2015-06-06
  • IOS代码笔记UIView的placeholder的效果

    IOS代码笔记UIView的placeholder的效果

    这篇文章主要为大家详细介绍了IOS实现placeholder效果的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • iOS创建与使用静态库

    iOS创建与使用静态库

    这篇文章主要为大家详细介绍了iOS创建与使用静态库的相关资料,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • 查看iOS已上架App的Crash信息定位、应对处理方式的实例

    查看iOS已上架App的Crash信息定位、应对处理方式的实例

    下面小编就为大家带来一篇查看iOS已上架App的Crash信息定位、应对处理方式的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-12-12
  • iOS键盘如何添加隐藏键盘功能

    iOS键盘如何添加隐藏键盘功能

    这篇文章主要为大家详细介绍了iOS键盘如何添加隐藏键盘功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-02-02
  • iOS实现MJRefresh下拉刷新(上拉加载)使用详解

    iOS实现MJRefresh下拉刷新(上拉加载)使用详解

    本篇文章主要介绍了iOS实现MJRefresh下拉刷新(上拉加载)使用详解,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-01-01
  • iOS开发常用线程安全锁

    iOS开发常用线程安全锁

    这篇文章主要为大家介绍了iOS开发常用线程安全锁示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • iOS实现选项卡效果的方法

    iOS实现选项卡效果的方法

    选项卡在我们日常开发的时候经常要用到,所以这篇文章给大家分享一种iOS实现的简单选项卡效果,很适合大家学习和使用,有需要的可以参考借鉴,下面来一起看看吧。
    2016-09-09
  • iOS WKWebview 白屏检测实现的示例

    iOS WKWebview 白屏检测实现的示例

    这篇文章主要介绍了iOS WKWebview 白屏检测实现的示例,帮助大家更好的进行ios开发,感兴趣的朋友可以了解下
    2020-10-10

最新评论