iOS 裁剪图片


//
//  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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值