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
时间: 2024-08-29 15:49:35

ios-裁剪加裁剪描边加把裁剪封装成一个方法类的相关文章

iOS-截图和把截图封装成一个方法

// // UIImage+Tools.h // 截屏 // // Created by YaguangZhu on 15/9/10. // Copyright (c) 2015年 YaguangZhu. All rights reserved. // #import <UIKit/UIKit.h> @interface UIImage (Tools) +(instancetype)imageWithCaptureView:(UIView *)view; @end // // UIImage+

iOS开发UI篇—懒加载

iOS开发UI篇—懒加载 1.懒加载基本 懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小).所谓懒加载,写的是其get方法. 注意:如果是懒加载的话则一定要注意先判断是否已经有了,如果没有那么再去进行实例化 2.使用懒加载的好处: (1)不必将创建对象的代码全部写在viewDidLoad方法中,代码的可读性更强 (2)每个控件的getter方法中分别负责各自的实例化处理,代码彼此之间的独立性强,松耦合 3.代码示例 1 // 2 // YYViewController.m 3

ios 图片的两种加载方式

控件加载图片,plist,懒加载,序列帧动画,添加动画效果. IOS中有2种加载图片的方式. 方式一:有缓存(图片所占用的内存会一直停留在程序中) [objc] view plaincopy + (UIImage *)imageNamed:(NSString *)name; 注:必须是png文件.需要把图片添加到 images.xcassets中 例如: [objc] view plaincopy @property (weak, nonatomic) IBOutlet UIImageView 

iOS开发UI中懒加载的使用方法

1.懒加载基本 懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小).所谓懒加载,写的是其getter方法.说的通俗一点,就是在开发中,当程序中需要利用的资源时.在程序启动的时候不加载资源,只有在运行当需要一些资源时,再去加载这些资源. 我们知道iOS设备的内存有限,如果在程序在启动后就一次性加载将来会用到的所有资源,那么就有可能会耗尽iOS设备的内存.这些资源例如大量数据,图片,音频等等,所以我们在使用懒加载的时候一定要注意先判断是否已经有了,如果没有那么再去进行实例化 2.使

iOS开发UI基础—懒加载

iOS开发UI基础-懒加载 1.懒加载基本 懒加载--也称为延迟加载,即在需要的时候才加载(效率低,占用内存小).所谓懒加载,写的是其get方法. 注意:如果是懒加载的话则一定要注意先判断是否已经有了,如果没有那么再去进行实例化 2.使用懒加载的好处: (1)不必将创建对象的代码全部写在viewDidLoad方法中,代码的可读性更强 (2)每个控件的getter方法中分别负责各自的实例化处理,代码彼此之间的独立性强,松耦合 3.代码示例 1 // 2 // YYViewController.m

iOS网络编程(三) 异步加载及缓存图片----&gt;SDWebImage

@SDWebImage提供一个UIImageView的类别以支持加载来自网络的远程图片.具有缓存管理.异步下载.同一个URL下载次数控制和优化等特征. @SDWebImage的导入1.https://github.com/rs/SDWebImage 下载SDWebImage开源包2.将类包拖入工程,再导入MapKit.framework.ImageIO.framework两个框架3.SDWebImage是支持ARC的,在MRC的工程中要注意,可参考MRC工程配置ARC4.注意:SDWebImag

iOS开发网络篇 —— OC加载HTML代码

html代码 图1 样式一:"<p><img src=\"/upload/image/20170609/1496978712941664.jpg\" title=\"1496978712941664.jpg\" alt=\"7.jpg\"/>测试内容信息无错</p>" 样式二:<h1 style=\"font-size: 32px; font-weight: bold; bo

iOS下使用SHA1WithRSA算法加签源码

首先了解一下几个相关概念,以方便后面遇到的问题的解决: RSA算法:1977年由Ron Rivest.Adi Shamirh和LenAdleman发明的,RSA就是取自他们三个人的名字.算法基于一个数论:将两个大素数相乘非常容易,但要对这个乘积的结果进行因式分解却非常困难,因此可以把乘积公开作为公钥.该算法能够抵抗目前已知的所有密码攻击.RSA算法是一种非对称算法,算法需要一对密钥,使用其中一个加密,需要使用另外一个才能解密.我们在进行RSA加密通讯时,就把公钥放在客户端,私钥留在服务器. PE

android Listview分批加载+自动加载(附源码下载)

直接上代码,代码有注释: public class TestForListviewActivity extends Activity implements OnScrollListener { private ListView mListview = null; private View mFooterView; private PaginationAdapter mAdapter; private Handler handler=new Handler(); private boolean i