iOS高级-QuartzCore框架-背景平铺

一、将图片平铺填充整个View

UIImage *oldImage = [UIImage imageNamed:@"me"];
UIGraphicsBeginImageContextWithOptions(self.view.frame.size,NO,0.0);
[oldImage drawInRect:self.view.bounds];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

self.view.backgroundColor = [UIColor colorWithPatternImage:newImage];

二、TableView的条纹背景

//1.创建一行背景图片
CGFloat rowW = self.view.frame.size.width;
CGFloat rowH = 40;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(rowW,rowH), 
NO,0.0);

CGContextRef ctx = UIGraphicsGetCurrentContext();
//画矩形框
[[UIColor redColor] set];
CGContextAddRect(ctx,CGRectMake(0,0,rowW,rowH));
CGContextFillPath;
//2.画线
[[UIColor blackColor] set];
CGFloat lineWidth =2;
CGFloat dividerX = 10;
CGFloat dividerY = rowH - lineWidth;
CGContextMoveToPoint(ctx,dividerX,dividerY);
CGContextAddLineToPoint(ctx,rowW - dividerX,dividerY);
CGContextStrokePath(ctx);

//3.取图
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

//4.结束上下文
UIGraphicsEndImageContext();

//5.设置为背景色
self.view.backgroundColor = [UIColor colorWithPatternImage:newImage];

 

posted on 2015-08-23 13:09  Marshall_Yin  阅读(206)  评论(0编辑  收藏  举报