您的位置:首页 > 移动开发 > 微信开发

高仿QQ、微信用户图像处理

2016-06-26 00:00 441 查看
摘要: 封装一个图片处理工具,类似QQ、微信用户图像处理,支持单击、双击、缩放手势,一句代码搞定。

// .h文件

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

@interface ImageDandleTool : NSObject

// 获取控件的坐标,传入用户图片

-(void)fullScreenWithUIView:(UIView *)view Image:(UIImage *)UserImage;

@end

// .m

#import "ImageDandleTool.h"

@interface ImageDandleTool ()<UIScrollViewDelegate>

@end

static CGRect oldFrame;

@implementation ImageDandleTool

-(void)fullScreenWithUIView:(UIView *)view Image:(UIImage *)UserImage{

UIImage *image = UserImage;

UIWindow *window = [UIApplication sharedApplication].keyWindow;

UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];

scrollView.maximumZoomScale = 1.5;

scrollView.minimumZoomScale = 1.0;

scrollView.bounces = NO;

scrollView.delegate = self;

// 坐标系的转化,将控件的坐标转换成目标视图中的坐标;

oldFrame = [view convertRect:view.bounds toView:window];

scrollView.backgroundColor = [UIColor blackColor];

scrollView.alpha = 0;

UIImageView *imageView = [[UIImageView alloc]initWithFrame:oldFrame];

imageView.image = image;

imageView.tag = 1;

[scrollView addSubview:imageView];

[window addSubview:scrollView];

// 添加点击手势;

UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(fullScreenTapGR:)];

[scrollView 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);

scrollView.alpha=1;

}

completion:^(BOOL finished) {

}];

// 添加双击的手势;

UITapGestureRecognizer *tapTwo = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapTwoAction:)];

tapTwo.numberOfTapsRequired = 2;

[scrollView addGestureRecognizer:tapTwo];

// 当双击手势和单击手势共存的时候,只有没有检测到双击手势的时候,单击才有效;

[tap requireGestureRecognizerToFail:tapTwo];

}

//双击手势触发的方法;

-(void)tapTwoAction:(UITapGestureRecognizer *)sender{

UIScrollView *scrollView = (UIScrollView *)sender.view;

if (scrollView.zoomScale <= 1.0) {

[UIView animateWithDuration:0.3 animations:^{

scrollView.zoomScale = 1.5;

}];

}else{

[UIView animateWithDuration:0.3 animations:^{

scrollView.zoomScale = 1.0;

}];

}

}

// 点击退回原来的图片;

-(void)fullScreenTapGR:(UITapGestureRecognizer*)tap{

UIScrollView *backgroundView = (UIScrollView *)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];

}];

}

//返回要缩放的控件;

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{

return [scrollView viewWithTag:1];

}

//只要进行缩放,就会执行的该方法,使图片居中;

-(void)scrollViewDidZoom:(UIScrollView *)scrollView{

CGPoint cender = scrollView.center;

if (scrollView.zoomScale >1) {

cender.x = [UIScreen mainScreen].bounds.size.width/2 +((scrollView.zoomScale - 1) * [UIScreen mainScreen].bounds.size.width/2);

cender.y = [UIScreen mainScreen].bounds.size.height/2;

}

[scrollView viewWithTag:1].center = cender;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息