123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- //
- // UIImage+ST.m
- // STImageHead
- //
- // Created by 沈兆良 on 16/2/25.
- // Copyright © 2016年 沈兆良. All rights reserved.
- //
- #import "UIImage+ST.h"
- @implementation UIImage (ST)
- /**
- * 绘制图片圆角
- *
- * @param image <#image description#>
- * @param borderWidth <#borderWidth description#>
- * @param color <#color description#>
- *
- * @return <#return value description#>
- */
- + (UIImage *)imageWithClipImage:(UIImage *)image borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)color
- {
- // 图片的宽度和高度
- CGFloat imageWH = image.size.width;
- // 设置圆环的宽度
- CGFloat border = borderWidth;
- // 圆形的宽度和高度
- CGFloat ovalWH = imageWH + 2 * border;
- // 1.开启上下文
- UIGraphicsBeginImageContextWithOptions(CGSizeMake(ovalWH, ovalWH), NO, 0);
- // 2.画大圆
- UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, ovalWH, ovalWH)];
- [color set];
- [path fill];
- // 3.设置裁剪区域
- UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(border, border, imageWH, imageWH)];
- [clipPath addClip];
- // 4.绘制图片
- [image drawAtPoint:CGPointMake(border, border)];
- // 5.获取图片
- UIImage *clipImage = UIGraphicsGetImageFromCurrentImageContext();
- // 6.关闭上下文
- UIGraphicsEndImageContext();
- return clipImage;
- }
- /**
- * 指定区域图片的截图
- *
- * @param sourceImage 原始图片
- * @param clipRect 截取范围
- *
- * @return 截图图片
- */
- + (UIImage *)imageWithSourceImage:(UIImage *)sourceImage
- clipRect:(CGRect)clipRect
- {
- CGImageRef imageRef = sourceImage.CGImage;
- CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, clipRect);
- CGSize imageSize = clipRect.size;
- UIGraphicsBeginImageContext(imageSize);
- CGContextRef context = UIGraphicsGetCurrentContext();
- CGContextDrawImage(context, clipRect, subImageRef);
- UIImage *clipImage = [UIImage imageWithCGImage:subImageRef];
- UIGraphicsEndImageContext();
- return clipImage;
- }
- /**
- * 指定图片的比例
- *
- * @param sourceImage <#sourceImage description#>
- * @param scaleSize <#scaleSize description#>
- *
- * @return <#return value description#>
- */
- + (UIImage *)imageWithSourceImage:(UIImage *)sourceImage
- scaleSize:(CGSize)scaleSize
- {
- // 创建一个bitmap的context
- // 并把它设置成为当前正在使用的context
- UIGraphicsBeginImageContext(scaleSize);
- // 绘制改变大小的图片
- [sourceImage drawInRect:CGRectMake(0, 0, scaleSize.width, scaleSize.height)];
- // 从当前context中创建一个改变大小后的图片
- UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
- // 使当前的context出堆栈
- UIGraphicsEndImageContext();
- // 返回新的改变大小后的图片
- return scaledImage;
- }
- /**
- * 截取指定位置的图片
- *
- * @param bounds <#bounds description#>
- *
- * @return <#return value description#>
- */
- - (UIImage *)croppedImage:(CGRect)bounds {
- CGFloat scale = MAX(self.scale, 1.0f);
- CGRect scaledBounds = CGRectMake(bounds.origin.x * scale+1, bounds.origin.y * scale+1, bounds.size.width * scale-2, bounds.size.height * scale-2);
- CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], scaledBounds);
- UIImage *croppedImage = [UIImage imageWithCGImage:imageRef scale:self.scale orientation:UIImageOrientationUp];
- CGImageRelease(imageRef);
- return croppedImage;
- }
- @end
|