IOS_Quartz2D_水印缩略图_PDF_setNeedsDisplay方法内部自动调用drawRect

H:/0804/01_Quartz2D_线(2种路径)+矩形+圆+弧+渐变+文本+图像+上下文(平移.旋转.缩放)_MyDrawingView.h
//
//  MyDrawingView.h
//  Quartz2D01-基本使用
//
//  Created by apple on 13-8-4.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MyDrawingView : UIView

@end

H:/0804/01_Quartz2D_线(2种路径)+矩形+圆+弧+渐变+文本+图像+上下文(平移.旋转.缩放)_MyDrawingView.m
//  MyDrawingView.m
//  Quartz2D01-基本使用
//  Created by apple on 13-8-4.
//  Copyright (c) 2013年 itcast. All rights reserved.
#import "MyDrawingView.h"
@implementation MyDrawingView
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    }
    return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    [self drawAll];
}
#pragma mark - 综合绘制
- (void)drawAll
{
    // 取出上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 备份上下文
    CGContextSaveGState(context);
    // 对上下文做一个平移操作,相当于将坐标系右移下移50
    CGContextTranslateCTM(context, 50, 50);
    [self drawShapeCircle];
    // 恢复上下文
    CGContextRestoreGState(context);
    // 备份上下文
    CGContextSaveGState(context);
    // 对上下文做一个平移操作
    CGContextTranslateCTM(context, 100, 100);
    // 对坐标系进行旋转
    CGContextRotateCTM(context, M_PI_4);
    // 对坐标系进行缩放
    CGContextScaleCTM(context, 0.5, 0.5);
    UIImage *image = [UIImage imageNamed:@"头像1.png"];
    // 在指定点绘制
    [image drawAtPoint:CGPointMake(50, 50)];
    // 恢复上下文
    CGContextRestoreGState(context);
}
#pragma mark - 绘制图像
- (void)drawImage
{
    UIImage *image = [UIImage imageNamed:@"头像1.png"];
    // 在指定点绘制
    [image drawAtPoint:CGPointMake(50, 50)];
    // 会拉伸
    [image drawInRect:CGRectMake(0, 0, 320, 460)];
    // 平铺,像贴瓷砖Tile
    [image drawAsPatternInRect:CGRectMake(0, 0, 320, 460)];
}
#pragma mark - 绘制文本
- (void)drawText
{
    NSString *text = @"I Love you!I Love you!I Love you!I Love you!I Love you!I Love you!I Love you!I Love you!I Love you!";
    // NSLog(@"%@",[UIFont familyNames]);
    // 使用set方法既指定边框颜色,又指定填充颜色
    [[UIColor redColor]set];
    UIFont *font = [UIFont fontWithName:@"Marker Felt" size:30];
    [text drawAtPoint:CGPointMake(50, 50) withFont:font];
    CGRect rect = CGRectMake(50, 200, 200, 200);
    [[UIColor blueColor]set];
	//用蓝色填充矩形,画个矩形
    UIRectFill(rect);
    [[UIColor redColor]set];
    // 只能用居中、左、右对齐,lineBreakMode最好指定单词完整模式
    [text drawInRect:rect withFont:font lineBreakMode:NSLineBreakByWordWrapping
					alignment:NSTextAlignmentCenter];
}
#pragma mark - 绘制渐变
- (void)drawGradient
{
    //    1. 定义渐变类型:CGGradientRef
    CGGradientRef gradient;
    //    2. 定义色彩空间引用
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    //    3. 定义渐变颜色组件
    //      每四个数一组,分别对应r,g,b,透明度
    CGFloat components[8] = {1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0};
    //    4. 定义颜色渐变位置
    // 第一个颜色开始渐变的位置,0.0代表第1种颜色,从起始端头就开始渐变
    // 第二个颜色结束渐变的位置,1.0代表第2种颜色,一直渐变到末端
    CGFloat locations[2] = {0.0, 1.0};
    //    5. 创建颜色渐进
    gradient = CGGradientCreateWithColorComponents(colorSpace,
								components, locations, 2);
    //    6. 创建贝塞尔路径,是OC的,如果只是制定了渐变,
			//没有指定剪切路径,就是整个视图的渐变(全屏幕都渐变)
    UIBezierPath *path = [UIBezierPath 
					bezierPathWithRect:CGRectMake(50, 50, 200, 200)];
    //    7. 添加剪切路径
    [path addClip];
    //    8. 绘制线性渐进
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawLinearGradient(context, gradient, CGPointMake(50, 50), 
					 CGPointMake(200, 50), kCGGradientDrawsAfterEndLocation);
    //    9. 释放颜色空间
    CGColorSpaceRelease(colorSpace);
    //    10. 释放渐变引用
    CGGradientRelease(gradient);
}
#pragma mark - 绘制圆弧
- (void)drawShapeArc
{
    //    1. 获取一个与视图相关联的上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    [[UIColor redColor]set];
    // 2. 添加弧线
    // 参数1 上下文
    // 参数2 中心点坐标
    // 参数3 半径
    // 参数4 开始角度,结束角度,角度的单位是“弧度”
	// 参数5 clockwise--1--顺时针,还是--0--逆时针
    CGContextAddArc(context, 160, 230, 100, M_PI, -M_PI_2, 0);
    // 绘制路径
    CGContextStrokePath(context);
}
#pragma mark - 绘制圆型
- (void)drawShapeCircle
{
    //    1. 获取一个与视图相关联的上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 设置颜色
    [[UIColor greenColor]setFill];
    // 2. 增加圆形的路径,矩形内切圆
    CGContextAddEllipseInRect(context, CGRectMake(50, 50, 200, 200));
    // 3. 画圆
    CGContextFillPath(context);
}
#pragma mark - 绘制矩形
- (void)drawShapeRect
{
    [[UIColor redColor]setFill];
    CGRect rect = CGRectMake(50, 50, 200, 200);
    UIRectFill(rect);
	//压缩矩形高度,就变成了一条线啦~
    [[UIColor blueColor]setFill];
    CGRect rect2 = CGRectMake(50, 300, 200, 2);
    UIRectFill(rect2);
}
#pragma mark - 画线代码
// 第二种画线方式,使用上下文的默认路径实现
- (void)drawLine2
{
    //    1. 获取一个与视图相关联的上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    //    2. 构建路径
    //    2.1 设置上下文路径起点
    CGContextMoveToPoint(context, 85, 85);
    //    2.2 增加路径内容……
    CGContextAddLineToPoint(context, 150, 150);
    CGContextAddLineToPoint(context, 250, 50);
    //    3. 保存上下文状态
    //    4. 设置上下文状态
    //    4.1 设置边线颜色
    CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);
    //    4.2 设置线宽
    CGContextSetLineWidth(context, 10);
    //    4.3 设置线段连接样式
    CGContextSetLineJoin(context, kCGLineJoinRound);
    //    4.4 设置线段收尾样式
    CGContextSetLineCap(context, kCGLineCapRound);
    //    4.5 设置虚线样式
    //    4. 绘制路径
    CGContextDrawPath(context, kCGPathStroke);
    //    5. 恢复上下文
    //    6. 在蓝线上面,再次绘制红色线条
//    CGContextMoveToPoint(context, 50, 50);
//    CGContextAddLineToPoint(context, 150, 150);
//    CGContextAddLineToPoint(context, 250, 50);
//    CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);
//    CGContextSetLineWidth(context, 2);
//    CGContextDrawPath(context, kCGPathStroke);
}
- (void)drawLine1
{
    //    1. 获取一个与视图相关联的上下文,UI开头原点同UIKit,颜色默认黑色
    CGContextRef context = UIGraphicsGetCurrentContext();
    //    2. 创建一个可变路径
    //    2.1 创建路径
    CGMutablePathRef path = CGPathCreateMutable();
    //    2.2 设置路径起点
    CGPathMoveToPoint(path, NULL, 50, 50);
    //    2.3 增加路径内容……
    CGPathAddLineToPoint(path, NULL, 150, 150);
    CGPathAddLineToPoint(path, NULL, 50, 150);
    //    CGPathAddLineToPoint(path, NULL, 50, 50);
    // 闭合路径,关闭路径,闭合路径是收尾相连的,圆滑好看,不突兀
    CGPathCloseSubpath(path);
    //    3. 将路径添加到上下文;
    CGContextAddPath(context, path);
    //    4. 设置上下文状态
    //    4.1 设置边线颜色
    // 颜色数值 = RGB数值 / 255
    CGContextSetRGBStrokeColor(context, 255.0 / 255.0, 0.0, 0.0 , 1.0);
    //    4.2 设置填充颜色
    CGContextSetRGBFillColor(context, 0.0, 0.0, 128.0 / 255.0, 1.0);
    //    4.3 设置线宽
    CGContextSetLineWidth(context, 5);
    //    4.4 设置线段连接样式,连接处是平角还是圆角
    CGContextSetLineJoin(context, kCGLineJoinBevel);
    //    4.5 设置线段首尾样式
    CGContextSetLineCap(context, kCGLineCapRound);
    //    4.6 设置虚线样式
    // lengths 设置公有几条虚线,每条虚线的长度
    // count 指的是lengths数组的长度
    CGFloat lengthes[2] = {10.0, 10.0};
    CGContextSetLineDash(context, 0, lengthes, 2);
    //    5. 绘制路径
    /**
     kCGPathStroke  绘制边线
     kCGPathFill    填充
	 kCGPathFillStroke 即填充又画边线
     */
    CGContextDrawPath(context, kCGPathFillStroke);
    //    6. 释放路径,不同对象对应着不同的release方法
    CGPathRelease(path);
}
@end

H:/0804/02.Quartz2D_水印+归档生成缩略图_ViewController.h
//
//  ViewController.h
//  Quartz2D02.给图像添加水印
//
//  Created by apple on 13-8-4.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

H:/0804/02.Quartz2D_水印+归档生成缩略图_ViewController.m
//  ViewController.m
//  Quartz2D02.给图像添加水印
//  Created by apple on 13-8-4.
//  Copyright (c) 2013年 itcast. All rights reserved.
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
#pragma mark 给图像添加水印
- (UIImage *)createImage
{
    // 1. 获得图像相关的上下文
    // 获得图像上下文的时候,需要指定上下文大小
    UIGraphicsBeginImageContext(CGSizeMake(320, 200));
    // 2. 绘制图像
    UIImage *image = [UIImage imageNamed:@"NatGeo02.png"];
    [image drawInRect:CGRectMake(0, 0, 320, 200)];
    // 3. 写水印文字
    NSString *text = @"水印文字";
//    [[UIColor whiteColor]set];
    // 新建一个UIColor
    UIColor *color = [UIColor colorWithRed:1 green:1 blue:0 alpha:0.5];
    [color set];
    [text drawInRect:CGRectMake(0, 170, 300, 20) withFont:[UIFont systemFontOfSize:12] 
				lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentRight];
    // 从图像上下文中获得当前绘制的结果,并生成图像
    UIImage *result =  UIGraphicsGetImageFromCurrentImageContext();
    // 4. 千万记得要关闭上下文~~~~~~~
    UIGraphicsEndImageContext();
    // 5. 把图像归档,可以用这个方法来做缩略图!!!!!!!!!!节约开销
//    NSArray *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//    NSString *path = [documents[0]stringByAppendingPathComponent:@"image.png"];
    NSString *path = @"/Users/apple/Desktop/image.png";
	// UIImagePNGRepresentation 根据图像获得data数据库
    NSData *imageData = UIImagePNGRepresentation(result);
    [imageData writeToFile:path atomically:YES];
    return result;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [_imageView setImage:[self createImage]];
}
@end

H:/0804/03_Quartz2D_仿便签背景线条_ViewController.h
//
//  ViewController.h
//  Quartz2D03模拟备忘录
//
//  Created by apple on 13-8-4.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITextView *textView;

@end

H:/0804/03_Quartz2D_仿便签背景线条_ViewController.m
//  ViewController.m
//  Quartz2D03模拟备忘录
//  Created by apple on 13-8-4.
//  Copyright (c) 2013年 itcast. All rights reserved.
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
#pragma mark - 创建背景图像
- (UIImage *)createBGImage
{
    // 1. 用矩形尺寸,开始创建图像上下文
    UIGraphicsBeginImageContext(CGSizeMake(320, 30));
    // 2. 绘制矩形
    CGRect rect = CGRectMake(0, 0, 320, 30);
    [[UIColor yellowColor]set];
    UIRectFill(rect);
    // 3. 绘制扁宽矩形作为下面的线条,2个点高的矩形
    CGRect rect2 = CGRectMake(0, 29, 320, 1);
    [[UIColor brownColor]set];
    UIRectFill(rect2);
    // 4. 从当前图像上下文context中获取绘制的图像
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    // 5. 千万记得要关闭上下文~~~~~~~
    UIGraphicsEndImageContext();
    // 6. 返回获取的手绘的图像(作平铺背景用)
    return image;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
	//设置背景颜色....用图像Pattern平铺方式生成color
    [self.textView setBackgroundColor:
			[UIColor colorWithPatternImage:
				[self createBGImage]]];
}

@end

H:/0804/04_Quartz2D_用指定图片生成PDF_ViewController.h
//
//  ViewController.h
//  Quartz2D04生成PDF文件
//
//  Created by apple on 13-8-4.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

H:/0804/04_Quartz2D_用指定图片生成PDF_ViewController.m
//  ViewController.m
//  Quartz2D04生成PDF文件
//  Created by apple on 13-8-4.
//  Copyright (c) 2013年 itcast. All rights reserved.
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
#pragma mark - 生成PDF文件
- (void)createPDFFile
{
    // 1. 开启PDF上下文到文件
    // 1) 参数2,路径
    // 2) 参数3,大小,指定为空CGRectZero,那么使用612 * 792大小作为pdf文件的页面大小
    // 3) 参数4,dict, PDF附加信息
    UIGraphicsBeginPDFContextToFile(@"/Users/apple/Desktop/demo.pdf",
							CGRectZero, nil);
    // 2. 写入内容
    // 在pdf里面是有页面的,一个页面一个页面的写入的
    // 建立PDF页面BeginPDFPageWithInfo
    // 一个页面最多能够写入两张图片,因此写入6张图片需要三个页面
    for (NSInteger i = 0; i < 6; i++) {
        if (i % 2 == 0) {
			//BeginPDFPageWithInfo指定尺寸,开启新的一页
            UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);
        }
        
        // 生成UIImage
        UIImage *image = [UIImage imageNamed:
				[NSString stringWithFormat:@"NatGeo%02d.png", i + 1]];
        // 指定位置,写入图片到PDF页面,每页写两张图,上面一张,下面一张,x始终为0
        [image drawAtPoint:CGPointMake(0, 400 * (i % 2))];
    }
    // 3. 千万记得关闭即结束PDF上下文,自动输出到指定路径
    UIGraphicsEndPDFContext();
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self  ];
}
@end

H:/0804/05_Quartz2D_setNeedsDisplay方法内部调用drawRect方法刷新视图_DemoView.h
//
//  DemoView.h
//  Quartz2D05.视图刷新
//
//  Created by apple on 13-8-4.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface DemoView : UIView

// 字体大小
@property (assign, nonatomic) CGFloat fontSize;

@end

H:/0804/05_Quartz2D_setNeedsDisplay方法内部调用drawRect方法刷新视图_DemoView.m
//  DemoView.m
//  Quartz2D05.视图刷新
//  Created by apple on 13-8-4.
//  Copyright (c) 2013年 itcast. All rights reserved.
#import "DemoView.h"
@implementation DemoView
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
// 重写UIView的drawRect:方法,该方法不允许用户手动调用,
//而是间接通过setNeedsDisplay内部系统调用drawRect方法,
//并且,iOS会自动准备好一个图形上下文,
//可以通过调用UIGraphicsGetCurrentContext()来获取
// 只要一个UIView需要被刷新或者重绘,drawRect:方法就会被系统自动调用,
//所以drawRect:被系统自动调用频率很高
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    // 绘制一段文字
    [[UIColor whiteColor]set];
    NSString *text = @"I Love you!";
	//折行方式:NSLineBreakByWordWrapping,对齐方式:居中
    [text drawInRect:CGRectMake(0, 0, 320, 200) withFont:[UIFont systemFontOfSize:_fontSize]
			lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentCenter];
}
#pragma mark - fontSize Setter方法
- (void)setFontSize:(CGFloat)fontSize
{
	//1,首先,设置好字体大小
    _fontSize = fontSize;
	//2,再调用setNeedsDisplay(内部会自动调用drawRect)从而,修改视图上面的文字
	[self setNeedsDisplay];
// 需要注意的是:
//重绘时应该调用setNeedsDisplay,setNeedsDisplay会自动调用drawRect:
//而不允许直接调用drawRect:,    
}
@end

H:/0804/05_Quartz2D_setNeedsDisplay方法内部调用drawRect方法刷新视图_ViewController.h
//
//  ViewController.h
//  Quartz2D05.视图刷新
//
//  Created by apple on 13-8-4.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "DemoView.h"

@interface ViewController : UIViewController

// 演练视图
@property (weak, nonatomic) IBOutlet DemoView *demoView;

// 响应滑块滑动,改变数值
- (IBAction)sliderChange:(id)sender;

@end

H:/0804/05_Quartz2D_setNeedsDisplay方法内部调用drawRect方法刷新视图_ViewController.m
//  ViewController.m
//  Quartz2D05.视图刷新
//  Created by apple on 13-8-4.
//  Copyright (c) 2013年 itcast. All rights reserved.
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    [_demoView setFontSize:20.0];
}
- (IBAction)sliderChange:(id)sender
{
    UISlider *slider = (UISlider *)sender;
    NSLog(@"%f", slider.value);
	//设置字体方法,手动调用了setNeedsDisplay,
	//而setNeedsDisplay内部又会自动调用drawRect方法,从而刷新视图上的文字尺寸
    [_demoView setFontSize:slider.value];
}
@end

H:/0804/06_随机加载视图_NewsView.h
//
//  NewsView.h
//  多视图选择演示
//
//  Created by apple on 13-8-4.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface NewsView : UIView
// 新闻标题
@property (weak, nonatomic) IBOutlet UILabel *newsLabel;
// 新闻图片1
@property (weak, nonatomic) IBOutlet UIImageView *newsImage1;
// 新闻图片2
@property (weak, nonatomic) IBOutlet UIImageView *newsImage2;
// 新闻图片3
@property (weak, nonatomic) IBOutlet UIImageView *newsImage3;
// 新闻图片4
@property (weak, nonatomic) IBOutlet UIImageView *newsImage4;

@end

H:/0804/06_随机加载视图_NewsView.m
//  NewsView.m
//  多视图选择演示
//  Created by apple on 13-8-4.
//  Copyright (c) 2013年 itcast. All rights reserved.
#import "NewsView.h"
@implementation NewsView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
@end

H:/0804/06_随机加载视图_ViewController.h
//
//  ViewController.h
//  多视图选择演示
//
//  Created by apple on 13-8-4.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

// 新闻界面占位视图
@property (weak, nonatomic) IBOutlet UIView *placeHolderView;

@end

H:/0804/06_随机加载视图_ViewController.m
//  ViewController.m
//  多视图选择演示
//  Created by apple on 13-8-4.
//  Copyright (c) 2013年 itcast. All rights reserved.
#import "ViewController.h"
#import "NewsView.h"
@interface ViewController ()
{
}
@end
@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSInteger type = arc4random() % 2;
    NSString *xibName;
    if (type == 1) {
        xibName = @"NewsView1";
    } else {
        xibName = @"NewsView2";
    }
	//loadNibNamed
    NewsView *view = [[[NSBundle mainBundle]loadNibNamed:xibName owner:self options:nil]lastObject];
    [_placeHolderView addSubview:view];
}
@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值