仿微信查看图片功能

仿微信中查看图片,部分功能

 先跟各位说声抱歉,在此功能中集成了分享功能,但是录屏时使用的是模拟器运行的,分享功能暂不可用;我们先看下运行的效果:


效果图.gif

在这个demo中,单击放大图片,长按调起系统弹窗,分别有分享图片和保存图片的功能,下面我们来看怎么去一步一步实现?!
大致的设计思路是这样的:

  • 初始化一个UIImageView,这个是放置图片的,在当前的视图上添加一个单击的手势,目的是要把将要放大的图片放置在另一个窗口中;
  • 这一步我们将要把盛放图片的View视图以及图片传递给我们新的窗口去做下一步的设置;
  • 在新的窗口中,拿到imageView和image之后,在新建一个Window,将拿到的UIImageView覆盖到window上去,然后我们在window中新建一个UIImageView,将其添加到当前的View中,并向当前View添加点击手势,用于切回原图去,随后向当前View添加长按手势,用于调起分享和保存图片,放大图片用到了动画效果;
    下面我们一起来看代码的实现:
    首先,我们创建一个FFTImageZoomVC的类,来实现放大和保存图片的功能,先看.h文件的声明
@class FFTImageZoomVC;

@protocol FFTImageZoomDelegate <NSObject>

- (void)imageZoomVC:(FFTImageZoomVC *)imageZoomVC
           hiddenVC:(NSDictionary *)userIndo;

@end

@interface FFTImageZoomVC : UIViewController

@property (nonatomic, weak) id<FFTImageZoomDelegate> delegate;

@property (nonatomic, weak) UIImageView *imageView;
@property (nonatomic, copy) NSString *imageName;

@end

这里面声明了一个隐藏当前VC的代理,还有传进来的imageView和imageName图片名,继续看.m对其的实现:

#import "FFTImageZoomVC.h"
#import "ShareSDKManager.h"

@interface FFTImageZoomVC ()

@property (nonatomic, assign) CGRect oldFrame;

@end

@implementation FFTImageZoomVC


- (void)scanBigImageWithImage:(UIImage * _Nullable)image
                       frame:(CGRect)imageOldframe
{
    self.oldFrame = imageOldframe;
    
    [self.view setBackgroundColor:[UIColor blackColor]];
    
    //将所展示的imageView重新绘制在当前view中
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.oldFrame];
    [imageView setImage:image];
    [imageView setTag:1000];
    
    [self.view addSubview:imageView];
    
    //添加点击事件同样是类方法 -> 作用是再次点击回到初始大小
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideImageView:)];
    
    [self.view addGestureRecognizer:tapGestureRecognizer];
    
    UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc]initWithTarget:self
                                                                                         action:@selector(saveImageClicked:)];
    [self.view addGestureRecognizer:longTap];
    
    //动画放大所展示的ImageView
    [UIView animateWithDuration:0.4 animations:^{
        CGFloat y,width,height;
        y = (KDeviceHeight - image.size.height * KDeviceWidth / image.size.width) * 0.5;
        //宽度为屏幕宽度
        width = KDeviceWidth;
        //高度 根据图片宽高比设置
        height = image.size.height * KDeviceWidth / image.size.width;
        [imageView setFrame:CGRectMake(0, y, width, height)];
        // 将视图显示出来
        [self.view setAlpha:1];
    } completion:^(BOOL finished)
     {
         
     }];
    
}

/**
 *  @param contentImageview 图片所在的imageView
 */

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

/**
 *  恢复imageView原始尺寸
 *
 *  @param tap 点击事件
 */
- (void)hideImageView:(UITapGestureRecognizer *)tap
{
    UIView *backgroundView = tap.view;
    //原始imageview
    UIImageView *imageView = [tap.view viewWithTag:1000];
    //恢复
    [UIView animateWithDuration:0.4 animations:^{
        [imageView setFrame:self.oldFrame];
        [backgroundView setAlpha:0];
    }
                     completion:^(BOOL finished)
     {
         //完成后操作->将背景视图删掉
         [backgroundView removeFromSuperview];
     }];
    if (self.delegate)
    {
        [self.delegate imageZoomVC:self hiddenVC:nil];
    }
}
- (void)saveImageClicked:(UILongPressGestureRecognizer *)tap
{
    if(tap.state == UIGestureRecognizerStateBegan)
        
    {
        UIAlertController *sheetControl = [UIAlertController alertControllerWithTitle:nil
                                                                              message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        
        UIAlertAction *shareAction = [UIAlertAction actionWithTitle:@"分享图片"
                                                              style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
        {
            [ShareSDKManager showShareIconContent:self withImageName:self.imageName];
        }];
        [sheetControl addAction:shareAction];
        
        UIAlertAction *saveAction = [UIAlertAction actionWithTitle:@"保存图片"
                                                             style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
        {
            // 保存图片到相册
        UIImageWriteToSavedPhotosAlbum(self.imageView.image,self,@selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:),nil);
            
        }];
        
        [sheetControl addAction:saveAction];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消"
                                                               style:UIAlertActionStyleCancel
                                                             handler:nil];
        [cancelAction setTextColor:[UIColor redColor]];
        
        [sheetControl addAction:cancelAction];
        
    
        [self presentViewController:sheetControl animated:YES completion:nil];
        
    }
}

#pragma mark 保存图片后的回调
- (void)imageSavedToPhotosAlbum:(UIImage* _Nullable)image
       didFinishSavingWithError:(NSError*)error
                    contextInfo:(id _Nullable)contextInfo

{
    NSString *message;
    
    if(!error)
    {
        message = @"成功保存到系统相册";
        
        [self progressHUDShowSuccess:message];
    }
    else
    {
        message = [error description];
        
        [self showAlertControlWithTitle:@"提示"
                                message:message
                         preferredStyle:UIAlertControllerStyleAlert
                            cancelTitle:@"取消"
                            cancelStyle:UIAlertActionStyleCancel
                           cancelHander:nil
                           confirmTitle:@"重试"
                           confirmStyle:UIAlertActionStyleDefault
                          confirmHander:nil];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationController.navigationBar.hidden = YES;
    
    [self imageZoomWithImageView:self.imageView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end

声明一下,这里的分享功能集成的微信的,我对其进行了二次的封装使用,还有一点最需要注意的是,使用保存图片,需要调用系统的相册权限,这个我们要到项目的info.plist去添加一下,
key:Privacy - Photo Library Additions Usage Description
value:应用内需访问相册(用于保存图片)这个说明是要添加的,至于内容视业务而定,这里不多赘述;
 以上,我们把主要的功能都实现了,现在我们来怎么去使用这个类?!新建一个VC类,引入FFTImageZommVC.h头文件,并把当前VC作为其代理,通过代码来看实例吧~

#import "FFTImageTestVC.h"
#import "FFTImageZoomVC.h"

@interface FFTImageTestVC ()<FFTImageZoomDelegate>

@property (nonatomic, strong) UIImageView *myImageView;
@property (nonatomic, strong) NSString *imageName;

@end

@implementation FFTImageTestVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"图片缩放";
    
    self.myImageView = [UIImageView new];
//    self.myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 150, KDeviceWidth-100, KDeviceHeight-100)];
    
    self.imageName = @"bk_nobill_bgImg";
    self.myImageView.image = [UIImage imageNamed:self.imageName];
    self.myImageView.frame = CGRectMake(0,0, self.myImageView.image.size.width, self.myImageView.image.size.height);
    
    self.myImageView.center = self.view.center;
   
    
    //添加点击事件
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scanBigImageClick:)];

    [self.myImageView addGestureRecognizer:tapGestureRecognizer];
    [self.myImageView setUserInteractionEnabled:YES];
    
    [self.view addSubview:self.myImageView];
}

#pragma mark - 浏览大图点击事件
-(void)scanBigImageClick:(UITapGestureRecognizer *)tap
{
    NSLog(@"点击图片");
    UIImageView *clickedImageView = (UIImageView *)tap.view;
    
    FFTImageZoomVC *vc = [FFTImageZoomVC new];
    vc.delegate = self;
    vc.imageView = clickedImageView;
    vc.imageName = self.imageName;
    
    vc.view.frame = CGRectMake(0, 0, KDeviceWidth, KDeviceHeight);
    [self.navigationController addChildViewController:vc];
    [self.navigationController.view addSubview:vc.view];
}

- (void)imageZoomVC:(FFTImageZoomVC *)imageZoomVC
           hiddenVC:(NSDictionary *)userIndo
{
    [imageZoomVC.view removeFromSuperview];
    [imageZoomVC removeFromParentViewController];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

 以上就是这次的全部功能的展示,如有不足之处还望各位指正,大家有好的想法和建议也希望各位留言交流~

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,036评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,046评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,411评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,622评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,661评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,521评论 1 304
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,288评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,200评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,644评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,837评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,953评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,673评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,281评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,889评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,011评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,119评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,901评论 2 355

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,103评论 4 62
  • 用到的组件 1、通过CocoaPods安装 2、第三方类库安装 3、第三方服务 友盟社会化分享组件 友盟用户反馈 ...
    SunnyLeong阅读 14,616评论 1 180
  • #幸福是需要修出来的~每天进步1%~幸福实修08班~14-申屠枫艳-富阳# 20170823(66/99) 【幸福...
    幸福实修申屠阅读 144评论 0 0
  • 一、背景介绍 一个程序员按照要求编写一个网页,不可能一次编写就完全达到目的,一般要对自己的的代码修改调试几次后才能...
    阿布_0caf阅读 13,116评论 0 12
  • 前日的八公里跑,已经是今年第三次八公里,跑完后,没有出现以往的疲惫,开心极了。分析的主要原因是纠正了跑姿,双臂减少...
    爱看云朵阅读 190评论 0 0