UIImage 扩展

原创
2016/12/12 14:04
阅读数 12

UIImage+AGExpand.h

//
//  UIImage+AGExpand.h
//  LiveBai
//
//  Created by Angelo on 2016/12/12.
//  Copyright © 2016年 Angelo. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UIImage (AGExpand)


/**
 生成纯颜色图片

 @param color 图片颜色
 @param size 图片大小
 @return 生成的图片
 */
+ (UIImage *)ag_ImageWithColor:(UIColor *)color size:(CGSize)size;


/**
 压缩图片到指定大小

 @param size 压缩后图片大小
 */
- (UIImage*)ag_compressedImage:(CGSize)size;


/**
 截取图片

 @param rect 截取图片位置,大小
 @return 截取好的图片
 */
- (UIImage*)ag_cutImage:(CGRect)rect;


/**
 绘制圆形图片

 @param bordWidth 边框宽度
 @param bordColor 边框颜色
 @return 绘制后图片
 */
- (UIImage*)ag_circleImageWithBordWidth:(CGFloat)bordWidth bordColor:(UIColor*)bordColor;


@end

UIImage+AGExpand.m

//
//  UIImage+AGExpand.m
//  LiveBai
//
//  Created by Angelo on 2016/12/12.
//  Copyright © 2016年 Angelo. All rights reserved.
//

#import "UIImage+AGExpand.h"

@implementation UIImage (AGExpand)


+ (UIImage *)ag_ImageWithColor:(UIColor *)color size:(CGSize)size{
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context,color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}


- (UIImage*)ag_compressedImage:(CGSize)size{
    UIGraphicsBeginImageContext(size);
    [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *newImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImg;
}

- (UIImage *)ag_cutImage:(CGRect)rect{
    CGImageRef imgRef = CGImageCreateWithImageInRect(self.CGImage, rect);
    CGSize size = rect.size;
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage (context, rect, imgRef);
    UIImage * smallImage = [UIImage imageWithCGImage:imgRef];
    UIGraphicsEndImageContext ();
    return smallImage;
}


- (UIImage *)ag_circleImageWithBordWidth:(CGFloat)bordWidth bordColor:(UIColor *)bordColor{
    CGFloat imgWidth = self.size.width + 2*bordWidth;
    CGFloat imgHeight = self.size.height + 2*bordWidth;
    CGSize imgSize = CGSizeMake(imgWidth, imgHeight);
    UIGraphicsBeginImageContextWithOptions(imgSize, NO, 0.0);
    CGContextRef ctr = UIGraphicsGetCurrentContext();
    [bordColor set];
    CGFloat bigRadius = imgHeight / 2.0;
    //大圆
    CGContextAddArc(ctr, bigRadius, bigRadius, bigRadius, 0, M_PI * 2, 0);
    CGContextFlush(ctr);
    //小圆
    CGContextAddArc(ctr, bigRadius, bigRadius, bigRadius - bordWidth, 0, M_PI * 2, 0);
    CGContextClip(ctr);
    [self drawInRect:CGRectMake(bordWidth, bordWidth, self.size.width, self.size.height)];
    UIImage *newImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImg;
}

@end

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部