#import <UIKit/UIKit.h>
@interface UIImage (Extension)
//颜色值换成image
+ (UIImage*)createImageWithColor:(UIColor*)color;
//画圆角矩形图
+ (UIImage *)createrRectangleImageWithColor:(UIColor *)color width:(float)width height:(float)height cornerRadius:(float)radius;
//图片处理
+(UIImage*)getSubImage:(UIImage *)image mCGRect:(CGRect)mCGRect centerBool:(BOOL)centerBool;
//按比例缩放图片
-(UIImage*)scaledImageWithNewSize:(CGSize)newSize;
//生成二维码图片
+ (UIImage *)qrImageForString:(NSString *)string imageSize:(CGFloat)size;
@end
#import "UIImage+Extension.h"
#import "QRCodeGenerator.h"
@implementation UIImage (Extension)
+ (UIImage*)createImageWithColor:(UIColor*) color
{
CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage*theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
+ (UIImage *)createrRectangleImageWithColor:(UIColor *)color width:(float)width height:(float)height cornerRadius:(float)radius
{
CGRect rect=CGRectMake(0.0f, 0.0f, width, height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context,1,1,1,0);//画笔线的颜色
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextMoveToPoint(context, width, height-radius*2); // 开始坐标右边开始
CGContextAddArcToPoint(context, width, height, width-radius*2, height, radius); // 右下角角度
CGContextAddArcToPoint(context, 0, height, 0, height-radius*2, radius); // 左下角角度
CGContextAddArcToPoint(context, 0, 0, width-radius*2, 0, radius); // 左上角
CGContextAddArcToPoint(context, width, 0, width, height-radius*2, radius); // 右上角
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFillStroke); //根据坐标绘制路径
UIImage*theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
-(UIImage*)scaledImageWithNewSize:(CGSize)newSize
{
// Create a graphics image context
UIGraphicsBeginImageContext(newSize);
// Tell the old image to draw in this new context, with the desired
// new size
[self drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Get the new image from the context
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
// End the contex
UIGraphicsEndImageContext();
// Return the new image.
return newImage;
}
//图片处理
+(UIImage*)getSubImage:(UIImage *)image mCGRect:(CGRect)mCGRect centerBool:(BOOL)centerBool
{
/*如若centerBool为Yes则是由中心点取mCGRect范围的图片*/
float imgwidth = image.size.width;
float imgheight = image.size.height;
float viewwidth = mCGRect.size.width;
float viewheight = mCGRect.size.height;
CGRect rect;
if(centerBool)
rect = CGRectMake((imgwidth-viewwidth)/2, (imgheight-viewheight)/2, viewwidth, viewheight);
else{
if (viewheight < viewwidth) {
if (imgwidth <= imgheight) {
rect = CGRectMake(0, 0, imgwidth, imgwidth*viewheight/viewwidth);
}else {
float width = viewwidth*imgheight/viewheight;
float x = (imgwidth - width)/2 ;
if (x > 0) {
rect = CGRectMake(x, 0, width, imgheight);
}else {
rect = CGRectMake(0, 0, imgwidth, imgwidth*viewheight/viewwidth);
}
}
}else {
if (imgwidth <= imgheight) {
float height = viewheight*imgwidth/viewwidth;
if (height < imgheight) {
rect = CGRectMake(0, 0, imgwidth, height);
}else {
rect = CGRectMake(0, 0, viewwidth*imgheight/viewheight, imgheight);
}
}else {
float width = viewwidth*imgheight/viewheight;
if (width < imgwidth) {
float x = (imgwidth - width)/2 ;
rect = CGRectMake(x, 0, width, imgheight);
}else {
rect = CGRectMake(0, 0, imgwidth, imgheight);
}
}
}
}
CGImageRef subImageRef = CGImageCreateWithImageInRect(image.CGImage, rect);
CGRect smallBounds = CGRectMake(0, 0, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));
UIGraphicsBeginImageContext(smallBounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, smallBounds, subImageRef);
UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
UIGraphicsEndImageContext();
return smallImage;
}
+ (UIImage *)qrImageForString:(NSString *)string imageSize:(CGFloat)size
{
return [QRCodeGenerator qrImageForString:string imageSize:size];
}
@end