Quartz 2D的內(nèi)存管理
l1>使用含有“Create”或“Copy”的函數(shù)創(chuàng)建的對(duì)象,使用完后必須釋放,否則將導(dǎo)致內(nèi)存泄露.總結(jié):凡是遇到retain, copy, create出的對(duì)象,都是需要進(jìn)行release的.
l2>使用不含有“Create”或“Copy”的函數(shù)獲取的對(duì)象,則不需要釋放.
l3>如有retain了一個(gè)對(duì)象,不再使用時(shí),需要將其release掉.
l4>可以使用Quartz 2D的函數(shù)來指定retain和release一個(gè)對(duì)象.也可以使用coreFoundation的CFRetain和CFRelease.注意不能傳遞NULL值給這些函數(shù).
l5>演示通過CGMutablePathRef實(shí)現(xiàn)繪圖.通過Product -> Analyze來進(jìn)行靜態(tài)分析.注意: CGPathCreatMutable()不是OC方法,所以不是調(diào)用某個(gè)對(duì)象的release方法.
//代碼實(shí)現(xiàn)
------------------------------ HMRootView.m------------------------------
- (void)drawRect:(CGRect)rect
{
// 1.獲取當(dāng)前繪圖上下文
CGContextRefctx =UIGraphicsGetCurrentContext();
// 2.創(chuàng)建畫線的path對(duì)象
CGMutablePathReflinePath =CGPathCreateMutable();
// 2.1拼接路徑
CGPathMoveToPoint(linePath,NULL,50,50);
CGPathAddLineToPoint(linePath,NULL,150,150);
CGPathAddLineToPoint(linePath,NULL,100,50);
// 2.2把路徑添加到上下文對(duì)象中
CGContextAddPath(ctx, linePath);
// 3.創(chuàng)建一個(gè)畫圓的路徑
CGMutablePathRefcirclePath =CGPathCreateMutable();
// 3.1添加路徑
CGPathAddArc(circlePath,NULL,150,150,50,0,M_PI*2,0);
// 3.2把路徑添加到上下文中
CGContextAddPath(ctx, circlePath);
// 4.渲染上下文
CGContextStrokePath(ctx);
//CGPathRelease(circlePath);
//CGPathRelease(linePath);
//只要是Core Foundation中的對(duì)象,都可以直接通過下面的代碼來釋放尊勿。
CFRelease(linePath);
CFRelease(circlePath);
}