IOS 中CALayer繪制圖片的實(shí)例詳解
IOS 中CALayer繪制圖片的實(shí)例詳解
CALayer渲染內(nèi)容圖層。與UIImageView相比,不具有事件響應(yīng)功能,且UIImageView是管理內(nèi)容。
注意事項(xiàng):如何使用delegate對(duì)象執(zhí)行代理方法進(jìn)行繪制,切記需要將delegate設(shè)置為nil,否則會(huì)導(dǎo)致異常crash。
CALayer繪制圖片與線條效果圖:

代碼示例:
CGPoint position = CGPointMake(160.0, 200.0); CGRect bounds = CGRectMake(0.0, 0.0, 150.0, 150.0); CGFloat cornerRadius = 150.0 / 2; CGFloat borderWidth = 2.0;
// 陰影層 CALayer *layerShadow = [[CALayer alloc] init]; layerShadow.position = position; layerShadow.bounds = bounds; layerShadow.cornerRadius = cornerRadius; layerShadow.borderWidth = borderWidth; layerShadow.borderColor = [UIColor whiteColor].CGColor; layerShadow.shadowColor = [UIColor grayColor].CGColor; layerShadow.shadowOffset = CGSizeMake(2.0, 1.0); layerShadow.shadowOpacity = 1.0; layerShadow.shadowRadius = 3.0; [self.view.layer addSublayer:layerShadow];
// 容器層 CALayer *layerContant = [[CALayer alloc] init]; // 添加到父圖層 [self.view.layer addSublayer:layerContant]; // 圖層中心點(diǎn)、大?。ㄖ行狞c(diǎn)和大小構(gòu)成frame) layerContant.position = position; layerContant.bounds = bounds; // 圖層背景顏色 layerContant.backgroundColor = [UIColor redColor].CGColor; // 圖層圓角半徑 layerContant.cornerRadius = cornerRadius; // 圖層蒙版、子圖層是否剪切圖層邊界 // layerContant.mask = nil; layerContant.masksToBounds = YES; // 邊框?qū)挾?、顏? layerContant.borderWidth = borderWidth; layerContant.borderColor = [UIColor whiteColor].CGColor; // 陰影顏色、偏移量、透明度、形狀、模糊半徑 // layerContant.shadowColor = [UIColor grayColor].CGColor; // layerContant.shadowOffset = CGSizeMake(2.0, 1.0); // layerContant.shadowOpacity = 1.0; // CGMutablePathRef path = CGPathCreateMutable(); // layerContant.shadowPath = path; // layerContant.shadowRadius = 3.0; // 圖層透明度 layerContant.opacity = 1.0;
// 繪制圖片顯示方法1 // 圖層形變 // 旋轉(zhuǎn)(angle轉(zhuǎn)換弧度:弧度=角度*M_PI/180;x上下對(duì)換、y左右對(duì)換、z先上下對(duì)換再左右對(duì)換;-1.0~1.0) // layerContant.transform = CATransform3DMakeRotation(M_PI, 0.0, 0.0, 0.0); // 縮放(0.0~1.0) // layerContant.transform = CATransform3DMakeScale(0.8, 0.8, 0.8); // 移動(dòng) // layerContant.transform = CATransform3DMakeTranslation(10.0, 1.0, 1.0); // 顯示內(nèi)容 [layerContant setContents:[UIImage imageNamed:@"header"].CGImage];
繪制圖片顯示方法2
layerContant.delegate = self;
[layerContant setNeedsDisplay];
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
// 繪圖
CGContextSaveGState(ctx);
// 圖形上下文形變,避免圖片倒立顯示
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextTranslateCTM(ctx, 0.0, -150.0);
// 圖片
UIImage *image = [UIImage imageNamed:@"header"];
CGContextDrawImage(ctx, CGRectMake(0.0, 0.0, 150.0, 150.0), image.CGImage);
CGContextRestoreGState(cox);
}
// 繪制實(shí)線、虛線
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
// 繪實(shí)線
// 線條寬
CGContextSetLineWidth(ctx, 1.0);
// 線條顏色
// CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
CGContextSetStrokeColorWithColor(ctx, [UIColor greenColor].CGColor);
// 方法1
// 坐標(biāo)點(diǎn)數(shù)組
CGPoint aPoints[2];
aPoints[0] = CGPointMake(10.0, 50.0);
aPoints[1] = CGPointMake(140.0, 50.0);
// 添加線 points[]坐標(biāo)數(shù)組,和count大小
CGContextAddLines(ctx, aPoints, 2);
// 根據(jù)坐標(biāo)繪制路徑
CGContextDrawPath(ctx, kCGPathStroke);
// 方法2
CGContextSetLineWidth(ctx, 5.0);
CGContextSetStrokeColorWithColor(ctx, [UIColor purpleColor].CGColor);
CGContextMoveToPoint(ctx, 10.0, 60.0); // 起點(diǎn)坐標(biāo)
CGContextAddLineToPoint(ctx, 140.0, 60.0); // 終點(diǎn)坐標(biāo)
CGContextStrokePath(ctx); // 繪制路徑
// 繪虛線
// 線條寬
CGContextSetLineWidth(ctx, 2.0);
// 線條顏色
CGContextSetStrokeColorWithColor(ctx, [UIColor blueColor].CGColor);
// 虛線
CGFloat dashArray[] = {1, 1, 1, 1};
CGContextSetLineDash(ctx, 1, dashArray, 1);
// 起點(diǎn)
CGContextMoveToPoint(ctx, 10.0, 100.0);
// 終點(diǎn)
CGContextAddLineToPoint(ctx, 140.0, 100.0);
// 繪制路徑
CGContextStrokePath(ctx);
}
// 內(nèi)存管理,避免異常crash
- (void)dealloc
{
for (CALayer *layer in self.view.layer.sublayers)
{
if ([layer.delegate isEqual:self])
{
layer.delegate = nil;
}
}
NSLog(@"%@ 被釋放了~", self);
}
如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Android 在頁(yè)面中顯示打包日期的實(shí)現(xiàn)方法
下面小編就為大家分享一篇Android 在頁(yè)面中顯示打包日期的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01
iOS應(yīng)用開發(fā)中使用Auto Layout來適配不同屏幕尺寸
這篇文章主要介紹了iOS應(yīng)用開發(fā)中使用Auto Layout來適配不同屏幕尺寸的方法,根據(jù)Xcode IDE下的實(shí)際調(diào)試步驟講解其用法,需要的朋友可以參考下2016-03-03
iOS 指壓即達(dá)集成iOS9里的3D Touch的方法
這篇文章主要介紹了iOS 指壓即達(dá)集成iOS9里的3D Touch的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
詳解Obejective-C中將JSON數(shù)據(jù)轉(zhuǎn)為模型的方法
這篇文章主要介紹了Obejective-C中JSON數(shù)據(jù)轉(zhuǎn)為模型的方法,同時(shí)介紹了使用jastor庫(kù)的方法,需要的朋友可以參考下2016-03-03
iOS實(shí)現(xiàn)帶遮罩的彈出選項(xiàng)卡
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)彈出選項(xiàng)卡,并附帶遮罩,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02
iOS利用UIBezierPath + CAAnimation實(shí)現(xiàn)路徑動(dòng)畫效果
在iOS開發(fā)中,制作動(dòng)畫效果是最讓開發(fā)者享受的環(huán)節(jié)之一,這篇文章主要給大家介紹了關(guān)于iOS利用UIBezierPath + CAAnimation實(shí)現(xiàn)路徑動(dòng)畫效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
iOS中關(guān)于UIWindow和statusbar的設(shè)置問題
最近在做開發(fā)時(shí)要做一個(gè)類似于UIAlertView的控件,做法是創(chuàng)建一個(gè)基于UIView的類,在里面進(jìn)行自定義控件的設(shè)置,為了盡量模仿UIAlertView,在這個(gè)類里面創(chuàng)建了一個(gè)新的UIWindow并將self顯示到這個(gè)window上2017-03-03
深入分析iOS應(yīng)用中對(duì)于圖片緩存的管理和使用
這篇文章主要介紹了iOS應(yīng)用中對(duì)于圖片緩存的管理和使用,實(shí)例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-04-04

