//
// ViewController.m
// test_btn_image_01
//
// Created by cdd on 16/7/4.
// Copyright © 2016年 jeffasd. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//
//
//
// button.frame = CGRectMake(100, 200, 200, 80);
// button.backgroundColor = [UIColor cyanColor];
//
// UIImage *btnImage = [UIImage imageNamed:@"right"];
//
// [button setImage:[UIImage imageNamed:@"right"] forState:UIControlStateNormal];
//
// [button setTitle:@"发布" forState:UIControlStateNormal];
//
//
[button setImageEdgeInsets:UIEdgeInsetsMake(0, btnImage.size.width, 0, btnImage.size.width)];
//
// [button setImageEdgeInsets:UIEdgeInsetsMake(0, btnImage.size.width, 0, -btnImage.size.width)];
//
// [self.view addSubview:button];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// [self cropImage:50 Image:[self imageWithColor:[UIColor cyanColor]]];
[self cropImage:50 UIColor:[UIColor cyanColor]];
}
- (UIImage *)cropImage:(CGFloat)width UIColor:(UIColor *)color {
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, width), NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
// CGContextSaveGState(context);
CGContextSetFillColorWithColor(context, [UIColor cyanColor].CGColor);
CGContextFillRect(context, CGRectMake(0, 0, width, width));
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, width), NO, 0.0);
CGContextRef ctx=UIGraphicsGetCurrentContext();
// CGContextRestoreGState(context);
CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, width, width));
CGContextClip(ctx);
// [image drawAtPoint:CGPointMake(0, 0)];
CGContextDrawImage(ctx, CGRectMake(0, 0, width, width), image.CGImage);
UIImage *newimage = UIGraphicsGetImageFromCurrentImageContext();
return newimage;
#if 0
CGFloat borderW = 0;
// 1.开启一个和原始图片一样大小的位图上下文.
CGSize size = CGSizeMake(image.size.width + 2 *borderW, image.size.height + 2 * borderW);
UIGraphicsBeginImageContextWithOptions(size,NO,0);
CGContextRef context = UIGraphicsGetCurrentContext();
// 2.绘制一个大圆,填充
// UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, size.width, size.height)];
// [[UIColor blueColor] set];
// [path fill];
// 3.添加一个裁剪区域.
// UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(borderW, borderW, image.size.width, image.size.height)];
// [path addClip];
CGContextAddEllipseInRect(context, CGRectMake(borderW, borderW, image.size.width, image.size.height));
//
//
CGContextClip(context);
// 4.把图片绘制到裁剪区域当中.
[image drawAtPoint:CGPointMake(borderW, borderW)];
// 5.生成一张新图片.
UIImage *clipImage = UIGraphicsGetImageFromCurrentImageContext();
// 6.关闭上下文.
UIGraphicsEndImageContext();
return clipImage;
#endif
}
- (UIImage *)imageWithColor:(UIColor *)color{
CGFloat width = 50;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, width), NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor cyanColor].CGColor);
CGContextFillRect(context, CGRectMake(0, 0, width, width));
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
return image;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end