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

图像之超简单方式实现微信头像功能

2016-03-02 23:38 711 查看
//直接使用下面函数就可以实现微信头像功能

//m文件

import “headImageViewController.h”

@interface headImageViewController ()

@property (strong, nonatomic) IBOutlet UIImageView *photoImage;

@property (assign ,nonatomic)CGPoint startpoint;

@property (strong, nonatomic)IBOutlet UIImageView *imageView;

@property (strong, nonatomic)UIImage *selectImage;

@property (strong, nonatomic)IBOutlet UIButton *okbut;

@property (strong, nonatomic)IBOutlet UIButton *cancelbut;

@end

@implementation headImageViewController

(void)viewDidLoad {

[super viewDidLoad];

self.imageView.image = self.selectImage;

UIPinchGestureRecognizer *pinchGestureRecognizermiddle = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];

UIPanGestureRecognizer *panGestureRecognizermiddle = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];

self.photoImage.hidden=YES;

[self.okbut setTitle:NSLocalizedString(@”Select”, “”) forState:UIControlStateNormal];

[self.cancelbut setTitle:NSLocalizedString(@”Cancel”,”“) forState:UIControlStateNormal];

UIView *viewup = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kDeviceWidth, ((kDeviceHeight - 49 -kDeviceWidth)/2))];

viewup.backgroundColor = [UIColor blackColor];

viewup.alpha = 0.4;

[self.view addSubview:viewup];

UIView *viewmiddle = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kDeviceWidth, kDeviceHeight - 49)];

viewmiddle.backgroundColor = [UIColor clearColor];

[self.view addSubview:viewmiddle];

[viewmiddle addGestureRecognizer:pinchGestureRecognizermiddle];

[viewmiddle addGestureRecognizer:panGestureRecognizermiddle];

UIView *viewdown = [[UIView alloc]initWithFrame:CGRectMake(0, (kDeviceHeight - 49 -kDeviceWidth)/2 + kDeviceWidth + 2, kDeviceWidth, (kDeviceHeight - 49 - kDeviceWidth)/2 - 2)];

viewdown.backgroundColor = [UIColor blackColor];

viewdown.alpha = 0.4;

[self.view addSubview:viewdown];

UIView *leftline = [[UIView alloc]initWithFrame:CGRectMake(0, (kDeviceHeight - 49 - kDeviceWidth)/2, 2, kDeviceWidth)];

leftline.backgroundColor = [UIColor whiteColor];

[self.view addSubview:leftline];

UIView *upperline = [[UIView alloc]initWithFrame:CGRectMake(0, (kDeviceHeight - 49 - kDeviceWidth)/2, kDeviceWidth, 2)];

upperline.backgroundColor = [UIColor whiteColor];

[self.view addSubview:upperline];

UIView *rightline = [[UIView alloc]initWithFrame:CGRectMake(kDeviceWidth - 2, (kDeviceHeight - 49 -kDeviceWidth)/2, 2, kDeviceWidth)];

rightline.backgroundColor = [UIColor whiteColor];

[self.view addSubview:rightline];

UIView *lowerline = [[UIView alloc]initWithFrame:CGRectMake(0, (kDeviceHeight - 49 - kDeviceWidth)/2 + kDeviceWidth, kDeviceWidth, 2)];

lowerline.backgroundColor = [UIColor whiteColor];

[self.view addSubview:lowerline];

[viewmiddle bringSubviewToFront:self.view];

}

-(void)viewWillAppear:(BOOL)animated

{

[self performSelector:@selector(fitimageview) withObject:nil afterDelay:0.1];

}

// 处理拖拉手势

- (void) panView:(UIPanGestureRecognizer *)panGestureRecognizer

{

if (panGestureRecognizer.state == UIGestureRecognizerStateBegan || panGestureRecognizer.state == UIGestureRecognizerStateChanged) {

CGPoint translation = [panGestureRecognizer translationInView:self.imageView.superview];

CGPoint point = CGPointMake(self.imageView.center.x + translation.x, self.imageView.center.y + translation.y);

CGPoint point1 = CGPointMake(self.imageView.center.x  + translation.x - self.imageView.frame.size.width/2, self.imageView.center.y + translation.y - self.imageView.frame.size.height/2);

CGPoint point2 = CGPointMake(self.imageView.center.x  + translation.x + self.imageView.frame.size.width/2, self.imageView.center.y + translation.y - self.imageView.frame.size.height/2);

CGPoint point3 = CGPointMake(self.imageView.center.x  + translation.x + self.imageView.frame.size.width/2, self.imageView.center.y + translation.y + self.imageView.frame.size.height/2);

CGPoint point4 = CGPointMake(self.imageView.center.x  + translation.x + self.imageView.frame.size.width/2, self.imageView.center.y + translation.y + self.imageView.frame.size.height/2);
point2 = CGPointMake(0,(kDeviceHeight - 49 -kDeviceWidth)/2);
point3 = CGPointMake(0,(kDeviceHeight - 49 -kDeviceWidth)/2 + kDeviceWidth);
CGPoint lastpoint = [self movefitsize:point Firstpoint:point1 Lastpoint:point4 LimitFirstpoint:point2 LimitLastpoint:point3];

if (!(point.x == lastpoint.x && point.y == lastpoint.y)) {
[UIView animateWithDuration:0.2
delay:0.0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
[self.imageView setCenter:point];
}
completion:^(BOOL finished){

[UIView animateWithDuration:0.3
delay:0.0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
[self.imageView setCenter:lastpoint];
}
completion:^(BOOL finished){
}];

}];

}else
{
[self.imageView setCenter:lastpoint];
}
[panGestureRecognizer setTranslation:CGPointZero inView:self.imageView.superview];
}


}

-(void)fitimageview

{

self.imageView.frame = CGRectMake(0, (kDeviceHeight - 49 - (kDeviceWidth *self.imageView.image.size.height /(self.imageView.image.size.width*1.0)))/2 , kDeviceWidth, (kDeviceWidth *self.imageView.image.size.height /(self.imageView.image.size.width *1.0)) );
self.startpoint =CGPointMake(self.imageView.frame.size.width, self.imageView.frame.size.height);


}

-(CGPoint)movefitsize:(CGPoint)Originalpoint Firstpoint:(CGPoint)firstpoint Lastpoint:(CGPoint)lastpoint LimitFirstpoint:(CGPoint)limitfirstpoint LimitLastpoint:(CGPoint)limitlastpoint

{

CGPoint point =CGPointMake(Originalpoint.x, Originalpoint.y);

if (firstpoint.x <= 0 && firstpoint.y <= limitfirstpoint.y && lastpoint.x > kDeviceWidth && lastpoint.y > limitlastpoint.y   ){

point = CGPointMake(Originalpoint.x, Originalpoint.y);

}else if (firstpoint.x > 0 && firstpoint.y <= limitfirstpoint.y && lastpoint.x > kDeviceWidth && lastpoint.y > (limitlastpoint.y)   ){
point = CGPointMake(Originalpoint.x - firstpoint.x, Originalpoint.y);

}else if (firstpoint.x <= 0 && firstpoint.y > limitfirstpoint.y && lastpoint.x > kDeviceWidth && lastpoint.y > (limitlastpoint.y)   ){

point = CGPointMake(Originalpoint.x , Originalpoint.y -(firstpoint.y - limitfirstpoint.y));

}else if (firstpoint.x <= 0 && firstpoint.y <= limitfirstpoint.y && lastpoint.x < kDeviceWidth && lastpoint.y > (limitlastpoint.y)   ){
point = CGPointMake(Originalpoint.x + kDeviceWidth - lastpoint.x , Originalpoint.y );

}else if (firstpoint.x <= 0 && firstpoint.y <= limitfirstpoint.y && lastpoint.x > kDeviceWidth && lastpoint.y < (limitlastpoint.y)   ){
point = CGPointMake(Originalpoint.x  , Originalpoint.y + (limitlastpoint.y) - lastpoint.y );

}else if (firstpoint.x > 0 && firstpoint.y > limitfirstpoint.y && lastpoint.x > kDeviceWidth && lastpoint.y > (limitlastpoint.y)   ){
point = CGPointMake(Originalpoint.x - firstpoint.x, Originalpoint.y -(firstpoint.y - limitfirstpoint.y));

}else if (firstpoint.x <= 0 && firstpoint.y > limitfirstpoint.y && lastpoint.x < kDeviceWidth && lastpoint.y > (limitlastpoint.y)   ){
point = CGPointMake(Originalpoint.x + kDeviceWidth - lastpoint.x, Originalpoint.y -(firstpoint.y - limitfirstpoint.y));

}else if (firstpoint.x <= 0 && firstpoint.y <= limitfirstpoint.y && lastpoint.x < kDeviceWidth && lastpoint.y < (limitlastpoint.y)   ){

point = CGPointMake(Originalpoint.x + kDeviceWidth - lastpoint.x, Originalpoint.y + (limitlastpoint.y) - lastpoint.y);

}else if (firstpoint.x > 0 && firstpoint.y <= limitfirstpoint.y && lastpoint.x > kDeviceWidth && lastpoint.y < (limitlastpoint.y)   ){

point = CGPointMake(Originalpoint.x - firstpoint.x, Originalpoint.y + (limitlastpoint.y) - lastpoint.y);

}else{
}
return point;


}

(IBAction)cancelbut:(UIButton *)sender {

[self dismissViewControllerAnimated:YES completion:nil];

}

// 处理缩放手势

- (void) pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer

{

if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan || pinchGestureRecognizer.state == UIGestureRecognizerStateChanged) {

self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);

pinchGestureRecognizer.scale = 1;

}
if (pinchGestureRecognizer.state == UIGestureRecognizerStateEnded&self.imageView.frame.size.width <kDeviceWidth) {
[UIView animateWithDuration:0.2
delay:0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, kDeviceWidth/self.imageView.frame.size.width*1.1,  kDeviceWidth/self.imageView.frame.size.width*1.1);
}
completion:^(BOOL finished){

[UIView animateWithDuration:0.2
delay:0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, kDeviceWidth/self.imageView.frame.size.width,  kDeviceWidth/self.imageView.frame.size.width);
}
completion:^(BOOL finished){
}];

}];
}


}

(IBAction)selectbut:(UIButton *)sender {

float offset =1.0;

offset = self.imageView.image.size.width/self.imageView.frame.size.width;

CGPoint point2 = CGPointMake(0,(kDeviceHeight - 49 -kDeviceWidth)/2);

CGPoint point3 = CGPointMake(kDeviceWidth,(kDeviceHeight - 49 -kDeviceWidth)/2 + kDeviceWidth);

CGPoint centerpoint = [self.imageView.layer convertPoint:point2 fromLayer:self.view.layer];

CGPoint centerpoint2 = [self.imageView.layer convertPoint:point3 fromLayer:self.view.layer];

centerpoint = CGPointMake(centerpoint.x *self.imageView.frame.size.width/ self.startpoint.x ,centerpoint.y *self.imageView.frame.size.height/ self.startpoint.y);

centerpoint2 = CGPointMake(centerpoint2.x *self.imageView.frame.size.width/ self.startpoint.x ,centerpoint2.y *self.imageView.frame.size.height/ self.startpoint.y);

if (centerpoint.x < 0.0) {

centerpoint = CGPointMake(0,centerpoint.y);

}

if (centerpoint.x > self.imageView.frame.size.width) {

centerpoint = CGPointMake(0,centerpoint.y);

}

if (centerpoint.y < 0.0) {

centerpoint = CGPointMake(centerpoint.x,0);

}

if (centerpoint.y > self.imageView.frame.size.height) {

centerpoint = CGPointMake(centerpoint.x,0);

}

if (centerpoint2.x < 0.0) {

centerpoint2 = CGPointMake(self.imageView.frame.size.width,centerpoint2.y);

}

if (centerpoint2.x > self.imageView.frame.size.width) {

centerpoint2 = CGPointMake(self.imageView.frame.size.width,centerpoint2.y);

}

if (centerpoint2.y < 0.0) {

centerpoint2 = CGPointMake(centerpoint2.x,self.imageView.frame.size.height);

}

if (centerpoint2.y > self.imageView.frame.size.height) {

centerpoint2 = CGPointMake(centerpoint2.x,self.imageView.frame.size.height);

}

CGPoint centerpoint3 = CGPointMake(((centerpoint.x + centerpoint2.x)/2.0)* offset, ((centerpoint.y + centerpoint2.y)/2.0)* offset );

float width = (centerpoint2.x - centerpoint.x)* offset ;

float hight = (centerpoint2.y - centerpoint.y)* offset ;

// 下面就是得到的图片

UIImage *image= [self getCutPartOfImage:self.imageView.image Width:width Hight:hight BeganPonit:centerpoint3];

}

-(UIImage )getCutPartOfImage:(UIImage )image Width:(float)width Hight:(float)hight BeganPonit:(CGPoint)point

{

@autoreleasepool {

Imageprocess *imageproc=[[Imageprocess alloc]init];

UIImage *newimage = [[UIImage alloc]init];

newimage = image;

CGSize size = CGSizeMake(width,hight);

//get image
UIImage *imagemodel = [[UIImage alloc]init];


//下面两个函数见我 《 图像之指定位置,指定大小切图》文章

(/article/7918935.html)

newimage = [imageproc imageAlphaToWhite:image];

newimage =[imageproc CutPartOfImage:newimage mainimage:self.imageView.image offX:point.x - width/2.0 offY:point.y - hight/2.0 Size:size];

return newimage;

}

}

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