欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

IOS中实现图片点击全屏预览

程序员文章站 2023-02-07 12:05:13
如果你感觉累,那就对了那是因为你在走上坡路。。这句话似乎有点道理的样子,时常提醒自己无论走到哪都不要忘记自己当初为什么出发。有时想想感觉有的东西可以记录一下,就把它记录下来...

如果你感觉累,那就对了那是因为你在走上坡路。。这句话似乎有点道理的样子,时常提醒自己无论走到哪都不要忘记自己当初为什么出发。有时想想感觉有的东西可以记录一下,就把它记录下来吧,这次想写一下关于单张图片点击全屏预览的问题,网上查了一些大神写的有的功能确实很强大但自己暂时想要的只是简单的功能就好,还有些方法自己也没弄出想要的效果,最后写了一个比较简单的点击单张图片的全屏预览和双指捏合缩小放大,可能有时要对图片做一些处理,这里放大后只是显示同一张图片并未做处理,下面直接贴出代码

//
// viewcontroller.m
// xwzoomimageview
//
// created by xiao on 15/11/13.
// copyright © 2015年 xiao. all rights reserved.
//

#import "viewcontroller.h"

@interface viewcontroller ()<uiscrollviewdelegate>
@property (weak, nonatomic) iboutlet uiimageview *picview;
@property (weak, nonatomic) uiscrollview *scrollview;
@property (weak, nonatomic) uiimageview *lastimageview;
@property (nonatomic, assign)cgrect originalframe;
@property (nonatomic, assign)bool isdoubletap;
@end

@implementation viewcontroller

- (void)viewdidload {
 [super viewdidload];
 
 self.picview.userinteractionenabled = yes;
 //添加单击手势
 uitapgesturerecognizer *tap = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(showzoomimageview:)];

 [self.picview addgesturerecognizer:tap];
 
}

-(void)showzoomimageview:(uitapgesturerecognizer *)tap
{
 if (![(uiimageview *)tap.view image]) {
  return;
 }
 //scrollview作为背景
 uiscrollview *bgview = [[uiscrollview alloc] init];
 bgview.frame = [uiscreen mainscreen].bounds;
 bgview.backgroundcolor = [uicolor blackcolor];
 uitapgesturerecognizer *tapbg = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(tapbgview:)];
 [bgview addgesturerecognizer:tapbg];
 
 uiimageview *picview = (uiimageview *)tap.view;
 
 uiimageview *imageview = [[uiimageview alloc] init];
 imageview.image = picview.image;
 imageview.frame = [bgview convertrect:picview.frame fromview:self.view];
 [bgview addsubview:imageview];
 
 [[[uiapplication sharedapplication] keywindow] addsubview:bgview];
 
 self.lastimageview = imageview;
 self.originalframe = imageview.frame;
 self.scrollview = bgview;
 //最大放大比例
 self.scrollview.maximumzoomscale = 1.5;
 self.scrollview.delegate = self;
 
 [uiview animatewithduration:0.5 animations:^{
  cgrect frame = imageview.frame;
  frame.size.width = bgview.frame.size.width;
  frame.size.height = frame.size.width * (imageview.image.size.height / imageview.image.size.width);
  frame.origin.x = 0;
  frame.origin.y = (bgview.frame.size.height - frame.size.height) * 0.5;
  imageview.frame = frame;
 }];
}

-(void)tapbgview:(uitapgesturerecognizer *)tapbgrecognizer
{
 self.scrollview.contentoffset = cgpointzero;
 [uiview animatewithduration:0.5 animations:^{
  self.lastimageview.frame = self.originalframe;
  tapbgrecognizer.view.backgroundcolor = [uicolor clearcolor];
 } completion:^(bool finished) {
  [tapbgrecognizer.view removefromsuperview];
  self.scrollview = nil;
  self.lastimageview = nil;
 }];
}

//返回可缩放的视图
-(uiview *)viewforzoominginscrollview:(uiscrollview *)scrollview
{
 return self.lastimageview;
}

最后同样带上一张图片吧,大致是这样子

IOS中实现图片点击全屏预览

再给大家分享一则ios中点击图片后放大的代码

.h文件-----------------------------------------------------------------
#import
@interface zoomimage : nsobject
/**
 *@brief点击图片放大,再次点击缩小
 *
 *@param oldimageview 头像所在的imageview
 */
+(void)showimage:(uiimageview*)avatarimageview;
@end
.m文件-----------------------------------------------------------------
#import "zoomimage.h"
static cgrect oldframe;
@implementation zoomimage
+(void)showimage:(uiimageview*)avatarimageview
{
 uiimage *image =avatarimageview.image;
 // 获得根窗口
 uiwindow *window =[uiapplication sharedapplication].keywindow;
 uiview *backgroundview =[[uiview alloc]initwithframe:cgrectmake(0, 0, width, height)];
 oldframe =[avatarimageview convertrect:avatarimageview.bounds toview:window];
 backgroundview.backgroundcolor =[uicolor blackcolor];
 backgroundview.alpha =0.5;
 uiimageview *imageview =[[uiimageview alloc]initwithframe:oldframe];
 imageview.image =image;
 imageview.tag =1;
 [backgroundview addsubview:imageview];
 [window addsubview:backgroundview];
 //点击图片缩小的手势
 uitapgesturerecognizer *tap =[[uitapgesturerecognizer alloc]initwithtarget:self action:@selector(hideimage:)];
 [backgroundview addgesturerecognizer:tap];
 [uiview animatewithduration:0.3 animations:^{
  imageview.frame =cgrectmake(0,([uiscreen mainscreen].bounds.size.height-image.size.height*[uiscreen mainscreen].bounds.size.width/image.size.width)/2, [uiscreen mainscreen].bounds.size.width, image.size.height*[uiscreen mainscreen].bounds.size.width/image.size.width);
  backgroundview.alpha =1;
 }];
}
+(void)hideimage:(uitapgesturerecognizer *)tap{
 uiview *backgroundview =tap.view;
 uiimageview *imageview =(uiimageview *)[tap.view viewwithtag:1];
 [uiview animatewithduration:0.3 animations:^{
  imageview.frame =oldframe;
  backgroundview.alpha =0;  
 } completion:^(bool finished) {
  [backgroundview removefromsuperview];
 }];
}