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
  • 相关阅读:
    CentOS、Ubuntu、Debian三个linux比较异同
    中国版 Ubuntu Kylin 14.04 LTS 麒麟操作系统中文版发布下载 (Ubuntu天朝定制版)
    keras之save & reload model
    2.keras实现-->深度学习用于文本和序列
    迭代器 、生成器、可迭代对象
    numpy 中clip函数的使用
    1.keras实现-->自己训练卷积模型实现猫狗二分类(CNN)
    1.keras实现-->使用预训练的卷积神经网络(VGG16)
    L1正则化和L2正则化
    keras搭建深度学习模型的一些小tips
  • 原文地址:https://www.cnblogs.com/zhuyaguang/p/4797100.html
Copyright © 2011-2022 走看看