zoukankan      html  css  js  c++  java
  • ios-裁剪加裁剪描边加把裁剪封装成一个方法类

    //
    //  UIImage+UItool.h
    //  图片裁剪
    //
    //  Created by YaguangZhu on 15/9/10.
    //  Copyright (c) 2015年 YaguangZhu. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface UIImage (UItool)
    + (instancetype)imageWithName:(NSString *)name border:(CGFloat)border borderColor:(UIColor *)color;
    
    @end
    
    //
    //  UIImage+UItool.m
    //  图片裁剪
    //
    //  Created by YaguangZhu on 15/9/10.
    //  Copyright (c) 2015年 YaguangZhu. All rights reserved.
    //
    
    #import "UIImage+UItool.h"
    
    @implementation UIImage (UItool)
    + (instancetype)imageWithName:(NSString *)name border:(CGFloat)border borderColor:(UIColor *)color
    {
        CGFloat borderW = border;
        UIImage *oldImage = [UIImage imageNamed:name];
        CGFloat imageW = oldImage.size.width + 2 * borderW;
        CGFloat imageH = oldImage.size.height+ 2 * borderW;
        
        CGFloat circirW = imageW > imageH ? imageH :imageW;
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(circirW, circirW), NO, 0.0);
        
        UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, circirW, circirW)];
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        
        CGContextAddPath(ctx, path.CGPath);
        [color set];
        
        CGContextFillPath(ctx);
        
        CGRect clipR = CGRectMake(borderW, borderW, oldImage.size.width, oldImage.size.height);
        
        UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:clipR];
        
        [clipPath addClip];
        
        [oldImage drawAtPoint:CGPointMake(borderW, borderW)];
        
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
        return newImage;
    }
    @end
    //
    //  ViewController.m
    //  图片裁剪
    //
    //  Created by YaguangZhu on 15/9/10.
    //  Copyright (c) 2015年 YaguangZhu. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "UIImage+UItool.h"
    
    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet UIImageView *ImageView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
       //白色圆环版
        _ImageView.image = [UIImage imageWithName:@"001" border:10 borderColor:[UIColor redColor]];
        
        
    }
    - (void)clipCrile
    {
        CGFloat borderW = 5;
        UIImage *oldImage = [UIImage imageNamed:@"001"];
        CGFloat imageW = oldImage.size.width + 2 * borderW;
        CGFloat imageH = oldImage.size.height+ 2 * borderW;
        
        CGFloat circirW = imageW > imageH ? imageH :imageW;
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(circirW, circirW), NO, 0.0);
        
        UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, circirW, circirW)];
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        
        CGContextAddPath(ctx, path.CGPath);
        [[UIColor whiteColor]set];
        
        CGContextFillPath(ctx);
        CGRect clipR = CGRectMake(borderW, borderW, oldImage.size.width, oldImage.size.height);
        
        UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:clipR];
        
        [clipPath addClip];
        
        [oldImage drawAtPoint:CGPointMake(borderW, borderW)];
        
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
        _ImageView.image = newImage;
    
    }
    - (void)clip
    {
        UIImage *oldImage = [UIImage imageNamed:@"001"];
        UIGraphicsBeginImageContextWithOptions(oldImage.size, NO, 0.0);
        UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, oldImage.size.width, oldImage.size.height)];
        [path addClip];
        
        [oldImage drawAtPoint:CGPointZero];
        
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
        
        _ImageView.image = newImage;
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
  • 相关阅读:
    01时间处理--02 指定日期--转成星期
    中文乱码处理
    判断js变量是否定义,
    安卓请求服务器js文件下载到本地,版本号不一致就下载
    a标签href跳转---传值---禁止单引号
    修改mysql配置文件,group_concat设置为最大.默认1024个字节字符串.多条json会超出
    GROUP_CONCAT()多条数据.拼接字符串 最大长度1024
    使用变量判断之前.务必先定义
    多种序列化器-指定类对象
    JsonHelper
  • 原文地址:https://www.cnblogs.com/zhuyaguang/p/4797100.html
Copyright © 2011-2022 走看看