ios-day15-05(Quartz 2D之一張圖片以平鋪或者填充的方式設定為UIView的背景、以填充的方式為UIView設定條紋背景)
阿新 • • 發佈:2019-02-08
// // JLViewController.m // 05-條紋背景 // // Created by Mac on 15-3-22. // Copyright (c) 2015年 vxinyou. All rights reserved. // #import "JLViewController.h" @interface JLViewController () @end @implementation JLViewController - (void)viewDidLoad { [super viewDidLoad]; // [self tile]; // [self fill]; [self listBackground]; } /** * 模仿listView的分割線,給view設定條紋背景,效果圖如圖3所示。 */ - (void)listBackground{ // 1.建立一行背景圖片(會以這張圖片為單位,平鋪整個view) CGFloat rowW = self.view.frame.size.width; // CGFloat rowH = 40; CGFloat rowH = 30; UIGraphicsBeginImageContextWithOptions(CGSizeMake(rowW, rowH), NO, 0.0); CGContextRef ctx = UIGraphicsGetCurrentContext(); // 畫矩形框 [[UIColor colorWithRed:142/255.0 green:255/255.0 blue:180/255.0 alpha:255/255.0] set]; CGContextAddRect(ctx, CGRectMake(0, 0, rowW, rowH)); CGContextFillPath(ctx); // 2.畫線 [[UIColor grayColor] set]; CGFloat lineWidth = 2; CGContextSetLineWidth(ctx, lineWidth); CGFloat dividerX = 5; 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]; } /** * 將一張圖片填充整個view,效果圖如圖2所示 */ - (void)fill{ UIImage *image = [UIImage imageNamed:@"me"]; UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0.0); [image drawInRect:self.view.bounds]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); // UIView不像UIImageView有個image屬性。 self.view.backgroundColor = [UIColor colorWithPatternImage:newImage]; } /** * 將一張圖片以平鋪的方式鋪滿整個view,效果圖如圖1所示 */ - (void)tile{ UIImage *image = [UIImage imageNamed:@"me"]; self.view.backgroundColor = [UIColor colorWithPatternImage:image]; } @end