ios-裁剪加裁剪描邊加把裁剪封裝成一個方法類

來源:互聯網
上載者:User

標籤:

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

 

ios-裁剪加裁剪描邊加把裁剪封裝成一個方法類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.