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);  // 開(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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • category and extension的官方文檔 類別的作用 將類的實(shí)現(xiàn)分散到多個(gè)不同文件或多個(gè)不同框架中:...
    磁針石閱讀 9,686評(píng)論 0 2
  • 面試時(shí)碰到幾次,類別中如何添加屬性。說(shuō)實(shí)話,平時(shí)這個(gè)幾乎不用,也沒(méi)怎么查過(guò)相關(guān)文檔,對(duì)此不甚了解,也就知道匿名類別...
    CoderCurtis閱讀 578評(píng)論 0 0
  • 在Objective-C中,給一個(gè)類擴(kuò)展一個(gè)其它方法,有兩種實(shí)現(xiàn)方式:類別和繼承。 1.繼承Inherit 這個(gè)是...
    不吃飯會(huì)餓閱讀 397評(píng)論 1 1
  • 品牌選擇: 使用中藥貼膏>認(rèn)為外用藥無(wú)差別>四個(gè)核心優(yōu)勢(shì)轉(zhuǎn)換認(rèn)知>認(rèn)為澤普思陣痛效果好,安全性好,可以長(zhǎng)期使用>頸...
    Y楊麗閱讀 317評(píng)論 0 0
  • 一、類別 類別就是類的擴(kuò)展,在不改變類的結(jié)構(gòu)的同時(shí),可以像類中添加方法(不能添加成 員變量) 二...
    Cheriez閱讀 4,197評(píng)論 0 1

友情鏈接更多精彩內(nèi)容