//字体
// MyView.matom
// Core Graphics次日spa
//3d
// Created by dc008 on 15/12/8.orm
// Copyright © 2015年 CXY. All rights reserved.图片
//ip
#import "MyView.h"it
@implementation MyViewio
- (void)drawRect:(CGRect)rect {table
CGContextRef context = UIGraphicsGetCurrentContext();
// [self drawText:context];
// [self drawImage:context];
[self drawImageAtImageContext:context];
}
#pragma mark 绘制文字
- (void)drawText : (CGContextRef) context{
NSString *str = @"今天咱们举办了一年一度的绘图大赛,同窗们踊跃参与。";
//定义文字显示区域
CGRect rect = CGRectMake(20, 50, 335, 300);
//创建一个段落样式
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
style.alignment = NSTextAlignmentCenter;//居中
//绘制文字,设置属性(字体大小,颜色)
[str drawInRect:rect withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor],NSParagraphStyleAttributeName:style}];
}
#pragma mark 绘制图片
- (void) drawImage : (CGContextRef) context
{
UIImage *image = [UIImage imageNamed:@"1"];
//1.从某一点开始绘制
// [image drawAtPoint:CGPointMake(0, 20)];
//2.绘制到指定的矩形中,注意:若是大小不合适会进行拉伸
[image drawInRect:CGRectMake(0, 100, 375, 667)];
UIImage *imageTwo = [UIImage imageNamed:@"2"];
[image drawInRect:CGRectMake(0, 300, 200, 200)];
//3.平铺绘制
// [image drawAsPatternInRect:CGRectMake(0, 0, 375, 667)];
}
#pragma mark 水印(图片上添加图片)
- (void)drawImageAtImageContext : (CGContextRef) context{
//开始图片上下文(设置画布大小)
UIGraphicsBeginImageContext(CGSizeMake(375, 667));
//建立图片
UIImage *image = [UIImage imageNamed:@"2"];
//裁剪图片->椭圆
context=UIGraphicsGetCurrentContext();//从新获取上下文
CGContextAddEllipseInRect(context, CGRectMake(0, 0, 375, 375));
CGContextClip(context);//裁剪
[image drawInRect:CGRectMake(0, 0, 375, 375)];
//添加水印
NSString *str = @"Photo by cxy";
[str drawInRect:CGRectMake(375/2.0-50, 667/2-10, 100, 30) withAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Arial" size:15],NSForegroundColorAttributeName:[UIColor whiteColor]}];
//返回绘制的新图像
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//关闭对应的上下文
UIGraphicsEndImageContext();
[newImage drawInRect:CGRectMake(0, 0, 375, 667)];
//保存图片
NSData *data = UIImagePNGRepresentation(newImage) ;
[data writeToFile:@"/Users/dc008/Desktop/photo.png" atomically:YES];
UIGraphicsBeginImageContext(CGSizeMake(375, 667));
UIImage *imageTwo = [UIImage imageNamed:@"1"];
context = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(context, CGRectMake(0, 0, 375, 375));
CGContextClip(context);
[imageTwo drawInRect:CGRectMake(0, 0, 375, 667)];
NSString *strTwo = @"Photo by cxy";
[strTwo drawInRect:CGRectMake(375/2.0-50, 667/2-10, 100, 30) withAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Arial" size:15],NSForegroundColorAttributeName:[UIColor whiteColor]}];
UIImage *newImageTwo = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[newImageTwo drawInRect:CGRectMake(0, 0, 375, 667)];
NSData *dataTwo = UIImagePNGRepresentation(newImageTwo);
[dataTwo writeToFile:@"/Users/dc008/Desktop/photoTwo.png" atomically:YES];
}
@end
//
// ViewController.m
// Core Graphics次日
//
// Created by dc008 on 15/12/8.
// Copyright © 2015年 CXY. All rights reserved.
//
#import "ViewController.h"
#import "MyView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
MyView *myView = [[MyView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
myView.backgroundColor = [UIColor colorWithRed:0.11 green:0.7 blue:0.6 alpha:0.3];
[self.view addSubview:myView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end