• iOS开发 --制作圆形的头像(UIImage)


        有时候我们的应用需要登录,登录后的用户信息中有用户头像,以前使用的方形图片比较丑陋,现在基本所有的应用都是使用圆形都头像了,但是用户上传上来都图片不一定是圆形的(基本上都不是),这个时候就需要我们程序员来处理这些图片了,处理的方法有两种(根据需求),第一种是只要普通颜色的边框(无边框也可以)且圆形的头像、第二种是需要花纹或者其他图片的边框 且 圆形的头像。

    以下为学习者提供的文章,不能用于商业利益。

    一 、普通颜色的边框(无边框也可以)且圆形的头像

    代码:

    UIImage * image = [UIImage imageNamed:@"icon_huo"];
        UIImageView * imageV = self.imageView;
        imageV.layer.masksToBounds = YES;
        imageV.layer.cornerRadius =imageV.frame.size.width / 2 ;
        /**如果需要边框,请把下面2行注释打开*/
    //    imageV.layer.borderColor = [UIColor purpleColor].CGColor;
    //    imageV.layer.borderWidth = 10;
        imageV.image=  image;

       

    二、花纹或者其他图片的边框

    为了更好的开发,把制作圆形的头像功能封装起来,首先为UIIamge新建一个Gategory(分类)

    UIImage+XG.h 文件
    #import <UIKit/UIKit.h>
    
    @interface UIImage (XG)
    
    /**
     *  @param icon         头像图片名称
     *  @param borderImage  边框的图片名称
     *  @param border       边框大小
     *
     *  @return 圆形的头像图片
     */
    + (instancetype)imageWithIconName:(NSString *)icon borderImage:(NSString *)borderImage border:(int)border;
    @end
    UIImage+XG.m 文件
    #import "UIImage+XG.h"
    
    @implementation UIImage (XG)
    
    + (instancetype)imageWithIconName:(NSString *)icon borderImage:(NSString *)borderImage border:(int)border{
        //头像图片
        UIImage * image = [UIImage imageNamed:icon];
        //边框图片
        UIImage * borderImg = [UIImage imageNamed:borderImage];
        //
        CGSize size = CGSizeMake(image.size.width + border, image.size.height + border);
        
        //创建图片上下文
        UIGraphicsBeginImageContextWithOptions(size, NO, 0);
        
        //绘制边框的圆
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextAddEllipseInRect(context, CGRectMake(0, 0, size.width, size.height));
        
        //剪切可视范围
        CGContextClip(context);
        
        //绘制边框图片
        [borderImg drawInRect:CGRectMake(0, 0, size.width, size.height)];
    
        //设置头像frame
        CGFloat iconX = border / 2;
        CGFloat iconY = border / 2;
        CGFloat iconW = image.size.width;
        CGFloat iconH = image.size.height;
        
        //绘制圆形头像范围
        CGContextAddEllipseInRect(context, CGRectMake(iconX, iconY, iconW, iconH));
        
        //剪切可视范围
        CGContextClip(context);
        
        //绘制头像
        [image drawInRect:CGRectMake(iconX, iconY, iconW, iconH)];
        
        //取出整个图片上下文的图片
        UIImage *iconImage = UIGraphicsGetImageFromCurrentImageContext();
        
        return iconImage;
    }
    @end
    
    

    效果:

    在需要制作圆形头像或图片的地方导入   #import "UIImage+XG.h"

    
    
    UIImage * image = [UIImage imageWithIconName:@"icon_huo" borderImage:@"border" border:40];
    
    self.imageView.image=  image;
    
    
    
     
  • 相关阅读:
    k8S 搭建集群
    K8S搭建
    华为交换机常用命令
    JSP中动态include和静态include的区别
    什么情况下调用doGet()和doPost()
    get和post的区别
    什么是spring框架
    spring的作用
    什么是DI
    依赖注入的三种实现方式
  • 原文地址:https://www.cnblogs.com/qq9070/p/4545251.html
Copyright © 2020-2023  润新知