Home>

ios as a smart phone system,When viewing pictures,If you can achieve full screen,Has a good visual experience for the user,It's actually very simple to implement,Below I will share a simple code to share with you,,

If you feel tired,That's right because you are going uphill.. This sentence seems a bit reasonable,Always remind yourself that no matter where you go, don't forget why you started.Sometimes think about something that you can record.Just record it,This time I want to write a question about a single picture click full screen preview,I checked some of the functions written by some great gods on the Internet. They are really powerful, but what I want for the time being is simple functions.There are also some methods that have not achieved the desired effect by themselves.Finally, I wrote a relatively simple full-screen preview of a single picture and pinch to zoom out.May sometimes do some processing on the picture,After zooming in here, only the same picture is displayed without processing.Paste the code directly below

//
//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;
 //Add a click gesture
 uitapgesturerecognizer * tap=[[uitapgesturerecognizer alloc] initwithtarget:self action:@selector (showzoomimageview :)];
 [self.picview addgesturerecognizer:tap];
}
-(void) showzoomimageview:(uitapgesturerecognizer *) tap
{
 if (! [(uiimageview *) tap.view image]) {
  return;
 }
 //scrollview as background
 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;
 //Maximum magnification
 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;
 }];
}
//Return to a zoomable view
-(uiview *) viewforzoominginscrollview:(uiscrollview *) scrollview
{
 return self.lastimageview;
}

Finally bring a picture too,Roughly like this

I will share with you a code that is enlarged after clicking the picture in ios

.h file ----------------------------------------------- ------------------
#import
@interface zoomimage:nsobject
/**
 *@brief Click on the image to enlarge it,Click again to zoom out
 *
 *@param oldimageview imageview where the avatar is
 * /
+ (void) showimage:(uiimageview *) avatarimageview;
@end
.m file ----------------------------------------------- ------------------
#import "zoomimage.h"
static cgrect oldframe;
@implementation zoomimage
+ (void) showimage:(uiimageview *) avatarimageview
{
 uiimage * image=avatarimageview.image;
 //get root window
 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];
 //click the picture to zoom out gesture
 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];
 }];
}
ios
  • Previous C # Socket Network Programming Example
  • Next Learn C pointers in 5 minutes