//直接使用下面函数就可以实现微信头像功能

//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.2delay:0.0options: UIViewAnimationOptionCurveEaseInanimations:^{[self.imageView setCenter:point];}completion:^(BOOL finished){[UIView animateWithDuration:0.3delay:0.0options: UIViewAnimationOptionCurveEaseInanimations:^{[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.2delay:0options: UIViewAnimationOptionCurveEaseInanimations:^{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.2delay:0options: UIViewAnimationOptionCurveEaseInanimations:^{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 imageUIImage *imagemodel = [[UIImage alloc]init];

//下面两个函数见我 《 图像之指定位置,指定大小切图》文章
(http://blog.csdn.net/f735614338/article/details/50776209)
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

图像之超简单方式实现微信头像功能相关推荐

  1. C# Winfrom Excel表格导出 Aspose.Cells超简单方式

    C# Winfrom Excel表格导出 Aspose.Cells超简单方式 首先需要下载 Aspose.Cells.dll,Aspose.Slides.dll,Aspose.Words.dll 这三 ...

  2. python做视频特效_python实现超简单的视频对象提取功能

    视频对象提取 与其说是视频对象提取,不如说是视频颜色提取,因为其本质还是使用了OpenCV的HSV颜色物体检测.下面话不多说了,来一起看看详细的介绍吧. HSV介绍 HSV分别代表,色调(H:hue) ...

  3. 汉字转拼音(纯html实现),JS实现超简单的汉字转拼音功能示例

    本文实例讲述了JS实现超简单的汉字转拼音功能.分享给大家供大家参考,具体如下: /** * 中文字符串转化为拼音 * alert(convertToPinyinLower("你好啊" ...

  4. ubuntu 下超简单的安装微信,QQ等软件

    Ubuntu 下超简单安装微信 QQ 等 Wine 软件的方法: 第一步,在终端中使用如下命令,添加软件仓库并自动刷新缓存: wget -O- https://deepin-wine.i-m.dev/ ...

  5. 将彩色图像变为素描图像,超简单!

    下面来谈谈彩色图像变为素描图像的原理,超简单! 如下为彩色原图: 第一步:图像灰度化,得到图像gray: 第二步:将灰度图像反色,得到反色图像: 第三步:对反色色图像进行高斯模糊处理,得到反色高斯图像 ...

  6. 2022 超有风格的微信头像,快换上吧

    以下内容来自公众号逆锋起笔,关注每日干货及时送达 上一期的头像,非常受欢迎,看到很多圈友都已经换上了,今天推荐一批不一样风格的,另外大家有需求可以在本文留言哈,小编来安排~ ﹡图源网络,如侵联删 20 ...

  7. android超简单方式实现镂空字体TextView

    概述 实现镂空字体用到的知识点是PorterDuffXfermode,它的作用是可以决定多个图像叠加时的显示模式.关于PorterDuffXfermode的一些说明可以参考下这篇文章: android ...

  8. php360随机壁纸图片接口,PHP超简单的随机图片头像API接口网站搭建教程

    摘要 向上文所说,为了其他邮箱调用随机图片,我当时特意去网上找了些随机图片API接口网站,发现他们API接口有点慢,本着不外链的精神,自己搭建了个随机图片and头像的API接口.在这里把教程分享给大家 ...

  9. node简单实现一个更改头像功能

    前言 一直想写这篇文章,无奈由于要考试的原因,一直在复习,拖延到现在才写?,之前用 node 的 express 框架写了个小项目,里面有个上传图片的功能,这里记录一下如何实现(我使用的是 ejs)? ...

最新文章

  1. 树状数组 poj 2352
  2. 移动web开发---第二天
  3. python的优点和缺点-python的优点和缺点是什么?
  4. JUC:ConcurrentHashMap(并发容器)
  5. 【英语学习】【Daily English】U04 Neighborhood L01 Anything else around?
  6. 如何实现SSID白名单管控
  7. fanuc系统md和mf区别_法兰克FANUC加工中心MF系统如何调取机床程序?
  8. 阿铭Linux_网站维护学习笔记201903019
  9. python导入数据画折线图_Python学习第93课-导入csv数据并绘制折线图
  10. 跟同事聊工作,不要用哦,可以用收到、明白、好、知道等词代替
  11. word2016画函数曲线图
  12. 听说唯一性索引搜索快?
  13. metaq入门部署到实战
  14. 牛客练习赛87 B k小数查询(STL)
  15. 读A Fast Single Image Haze Removal Algorithm Using Color Attenuation Prior
  16. 图着色问题 (25分)
  17. [Samba]源码安装samba
  18. 关键词提取(keyword extraction)技术
  19. JAVA EE程序员必读书籍大全
  20. Windows 补丁: GDR 与 QFE 的区别

热门文章

  1. 【codecs】视频显示分辨率格式分析
  2. mac上免费的音频剪辑软件在哪里可以快速下载
  3. AD ADSI入门
  4. window常用自带工具
  5. Lync问题解决之在Lync客户端中,无法查看到聊天记录
  6. 【转载】刘未鹏的C++学习历程 + 小诗一首
  7. USB调试时出现“because an app is obscuring a permission request settings cab't verify your resonse”问题
  8. 程序员如何提升自身价值
  9. 程序员,你需要些“脑力运动”了
  10. 为第九大股东;此前40次增持民生银行A股股份