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
  • 相关阅读:
    关于IE6不能兼容LCUC使用的PNG透明图象
    rmvb压制中高级技巧
    不错的课件网站
    C#程序多用户只启动一个进程的方法
    不可想像的加密光盘复制工具
    检测远程URL是否存在的三种方法
    请哪里有英文单词单复数转换的代码?
    一些感想,欢迎拍砖
    Some thoughts on my own O/R Mapping or Code Generation tools
    有了net send,谁还用IM?
  • 原文地址:https://www.cnblogs.com/zhuyaguang/p/4797100.html
Copyright © 2011-2022 走看看