一、将图片平铺填充整个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];