实现图片缩放

//

//  ImagePreViewController.h

//  missbetterc

//

//  Created by Gloomy Ming River on 4/15/15.

//  Copyright (c) 2015 qiqugou. All rights reserved.

//

ImagePreViewController.h文件:

#import "xxxViewController.h"


@interface ImagePreViewController :xxxViewController<UIScrollViewDelegate,UIGestureRecognizerDelegate>{

    

    UIScrollView *_scrollview;

    UIImageView *_imageview;

    UIImage *preImage;

    

    CGPoint oldPoint;

    

    CGFloat _touchX;

    CGFloat _touchY;

    

    CGFloat newWidth;

    CGFloat newHight;

    

   //用于判断是否为原始大小

    CGFloat startWidth;

    

    

 

}

-(id)initWithPath:(NSString *)path;


@property (nonatomic)BOOL isTwiceTaping;

@property (nonatomic)BOOL isDoubleTapingForZoom;

@property (nonatomic)CGFloat currentScale;

@property (nonatomic)CGFloat offsetY;

@property (nonatomic) CGFloat touchX;

@property (nonatomic) CGFloat touchY;

@end



.m文件:


//

//  ImagePreViewController.m

//  missbetterc

//

//  Created by Gloomy Ming River on 4/15/15.

//  Copyright (c) 2015 qiqugou. All rights reserved.

//


#import "ImagePreViewController.h"

#import "Common.h"

#import "setting.h"



 

#define kMaxZoom 1.5

@interface ImagePreViewController ()


@end


@implementation ImagePreViewController


-(id)initWithPath:(NSString *)path{

    self=[super init];

    if (self) {

        //根据url地址获取相应图片的路径

        NSString *imagePath= getFilePathFromURL(path);

       

        preImage=[[UIImage alloc]initWithContentsOfFile:imagePath];

        

    }

    return self;

}


-(void)setScrollView{

    

    //检测图片大小

    [self checkImage];

    

    _scrollview=[[UIScrollView alloc]initWithFrame:self.view.bounds];

    _scrollview.showsVerticalScrollIndicator=NO;

    _scrollview.showsHorizontalScrollIndicator=NO;

    [self.view addSubview:_scrollview];

    _scrollview.contentMode=UIViewContentModeCenter;

    UIImage *image=preImage;

    _imageview=[[UIImageView alloc]initWithImage:image];

    

    //纪录初始宽度

    startWidth=self.view.frame.size.width;

    _imageview.userInteractionEnabled=YES;

    

    //图片居中显示

    _imageview.center=CGPointMake(self.view.frame.size.width/2,self.view.frame.size.height/2);

    

    oldPoint=_imageview.frame.origin;

    [_scrollview addSubview:_imageview];

    

    //计算缩放最小倍数,让图片最小为屏幕宽度

    minScale=self.view.frame.size.width/preImage.size.width;

    

    //设置UIScrollView的滚动范围和图片的真实尺寸一致

    _scrollview.contentSize=CGSizeMake(preImage.size.width, preImage.size.height+10);

    

    

    //设置实现缩放

    //设置代理scrollview的代理对象

    _scrollview.delegate=self;

    //设置最大伸缩比例

    _scrollview.maximumZoomScale=1.5;

    //设置最小伸缩比例

    _scrollview.minimumZoomScale=minScale;

    

    

    //_scrollview.backgroundColor=[UIColor clearColor];

    

    //单击手势

    UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImage:)];

    //设置点击数目

    [tapGesture setNumberOfTapsRequired:1];

    //设置点击的手指数目

    [tapGesture setNumberOfTouchesRequired:1];

    [_scrollview addGestureRecognizer:tapGesture];

    

    

    //双击手势

    UITapGestureRecognizer *tapDoubleGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapDoubleImage:)];

    [tapDoubleGesture setNumberOfTapsRequired:2];

    [tapDoubleGesture setNumberOfTouchesRequired:1];

    [_scrollview addGestureRecognizer:tapDoubleGesture];

    //单击和双击手势共存

    [tapGesture requireGestureRecognizerToFail:tapDoubleGesture];

    

    

    

}

//检测图片大小,若过大,则等比缩放到与屏幕同宽的图片

-(void)checkImage{

//   

    newWidth=preImage.size.height;

    newHight=preImage.size.width;

    CGFloat changeScale;

    if (preImage.size.width>self.view.frame.size.width) {

       newWidth=self.view.frame.size.width;

        changeScale=self.view.frame.size.width/preImage.size.width;

         preImage=[self scaleImage:preImage toScale:changeScale];

    }

}

//等笔缩放图片

- (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize

{

    UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize));

    [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];

    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return scaledImage;

    

}


- (void)viewDidLoad {

    [super viewDidLoad];

    

    //设置颜色,让弹出界面时候不太难看

    [self.view setBackgroundColor:[UIColor blackColor]];


    //初始化控件

    [self setScrollView];

 

    

}

-(void)tapImage:(id)sender{

    

    if (_isTwiceTaping==NO) {

        [self dismissViewControllerAnimated:YES completion:nil];

    }

}


-(void)tapDoubleImage:(id)sender{

    _touchX = [sender locationInView:_scrollview].x;

    _touchY = [sender locationInView:_scrollview].y;

    CGPoint point=CGPointMake(_touchX, _touchY);

    if(_isTwiceTaping){

        return;

    }

    //点击状态

    _isTwiceTaping = YES;

    //如果已被拉大或已被放大,则不允许放大,进行缩小

    if(_currentScale > 1.0||_imageview.frame.size.width!=startWidth){

       

        _currentScale = 1.0;

    

        CGRect zoomRect = [self zoomRectForScale:_currentScale withCenter:point];

        [_scrollview zoomToRect:zoomRect animated:YES];


    }

    else{

        _isDoubleTapingForZoom = YES;

        _currentScale = kMaxZoom;

        CGRect zoomRect = [self zoomRectForScale:_currentScale withCenter:point];

        [_scrollview zoomToRect:zoomRect animated:YES];

    }

    

    _isDoubleTapingForZoom = NO;

    //延时做标记判断,使用户点击3次时的单击效果不生效;否则会产生双击+单击的回调响应

    [self performSelector:@selector(twiceTaping) withObject:nil afterDelay:0.65];

}


-(void)twiceTaping{

    _isTwiceTaping=NO;

}



//获得当前点击处的中心X与原来图片尺寸的差值,然后传递给Scrollview进行缩放

- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center

{

    CGRect zoomRect;

    zoomRect.size.height =_imageview.frame.size.height / scale;

    zoomRect.size.width  =_imageview.frame.size.width  / scale;

    zoomRect.origin.x = center.x - (zoomRect.size.width  /2.0);

    zoomRect.origin.y = center.y - (zoomRect.size.height /2.0);

    return zoomRect;

}



//下面实现图片缩放


-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView_

{

    return _imageview;

}



- (void)scrollViewDidZoom:(UIScrollView *)aScrollView

{



    //让图片居中

    CGFloat offsetX = (_scrollview.bounds.size.width > _scrollview.contentSize.width)?

    (_scrollview.bounds.size.width - _scrollview.contentSize.width) * 0.5 : 0.0;

    CGFloat offsetY = (_scrollview.bounds.size.height > _scrollview.contentSize.height)?

    (_scrollview.bounds.size.height - _scrollview.contentSize.height) * 0.5 : 0.0;

    _imageview.center = CGPointMake(_scrollview.contentSize.width * 0.5 + offsetX,

                                 _scrollview.contentSize.height * 0.5 + offsetY);

    //设置可以上下左右拖动

   _scrollview.contentSize=CGSizeMake(_imageview.frame.size.width,_imageview.frame.size.height+10);

 

}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



@end




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值