UIImage类别

#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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • category and extension的官方文档 类别的作用 将类的实现分散到多个不同文件或多个不同框架中:...
    磁针石阅读 9,638评论 0 2
  • 面试时碰到几次,类别中如何添加属性。说实话,平时这个几乎不用,也没怎么查过相关文档,对此不甚了解,也就知道匿名类别...
    CoderCurtis阅读 534评论 0 0
  • 在Objective-C中,给一个类扩展一个其它方法,有两种实现方式:类别和继承。 1.继承Inherit 这个是...
    不吃饭会饿阅读 366评论 1 1
  • 品牌选择: 使用中药贴膏>认为外用药无差别>四个核心优势转换认知>认为泽普思阵痛效果好,安全性好,可以长期使用>颈...
    Y杨丽阅读 266评论 0 0
  • 一、类别 类别就是类的扩展,在不改变类的结构的同时,可以像类中添加方法(不能添加成 员变量) 二...
    Cheriez阅读 4,093评论 0 1