一、使用UIImage和CGImage處理圖位
可用通過(guò)Quartz的圖片對(duì)象或原始圖片數(shù)據(jù)來(lái)創(chuàng)建UIImage。UIImage還提供了一些方法,允許將自身繪制到繪圖Context中。
1、顯示動(dòng)畫(huà)
UIImage支持的各種圖片格式:
標(biāo)簽圖像文件格式(TIFF) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .tiff、.tif
JPEG(Joint Photographic Experts Group)格式 ? ?.jpg、.jpeg
GIF(Graphic Interchange Format)格式 ? ? ? ? ? ? ? .gif
PNG(Portable Network Graphic)格式 ? ? ? ? ? ? ? ? .png
Windows位圖格式(DIB)格式 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?.bmp、.BMPf
Windows圖標(biāo)格式 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?.ico
Windows光標(biāo)格式 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?.cur
Window系統(tǒng)位圖 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?.xbm
當(dāng)系統(tǒng)內(nèi)存緊張時(shí),UIImage會(huì)將圖片數(shù)據(jù)從UIImage對(duì)象中清理出去,以節(jié)省系統(tǒng)內(nèi)存,這里的清理行為只是清理UIImage內(nèi)部存儲(chǔ)的圖片數(shù)據(jù),并不清理UIImage對(duì)象本身。當(dāng)程序使用一個(gè)圖片數(shù)據(jù)被清理過(guò)的UIImage對(duì)象時(shí),該UIImage將會(huì)自動(dòng)從原始的圖片文件中加載圖片數(shù)據(jù)。
盡量避免使用UIImage加載過(guò)大(如大雨1024 * 1024)的圖片
用UIimage來(lái)顯示靜止的圖片,通常用一下方法:
+imageNamed:加載指定文件名對(duì)應(yīng)的圖片,該方法有緩存機(jī)制,用該方法加載的文件不存在,它才會(huì)去加載圖片文件并緩存它;如果已經(jīng)緩存過(guò)指定圖片,該方法直接使用已緩存的圖片。
當(dāng)程序需要頻繁加載、卸載圖片文件時(shí),不要使用該方法來(lái)加載圖片
+imageWithContentsOfFile:該方法用于加載指定文件名對(duì)應(yīng)圖片。
+imageWithData:該方法用于根據(jù)NSData中封裝的圖片數(shù)據(jù)來(lái)創(chuàng)建圖片。
+imageWithData:scale:該方法用于根據(jù)NSData中封裝的圖片數(shù)據(jù)來(lái)創(chuàng)建圖片,并按指定縮放因子對(duì)圖片進(jìn)行縮放。
+imageWithCGImage:該方法用于根據(jù)指定的CGImageRef對(duì)象來(lái)創(chuàng)建UIImage。
+imageWithCGImage:scale:orientation:該方法用于根據(jù)指定的CGImageRef對(duì)象來(lái)創(chuàng)建UIImage,按照指定scale來(lái)對(duì)圖片進(jìn)行縮放,按照orientation參數(shù)對(duì)圖片進(jìn)行旋轉(zhuǎn)、鏡像等。
+animatedImageNamed:duration:根據(jù)指定圖片名來(lái)加載一系列圖片
+animatedImageWithImages:duration:傳入一個(gè)每個(gè)對(duì)象都是UIImage對(duì)象的數(shù)組,作為多張動(dòng)畫(huà)圖片。
2、CGImage與CGImageRef
UIImage雖然可以加載、顯示各種格式的位圖,甚至可以同時(shí)加載圖片、并依次播放多張圖片形成動(dòng)畫(huà),但是它不能對(duì)圖片進(jìn)行縮放、旋轉(zhuǎn),不能截取指定區(qū)域等,這些功能可借助Quartz2D的CGImageRef來(lái)實(shí)現(xiàn)。
UIImage與CGImageRef可以相互轉(zhuǎn)換。CGImageRef只是一個(gè)指針類(lèi)型。
二、Quartz2D繪圖
使用Quartz2D繪圖的關(guān)鍵步驟有兩步:獲取CGContextRef;調(diào)用CGContextRef方法進(jìn)行繪圖。
1、Quartz2D提供的繪圖函數(shù):
CGContextClearRect(CGContectRef c, CGRect rect) //擦除指定區(qū)域繪制圖形
CGContextDrawPath(CGContextRef? _Nullable c, CGPathDrawingMode mode) //使用指定模式繪制當(dāng)前CGContextRef所包含的路徑。第二個(gè)參數(shù)支持KCGPathFill、kCGPathEOFill、kCGPathStroke、kCGPathFillStroke、kCGPathEOFillStroke等枚舉值
CGContextEOFillPath(CGContextRef? _Nullable c) //使用奇偶規(guī)則來(lái)填充該路徑的區(qū)域,奇偶規(guī)則是指:如果某個(gè)點(diǎn)被路徑包圍了奇數(shù)次,name系統(tǒng)繪制該點(diǎn);如果是偶數(shù)次則不繪制
CGContextFillPath(CGContextRef? _Nullable c)//填充CGContextRef所包含的路徑包圍的區(qū)域
CGContextFillRect(CGContextRef? _Nullable c, CGRect rect)//填充rect所代表的矩形
CGContextFillRects(CGContextRef? _Nullable c, const CGRect * _Nullable rects, size_t count)//填充多個(gè)矩形
CGContextFillEllipseInRect(CGContextRef? _Nullable c, CGRect rect)//填充rect矩形的內(nèi)切橢圓區(qū)域
CGContextStrokePath(CGContextRef? _Nullable c)//使用當(dāng)前CGContextRef設(shè)置的線(xiàn)寬繪制路徑
CGContextStrokeRect(CGContextRef? _Nullable c, CGRect rect)//使用當(dāng)前CGContextRef設(shè)置的繪制矩形框
CGContextStrokeRectWithWidth(CGContextRef? _Nullable c, CGRect rect, CGFloat width)//使用指定線(xiàn)寬繪制矩形
CGContextReplacePathWithStrokedPath(CGContextRef? _Nullable c)//使用繪制當(dāng)前路徑時(shí)覆蓋的區(qū)域作為當(dāng)前CGContextRef中的新路徑。
CGContextStrokeEllipseInRect(CGContextRef? _Nullable c, CGRect rect)//使用當(dāng)前CGContextRef設(shè)置的線(xiàn)寬繪制rect矩形的內(nèi)切圓
CGContextStrokeLineSegments(CGContextRef? _Nullable c, const CGPoint * _Nullable points, size_t count)//使用當(dāng)前CGContextRef設(shè)置的線(xiàn)寬繪制多條線(xiàn)段。
2、設(shè)置繪圖屬性的相關(guān)函數(shù)
CGContextSaveGState(CGContextRef? _Nullable c)//保存CGContextRef當(dāng)前的繪圖狀態(tài),方便以后恢復(fù)該狀態(tài)
CGContextRestoreGState(CGContextRef? _Nullable c)//把CGContextRef的狀態(tài)恢復(fù)到最近一次保存時(shí)的狀態(tài)
CGContextGetInterpolationQuality(CGContextRef? _Nullable c)//獲取當(dāng)前CGContextRef在放大圖片的插值質(zhì)量
CGContextSetInterpolationQuality(CGContextRef? _Nullable c, CGInterpolationQuality quality)//設(shè)置當(dāng)前CGContextRef在放大圖片的插值質(zhì)量
CGContextSetLineCap(CGContextRef? _Nullable c, CGLineCap cap)//設(shè)置線(xiàn)段端點(diǎn)的繪制形狀。
CGContextSetLineDash(CGContextRef? _Nullable c, CGFloat phase, const CGFloat * _Nullable lengths, size_t count)//繪制邊框時(shí)所用的點(diǎn)線(xiàn)模式
CGContextSetLineJoin(CGContextRef? _Nullable c, CGLineJoin join)//設(shè)置線(xiàn)條連接點(diǎn)的風(fēng)格
CGContextSetLineWidth(CGContextRef? _Nullable c, CGFloat width)//設(shè)置繪制直線(xiàn)、邊框時(shí)的線(xiàn)寬
CGContextSetMiterLimit(CGContextRef? _Nullable c, CGFloat limit)//把連接點(diǎn)風(fēng)格設(shè)置為meter風(fēng)格時(shí),該函數(shù)用于控制銳角箭頭的長(zhǎng)度
CGContextSetPatternPhase(CGContextRef? _Nullable c, CGSize phase)//設(shè)置該CGContextRef采用位圖填充的相位
CGContextSetFillPattern(CGContextRef? _Nullable c, CGPatternRef? _Nullable pattern, const CGFloat * _Nullable components)//設(shè)置該CGContextRef使用位圖填充
CGContextSetShouldAntialias(CGContextRef? _Nullable c, bool shouldAntialias)//設(shè)置該CGContextRef是否應(yīng)該抗鋸齒
CGContextSetStrokePattern(CGContextRef? _Nullable c, CGPatternRef? _Nullable pattern, const CGFloat * _Nullable components)//設(shè)置該CGContextRef使用位圖繪制線(xiàn)條、邊框
CGContextSetBlendMode(CGContextRef? _Nullable c, CGBlendMode mode)//設(shè)置CGContextRef的疊加模式。
CGContextSetAllowsAntialiasing(CGContextRef? _Nullable c, bool allowsAntialiasing)//設(shè)置該CGContextRef是否允許抗鋸齒
CGContextSetAllowsFontSmoothing(CGContextRef? _Nullable c, bool allowsFontSmoothing)//設(shè)置該CGContextRef是否允許光滑字體
CGContextSetShouldSmoothFonts(CGContextRef? _Nullable c, bool shouldSmoothFonts)//設(shè)置該CGContextRef是否應(yīng)該光滑字體
CGContextSetAlpha(CGContextRef? _Nullable c, CGFloat alpha)//設(shè)置全局透明度
CGContextSetCMYKFillColor(CGContextRef? _Nullable c, CGFloat cyan, CGFloat magenta, CGFloat yellow, CGFloat black, CGFloat alpha)//使用CMYK顏色模式來(lái)設(shè)置該CGContextRef的填充顏色
CGContextSetCMYKStrokeColor(CGContextRef? _Nullable c, CGFloat cyan, CGFloat magenta, CGFloat yellow, CGFloat black, CGFloat alpha)//使用CMYK顏色模式來(lái)設(shè)置該CGContextRef的線(xiàn)條顏色
CGContextSetFillColorWithColor(CGContextRef? _Nullable c, CGColorRef? _Nullable color)//使用指定顏色來(lái)實(shí)現(xiàn)該CGContextRef的填充顏色
CGContextSetShadowWithColor(CGContextRef? _Nullable c, CGSize offset, CGFloat blur, CGColorRef? _Nullable color)//使用指定顏色來(lái)實(shí)現(xiàn)該CGContextRef的線(xiàn)條顏色
CGContextSetGrayFillColor(CGContextRef? _Nullable c, CGFloat gray, CGFloat alpha)//使用灰色來(lái)實(shí)現(xiàn)該CGContextRef的填充顏色
CGContextSetGrayStrokeColor(CGContextRef? _Nullable c, CGFloat gray, CGFloat alpha)//使用灰色來(lái)實(shí)現(xiàn)該CGContextRef的線(xiàn)條顏色
CGContextSetRGBFillColor(CGContextRef? _Nullable c, CGFloat red, CGFloat green, CGFloat blue, CGFloat alpha)//使用RGB顏色來(lái)實(shí)現(xiàn)該CGContextRef的填充顏色
CGContextSetRGBStrokeColor(CGContextRef? _Nullable c, CGFloat red, CGFloat green, CGFloat blue, CGFloat alpha)//使用RGB顏色來(lái)實(shí)現(xiàn)該CGContextRef的線(xiàn)條顏色
CGContextSetShadow(<#CGContextRef? _Nullable c#>, <#CGSize offset#>, <#CGFloat blur#>)////設(shè)置陰影在x、y軸上的偏移,以及模糊度
CGContextSetShadowWithColor(<#CGContextRef? _Nullable c#>, <#CGSize offset#>, <#CGFloat blur#>, <#CGColorRef? _Nullable color#>)//設(shè)置陰影在x、y軸上的偏移,以及模糊度和陰影的顏色
3、點(diǎn)線(xiàn)模式繪制
CGContextSetLineDash(CGContextRef? _Nullable c, CGFloat phase, const CGFloat * _Nullable lengths, size_t count)//點(diǎn)線(xiàn)模式繪制
4、繪制文本
CGContextGetTextMatrix(CGContextRef? _Nullable c)獲取當(dāng)前對(duì)文本執(zhí)行變換的變換矩陣
CGContextGetTextPosition(CGContextRef? _Nullable c) 獲取當(dāng)前文本繪制位置
CGContextSetCharacterSpacing(CGContextRef? _Nullable c, CGFloat spacing)設(shè)置繪制文本的字符間距
CGContextSetFont(CGContextRef? _Nullable c, CGFontRef? _Nullable font)設(shè)置繪制文本字體
CGContextSetFontSize(CGContextRef? _Nullable c, CGFloat size)設(shè)置繪制文本字體大小
CGContextSetTextDrawingMode(CGContextRef? _Nullable c, CGTextDrawingMode mode)設(shè)置繪制文本的繪制模式
CGContextSetTextMatrix(CGContextRef? _Nullable c, CGAffineTransform t)設(shè)置對(duì)將要繪制的文本執(zhí)行指定的變換
CGContextSetTextPosition(CGContextRef? _Nullable c, CGFloat x, CGFloat y)設(shè)置繪制文本的繪制位置
5、設(shè)置陰影
CGContextSetShadow(<#CGContextRef? _Nullable c#>, <#CGSize offset#>, <#CGFloat blur#>)//設(shè)置陰影在X、Y軸上的偏移,并設(shè)置陰影模糊程度
CGContextSetShadowWithColor(<#CGContextRef? _Nullable c#>, <#CGSize offset#>, <#CGFloat blur#>, <#CGColorRef? _Nullable color#>)//設(shè)置陰影在X、Y軸上的偏移,并設(shè)置陰影模糊程度,設(shè)置陰影顏色
6、路徑使用
CGContextBeginPath(CGContextRef? _Nullable c)開(kāi)始定義路徑
CGContextClosePath(CGContextRef? _Nullable c)關(guān)閉當(dāng)前定義路徑
CGContextAddArc(CGContextRef? _Nullable c, CGFloat x, CGFloat y, CGFloat radius, CGFloat startAngle, CGFloat endAngle, int clockwise)向當(dāng)前路徑添加一段圓弧
CGContextAddArcToPoint(CGContextRef? _Nullable c, CGFloat x1, CGFloat y1, CGFloat x2, CGFloat y2, CGFloat radius)向當(dāng)前路徑添加一段圓弧
CGContextAddCurveToPoint(CGContextRef? _Nullable c, CGFloat cp1x, CGFloat cp1y, CGFloat cp2x, CGFloat cp2y, CGFloat x, CGFloat y)向當(dāng)前路徑添加一段貝濟(jì)埃曲線(xiàn)
CGContextAddLines(CGContextRef? _Nullable c, const CGPoint * _Nullable points, size_t count)向當(dāng)前路徑添加多條線(xiàn)段
CGContextAddLineToPoint(CGContextRef? _Nullable c, CGFloat x, CGFloat y)向當(dāng)前路徑結(jié)束點(diǎn)連接到(x,y)點(diǎn)
CGContextAddQuadCurveToPoint(CGContextRef? _Nullable c, CGFloat cpx, CGFloat cpy, CGFloat x, CGFloat y)向當(dāng)前路徑添加一條二次曲線(xiàn)
CGContextAddRect(CGContextRef? _Nullable c, CGRect rect)向當(dāng)前路徑添加一個(gè)矩形
CGContextAddRects(CGContextRef? _Nullable c, const CGRect * _Nullable rects, size_t count)向當(dāng)前路徑添加多個(gè)矩形
CGContextMoveToPoint(CGContextRef? _Nullable c, CGFloat x, CGFloat y)向當(dāng)前路徑結(jié)束點(diǎn)移動(dòng)到(x,y)點(diǎn)
CGContextAddEllipseInRect(CGContextRef? _Nullable c, CGRect rect)向當(dāng)前路徑添加一個(gè)橢圓
CGContextCopyPath(CGContextRef? _Nullable c)復(fù)制當(dāng)前路徑
CGContextAddPath(CGContextRef? _Nullable c, CGPathRef? _Nullable path)向當(dāng)前路徑添加已有的路徑
CGContextIsPathEmpty(CGContextRef? _Nullable c)判斷CGContextRef包含的路徑是否為空
CGContextGetPathCurrentPoint(CGContextRef? _Nullable c)返回CGContextRef包涵路徑的當(dāng)前點(diǎn)
CGContextGetPathBoundingBox(CGContextRef? _Nullable c)返回CGContextRef完整包圍的所有路徑的最小矩形
CGContextPathContainsPoint(CGContextRef? _Nullable c, CGPoint point, CGPathDrawingMode mode)CGContextRef包含的路徑按指定繪制模式進(jìn)行繪制時(shí),是否需要繪制point點(diǎn)
7、繪制曲線(xiàn)
CGContextAddCurveToPoint()添加貝濟(jì)埃曲線(xiàn)
CGContextAddQuadCurveToPoint()添加二次曲線(xiàn)
8、內(nèi)存中繪圖
步驟1:調(diào)用UIGraphicsBeginImageContext(CGSize size)準(zhǔn)備繪圖環(huán)境
步驟2:調(diào)用UIGraphicsGetCurrentContext()獲取繪圖CGContextRef
步驟3:使用前面介紹的幾何圖形、路徑等函數(shù)進(jìn)行繪圖
步驟4:調(diào)用UIGraphicsGetImageFromCurrentImageContext()函數(shù)獲取當(dāng)前繪制圖形,返回一個(gè)UIImage對(duì)象
步驟5:調(diào)用UIGraphicsEndImageContext()結(jié)束繪圖,關(guān)閉繪圖環(huán)境
UIRectFill(CGRect rect):向當(dāng)前繪圖環(huán)境所創(chuàng)建的內(nèi)存中的圖片填充一個(gè)矩形
UIRectFillUsingBlendMode(CGRect rect, CGBlendModel blendModel):向當(dāng)前繪圖環(huán)境所創(chuàng)建的內(nèi)存中的圖片填充一個(gè)矩形,繪制時(shí)使用指定的混合模式
UIRectFrame(CGRect rect):向當(dāng)前繪圖環(huán)境所創(chuàng)建的內(nèi)存中的圖片繪制一個(gè)矩形邊框
UIRectFrameUsingBlendMode(CGRect rect, CGBlendModel blendModel):向當(dāng)前繪圖環(huán)境所創(chuàng)建的內(nèi)存中的圖片繪制一個(gè)矩形邊框,繪制時(shí)使用指定的混合模式
9、繪制位圖
為了繪制位圖,UIImage本身提供以下方法:
1、-DrawAtPoint:將該圖片本身繪制到當(dāng)前CGContextRef的指定點(diǎn),該函數(shù)必須傳入一個(gè)指定點(diǎn)
2、-DrawAtPoint:blendMode:alpha:將該圖片本身繪制到當(dāng)前CGContextRef的指定點(diǎn),該函數(shù)必須傳入一個(gè)指定點(diǎn),指定繪制圖片的疊加模式和透明度
3、-DrawInRect:將該圖片本身繪制到當(dāng)前CGContextRef的指定區(qū)域,必須傳入CGRect參數(shù)
4、-DrawInRect:blendMode:alpha:將該圖片本身繪制到當(dāng)前CGContextRef的指定區(qū)域,必須傳入CGRect參數(shù),指定繪制圖片的疊加模式和透明度
如果希望對(duì)圖片執(zhí)行旋轉(zhuǎn)等變換,則可借助一下函數(shù)來(lái)繪制圖片
1、CGContextDrawImage(CGContextRef c, CGRect rect, CGImageRef Image):將Image繪制到指定Rect區(qū)內(nèi)
2、CGContextDrawTiledImage(CGContextRef c, CGRect rect, CGImageRef image):采用‘平鋪’模式將image繪制到指定Rect區(qū)域內(nèi)
如果需要‘挖取’已有圖片部分或全部,則可用以下函數(shù):
1、CGImageCreatCopy(CGImageRef image):創(chuàng)建image圖片副本
2、CGImageCreatWithImageInRect(CGImageRef image, CGRect rect):挖取image中指定區(qū)域rect
三、圖形變換
1、坐標(biāo)變換
CGContextTranslateCTM(CGContextRef? _Nullable c, CGFloat tx, CGFloat ty):平移坐標(biāo)系統(tǒng)
CGContextScaleCTM(CGContextRef? _Nullable c, CGFloat sx, CGFloat sy)縮放坐標(biāo)系統(tǒng)
CGContextRotateCTM(CGContextRef? _Nullable c, CGFloat angle)旋轉(zhuǎn)坐標(biāo)系統(tǒng)
CGContextSaveGState(CGContextRef? _Nullable c)保存當(dāng)前繪圖狀態(tài)
CGContextRestoreGState(CGContextRef? _Nullable c)恢復(fù)之前保存的繪圖狀態(tài)
2、矩陣變換
CGContextConcatCTM(CGContextRef? _Nullable c, CGAffineTransform transform)使用transform變換矩陣對(duì)CGContextRef的坐標(biāo)系執(zhí)行變換,通過(guò)矩陣可以對(duì)坐標(biāo)系統(tǒng)執(zhí)行任意變換。
CGContextGetCTM(CGContextRef? _Nullable c)獲取CGContextRef的變換矩陣
CGAffineTransformMakeTranslation(CGFloat tx, CGFloat ty)創(chuàng)建進(jìn)行位移變換的變換矩陣
CGAffineTransformScale(CGAffineTransform t, CGFloat sx, CGFloat sy)創(chuàng)建進(jìn)行縮放變換的變換矩陣。
CGAffineTransformMakeRotation(CGFloat angle)創(chuàng)建進(jìn)行旋轉(zhuǎn)變換的變換矩陣
CGAffineTransformMake(CGFloat a, CGFloat b, CGFloat c, CGFloat d, CGFloat tx, CGFloat ty)自定義變換矩陣執(zhí)行變換
對(duì)已有矩陣進(jìn)行額外變換
CGAffineTransformTranslate(CGAffineTransform t, CGFloat tx, CGFloat ty)對(duì)已有變換矩陣t額外增加位移變換
CGAffineTransformScale(CGAffineTransform t, CGFloat sx, CGFloat sy)對(duì)已有變換矩陣t額外增加縮放變換
CGAffineTransformRotate(CGAffineTransform t, CGFloat angle)對(duì)已有變換矩陣t額外增加旋轉(zhuǎn)變換
CGAffineTransformInvert(CGAffineTransform t)對(duì)已有變換矩陣t進(jìn)行反轉(zhuǎn)
CGAffineTransformConcat(CGAffineTransform t1, CGAffineTransform t2)將兩個(gè)變換矩陣進(jìn)行疊加
對(duì)點(diǎn)、矩形等幾何形狀進(jìn)行變換
CGPointApplyAffineTransform(CGPoint point, CGAffineTransform t)對(duì)指定CGpoint執(zhí)行變換,函數(shù)返回坐標(biāo)變換后的CGpoint
CGSizeApplyAffineTransform(CGSize size, CGAffineTransform t)對(duì)指定CGSize執(zhí)行變換,函數(shù)返回坐標(biāo)變換后的CGSize
CGRectApplyAffineTransform(CGRect rect, CGAffineTransform t)對(duì)指定CGRect執(zhí)行變換,函數(shù)返回坐標(biāo)變換后的CGRect
3、控制疊加模式
CGContextSetBlendMode(CGContextRef? _Nullable c, CGBlendMode mode)設(shè)置CGContextRef繪圖環(huán)境的疊加模式
UIRectFillUsingBlendMode(CGRect rect, CGBlendMode blendMode)
UIRectFrameUsingBlendMode(CGRect rect, CGBlendMode blendMode)
4、處理填充
CGContextDrawLinearGradient(CGContextRef? _Nullable c, CGGradientRef? _Nullable gradient, CGPoint startPoint, CGPoint endPoint, CGGradientDrawingOptions options)設(shè)置線(xiàn)性漸變填充,gradient代表漸變對(duì)象,startPoint線(xiàn)性漸變起點(diǎn),endPoint線(xiàn)性漸變結(jié)束點(diǎn),options支持kCGGradientDrawsBeforeStartLocation(擴(kuò)展填充起點(diǎn)之前的區(qū)域)或者kCGGradientDrawsAfterEndLocation(擴(kuò)充填充結(jié)束點(diǎn)之后的區(qū)域)
CGContextDrawRadialGradient(CGContextRef? _Nullable c, CGGradientRef? _Nullable gradient, CGPoint startCenter, CGFloat startRadius, CGPoint endCenter, CGFloat endRadius, CGGradientDrawingOptions options)設(shè)置圓形漸變填充,gradient漸變對(duì)象,startCenter參數(shù)設(shè)置起始點(diǎn)的圓心,startRadius參數(shù)設(shè)置起始點(diǎn)半徑,endCenter參數(shù)設(shè)置結(jié)束圓圓心,endRadius參數(shù)設(shè)置結(jié)束圓半徑,options支持kCGGradientDrawsBeforeStartLocation(擴(kuò)展填充起點(diǎn)之前的區(qū)域)或者kCGGradientDrawsAfterEndLocation(擴(kuò)充填充結(jié)束點(diǎn)之后的區(qū)域)
CGGradientCreateWithColorComponents(CGColorSpaceRef? _Nullable space, const CGFloat * _Nullable components, const CGFloat * _Nullable locations, size_t count)space指定顏色變換空間,CGFloat用于根據(jù)不同顏色空間設(shè)置多種顏色,locations指定各顏色點(diǎn)分布位置,count指定該漸變包含多少種顏色
Quartz 2D允許采用兩種方式來(lái)填充:
使用CGColorCreateWithPattern()函數(shù)創(chuàng)建代表填充的CGColorRef,接下來(lái)使用顏色填充指定區(qū)域或繪制邊框
首先創(chuàng)建CGPatternRef:(代表模式填充的引用),接下來(lái)繪制時(shí)調(diào)用CGContextSetFillPattern()或CGContextSetStrokePattern()函數(shù)設(shè)置填充或繪制邊框的模式
1、創(chuàng)建代表模式填充的顏色
》定義繪制填充的回調(diào)函數(shù),該函數(shù)通過(guò)CGContextRef執(zhí)行任意繪制,但必須遵守void函數(shù)名(void *info,CGContextRef context)的簽名
》使用前面創(chuàng)建的函數(shù)創(chuàng)建CGPatternCallbacks結(jié)構(gòu)體數(shù)據(jù)
》以CGPatternCallbacks結(jié)構(gòu)體作為參數(shù),調(diào)用CGPatternCreate()函數(shù)創(chuàng)建CGPatternRef。
》調(diào)用CGColorSpaceCreatePattern()函數(shù)用于模式填充顏色空間。該函數(shù)返回CGColorSpaceRef
》以前面創(chuàng)建的CGPatternRef、CGColorSpaceRef為參數(shù),調(diào)用CGColorCreateWithPattern()函數(shù)創(chuàng)建一個(gè)代表模式填充的顏色:CGColorRef。
2、使用CGPatternRef設(shè)置模式填充
程序無(wú)需創(chuàng)建代表模式填充的UIColorRef,只需創(chuàng)建代表模式填充的CGPatternRef,然后在繪制時(shí)使用CGContextSetFillPattern()或CGContextSetStrokePattern()函數(shù)設(shè)置使用模式進(jìn)行填充或繪制路徑即可
使用CGPatternRef設(shè)置模式填充步驟如下:
》定義繪制填充的回調(diào)函數(shù),該函數(shù)必須遵守void函數(shù)名(void *info,CGContextRef context)的簽名。該函數(shù)可通過(guò)CGContextRef參數(shù)執(zhí)行繪制-但只繪制形狀,無(wú)需指定顏色
》使用前面創(chuàng)建的函數(shù)創(chuàng)建CGPatternCallbacks結(jié)構(gòu)體
》以CGPatternCallbacks結(jié)構(gòu)體作為參數(shù),調(diào)用CGPatternCreate()函數(shù)創(chuàng)建CGPatternRef。
》繪制時(shí)調(diào)用CGContextSetFillColorSpaces()函數(shù)設(shè)置使用模式填充的顏色空間
》繪制時(shí)調(diào)用CGContextSetFillPattern()或CGContextSetStrokePattern()函數(shù)設(shè)置使用指定的模式執(zhí)行填充或繪制
四、濾鏡
Core Image的三個(gè)核心API:
1、CIContext:它是Core Image處理的核心API,所有圖片的處理都是在它的管理下完成的。
2、CIFilter:它代表過(guò)濾器。所有的過(guò)濾器都由該CIFilter代表,在創(chuàng)建CIFilter時(shí)傳入不同參數(shù)即可創(chuàng)建不同類(lèi)型的過(guò)濾器。
3、CIImage:代表Core Image過(guò)濾器處理的圖片,CIFilter過(guò)濾器的輸入圖片、輸出圖片都由該CIImage代表。程序可通過(guò)UIImage、圖片文件或像素?cái)?shù)據(jù)來(lái)創(chuàng)建CIImage。
1、創(chuàng)建CIContext:
》基于CPU的CIContext、基于OpenGL優(yōu)化的CIContext
2、創(chuàng)建CIFilter過(guò)濾器。
3、創(chuàng)建CIImage對(duì)象,該CIImage將要作為過(guò)濾器的源圖片。
4、調(diào)用CIFilter的[filter setValue:beginImage forKey:@“inputImage”]方法為inputImage屬性賦值,該屬性用于指定該過(guò)濾器將要處理的源圖片。
5、根據(jù)需要,為不同的過(guò)濾器設(shè)置不同過(guò)濾參數(shù)。
6、調(diào)用CIFilter的outputImage屬性獲取過(guò)濾器處理后的圖片,返回的時(shí)CIImage對(duì)象。
7、調(diào)用CIContext的不同方法將CIImage轉(zhuǎn)換為CGImageRef,或者將該CIImage繪制到指定區(qū)域
五、動(dòng)畫(huà)
1、Core Animation動(dòng)畫(huà)基礎(chǔ)
Core Animation是一組非常強(qiáng)大的動(dòng)畫(huà)處理API,使用它能做出非常炫麗的動(dòng)畫(huà)效果,而且往往是事半功倍。Core Animation動(dòng)畫(huà)使用CALayer來(lái)創(chuàng)建用戶(hù)界面。
Core Animation動(dòng)畫(huà)在單獨(dú)的線(xiàn)程中完成,不會(huì)阻塞主線(xiàn)程。
Core Animation動(dòng)畫(huà)只會(huì)重繪界面上變化部分(局部刷新)
Core Animation動(dòng)畫(huà)的核心是CALayer。
CAAnimation是所有動(dòng)畫(huà)類(lèi)的基類(lèi),它實(shí)現(xiàn)了CAMedia Timing協(xié)議,提供了動(dòng)畫(huà)的持續(xù)時(shí)間、速度和重復(fù)計(jì)數(shù)等。
CATransition:CAAnimation的子類(lèi),CATransition可通過(guò)預(yù)置的過(guò)度效果來(lái)控制CALayer層的過(guò)度動(dòng)畫(huà)。
CAPropertyAnimation:CAAnimation的子類(lèi),代表屬性動(dòng)畫(huà),可通過(guò)+animationWithKeyPath:類(lèi)方法來(lái)創(chuàng)建屬性動(dòng)畫(huà)實(shí)例,該方法需要制定一個(gè)CALayer支持動(dòng)畫(huà)的屬性,然后通過(guò)它的子類(lèi)(CABasicAnimation、CAKeyframeAnimation)控制CALayer的動(dòng)畫(huà)屬性慢慢改變,即可實(shí)現(xiàn)CALayer動(dòng)畫(huà)。
CABasicAnimation:CAPropertyAnimation的子類(lèi),簡(jiǎn)單控制CALayer層屬性慢慢改變,從而實(shí)現(xiàn)動(dòng)畫(huà)效果。
CAKeyframeAnimation:CAPropertyAnimation的子類(lèi),支持關(guān)鍵幀的屬性動(dòng)畫(huà),該動(dòng)畫(huà)最大的特點(diǎn)在于可通過(guò)values屬性指定多個(gè)關(guān)鍵幀,通過(guò)關(guān)鍵幀可以指定動(dòng)畫(huà)各個(gè)階段的關(guān)鍵值。
CAAnimtaionGroup:CAAnimation的子類(lèi),用于多個(gè)動(dòng)畫(huà)組合在一起執(zhí)行。
CAAnimation提供了以下方法和屬性:
1、BOOL removedOnCompletion;該屬性指定該動(dòng)畫(huà)完成時(shí)是否從目標(biāo)CALayer上刪除該動(dòng)畫(huà)。
?2、timingFunction;該屬性指定一個(gè)CAMedia TimingFunction對(duì)象,該對(duì)象負(fù)責(zé)控制動(dòng)畫(huà)變化步長(zhǎng)。
3、- (void)animationDidStart:(CAAnimation *)aim:動(dòng)畫(huà)開(kāi)始時(shí)將會(huì)調(diào)用該方法
4、- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag:動(dòng)畫(huà)結(jié)束時(shí)調(diào)用該方法。