iOS 裁剪图片
来源:互联网 发布:今日头条数据运营待遇 编辑:程序博客网 时间:2024/05/19 22:27
//// 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
0 0
- ios裁剪图片
- iOS 裁剪图片
- IOS 裁剪图片
- ios图片裁剪
- ios 代码裁剪图片
- IOS 裁剪图片
- iOS 图片裁剪 缩放
- iOS 裁剪图片
- iOS裁剪图片
- iOS- 裁剪图片
- iOS-图片裁剪
- iOS 裁剪图片
- iOS图片裁剪
- ios 图片裁剪
- iOS 裁剪图片
- iOS-裁剪图片
- iOS-有关图片裁剪
- ios 图片裁剪
- REST学习总结
- iOS 常用正则表达式一览表
- io学习之一
- 孤儿进程与僵尸进程
- XenCenter 创建 New VM
- iOS 裁剪图片
- C# 处理批量插入数据(事物) mysql oracle sqlserver三个版本
- 天体物理 —— 开普勒三大定律
- error lnk1158 无法运行cvtres.exe
- Oracle体系结构详细图解
- 不使用库实现栈的基本操作
- CentOS下nano编辑器使用教程
- 对于java和前端方面自己的看法
- 隐藏Activity到后台而不关闭