zoukankan      html  css  js  c++  java
  • iOS_UIImage_裁切圆形头像

    github地址: https://github.com/mancongiOS/UIImage.git

    UIImage的Cagetory

    UIImage+ImageCircle.h

    - (UIImage *)imageClicpCircleWithRect:(CGRect)rect;

    UIImage+ImageCircle.m

    #import "UIImage+ImageCircle.h"
    
    @interface View : UIView
    @property (nonatomic, strong) UIImage * image;
    @end
    
    @implementation View
    
    - (void)drawRect:(CGRect)rect {
        
        CGContextRef contextRef = UIGraphicsGetCurrentContext();
        CGContextSaveGState(contextRef);
        
        // Ellipse --> 椭圆的
        CGContextAddEllipseInRect(contextRef, CGRectMake(rect.size.width / 4, rect.size.height / 4, rect.size.width / 2, rect.size.height / 2));
        CGContextClip(contextRef);
        CGContextFillPath(contextRef);
        [self.image drawAtPoint:CGPointMake(100, 0)];
        
        CGContextRestoreGState(contextRef);
    }
    @end
    
    @implementation UIImage (ImageCircle)
    
    - (UIImage *)imageClicpCircleWithRect:(CGRect)rect {
        
        View * myView = [[View alloc] init];
        myView.image = self;
        
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        
        myView.frame = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
        myView.backgroundColor = [UIColor orangeColor];
        [myView.layer renderInContext:context];
        
        UIImage * imageNew = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        return imageNew;
    }
    
    @end

    使用:

    - (UIImageView *)imageView {
        if (_imageView == nil) {
            self.imageView = [[UIImageView alloc] init];
            self.imageView.backgroundColor = [UIColor redColor];
            
            
            UIImage * image = [UIImage imageNamed:@"1.jpg"];
            // 裁剪出来一个在原图中心点,半径为四分之一原图宽高最小值的圆.
            
            CGFloat imageSizeMin = MIN(image.size.width, image.size.height);
            
            CGFloat circleImageWH = imageSizeMin;
            CGFloat circleImage_x = (image.size.width - circleImageWH) / 2;
            CGFloat circleImage_y = (image.size.height - circleImageWH) / 2;
            
    
            self.imageView.image = [image imageClicpCircleWithRect:CGRectMake(circleImage_x, circleImage_y, circleImageWH, circleImageWH)];
    
        } return _imageView;
    }
  • 相关阅读:
    BZOJ2741:[FOTILE模拟赛]L
    BZOJ3996:[TJOI2015]线性代数
    BZOJ3876:[AHOI2014]支线剧情
    BZOJ1861:[ZJOI2006]Book书架
    BZOJ3190:[JLOI2013]赛车
    HDU-1540 Tunnel Warfare 【线段树+单点修改+区间更新】
    HDU-1846 Brave Game 【巴什博弈】
    HDU-1421 搬寝室 【DP】
    HDU-4734 F(x) 【数位DP】
    AHU-412 Bessie Come Home 【Dijkstra】
  • 原文地址:https://www.cnblogs.com/mancong/p/6138104.html
Copyright © 2011-2022 走看看