#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); // 開(kāi)始坐標(biāo)右邊開(kāi)始
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); //根據(jù)坐標(biāo)繪制路徑
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則是由中心點(diǎn)取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