iOS 画图讲解2
//layer上下文只能显示在drawRect里
//当开启上下文时,绘制图形即可在viewDidLoad中实现
//位图的上下文
//UIGraphicsBeginImageContext()仅适用于非retina屏
//开启位图上下文
size:位图的尺寸
opaque:不透明是yes,透明就是no
scale:是否缩放上下文
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
//绘制原始图片
[image drawAtPoint:CGPointZero];
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
//第一种
NSString * info = @"@大欢";
NSDictionary * dict = @{NSForegroundColorAttributeName:[UIColor whiteColor],
NSFontAttributeName:[UIFont systemFontOfSize:30]};
[info drawAtPoint:CGPointMake(120, 200) withAttributes:dict];
//第二种
UIBezierPath * path = [UIBezierPath bezierPathWithRect:CGRectMake(100, 100, 100, 100)];
[[UIColor redColor] set];
[path fill];
//第三种
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(ctx, 20, 20);
CGContextAddLineToPoint(ctx, 40, 40);
CGContextStrokePath(ctx);
//生成一张新的图片,从位图上下文获取
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
//关闭图片上下文
UIGraphicsEndImageContext();
self.imageView.image = newImage;
2、图片等比例缩小
操作 写在 通过上下文获取图片 之前
UIImage * image = [UIImage imageNamed:@"huoyanshan.jpg"];
CGFloat newImageWH = image.size.height;
//开启位图上下文
UIGraphicsBeginImageContextWithOptions(CGSizeMake(newImageWH, newImageWH), NO, 0);
UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, newImageWH, newImageWH)];
//添加剪切
[path addClip];
//绘图
[image drawAtPoint:CGPointZero];
//通过上下文获取图片
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
//关闭上下文
UIGraphicsEndImageContext();
self.imageView.image = newImage;
3、画线
//纯代码执行的第一个方法
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setUp];
}
return self;
}
//故事板执行的第一个方法
- (void)awakeFromNib {
[self setUp];
}
4、相册
//保存到手机相册
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
//创建相册
UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
<UINavigationControllerDelegate, UIImagePickerControllerDelegate>写两个协议
imagePicker.delegate = self;
//UIImagePickerControllerSourceTypePhotoLibrary 相册
//UIImagePickerControllerSourceTypeCamera 相机
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//允许编辑
imagePicker.allowsEditing = YES;
[self presentViewController:imagePicker animated:YES completion:nil];
}
//相册选择完成的方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
//编辑图片 如果allowsEditing为NO,image没有值
_drawView.image = info[UIImagePickerControllerEditedImage];
//原始图片
_drawView.image = info[UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:nil];
}
//取消选择
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
NSLog(@"取消选择");
[picker dismissViewControllerAnimated:YES completion:nil];
}
iOS 画图讲解2的更多相关文章
- iOS 画图讲解
5.画图 (1)画线 //绘图代码写在drawRect里,view加载完成,需要显示的时候调用 //1.获取图形上下文 2.创建路径 3.把图形放入上下文 4.渲染上下文 //drawRect的rec ...
- IOS NSUserDefaults 讲解 用法
IOS NSUserDefaults 讲解 用法 NSUserDefaults适合存储轻量级的本地数据,比如要保存一个登陆界面的数据,用户名.密码之类的,个人觉得使用NSUserDefaults ...
- iOS开发讲解SDWebImage,你真的会用吗?
SDWebImage作为目前最受欢迎的图片下载第三方框架,使用率很高.但是你真的会用吗?本文接下来将通过例子分析如何合理使用SDWebImage. 使用场景:自定义的UITableViewCell上有 ...
- 利用IOS画图功能画出五角星,并且可以调整五角星的填充范围
我们要花的为一个黄色的五角星并且其中的填充黄色能够任意调整,比如只填满半个五角星,或者只填满一个角等等. 首先要重写DrawRect 方法,然后在这里实现我们的画图代码. - (void)drawRe ...
- iOS 画图基础
基础要点: 1,画图不可以在 ViewController 里,而是应该在一个 UIView 的子类中,比如新建一个 DrawView 继承自 UIView. 2,覆盖 UIView 的 drawRe ...
- ios 画图总结
0 CGContextRef context = UIGraphicsGetCurrentContext(); 设置上下文1 CGContextMoveToPoint 开始画线2 CGContextA ...
- ios 深入讲解iOS键盘一:控制键盘隐藏显示
在iOS的开发中,我们一般使用UITextField.UITextView处理文字输入等操作,大部分情况下我们只需要一两行代码去手动管理键盘的显示隐藏:让UITextField或UITextView成 ...
- ios开发讲解之anchorPoint和position详解
引言 相信初接触到CALayer的人都会遇到以下几个问题: 为什么修改anchorPoint会移动layer的位置? CALayer的position点是哪一点呢? anchorPoint与posi ...
- iOS 谓词讲解
1.NSPredicate (1)比较运算符 1.比较运算符 > .< .== . >= .<= . != 运算符还可以跟逻辑运算符一起使用,&& , || ...
随机推荐
- Oracle日志文件管理与查看
Oracle日志文件管理与查看 from:http://hi.baidu.com/shuker/item/25ee611ee960c7426826bb1f 1.查询系统使用的是哪一组日志文件: sel ...
- C#中的Collection 2
Core Generic interface IEnumerable<T>:you can interate my elemnts, no need to know the count, ...
- 测试rest接口的两个工具使用详解(restclient+soapUI)
http://www.cnblogs.com/lanxuezaipiao/archive/2013/05/31/3110764.html http://www.oschina.net/news/618 ...
- DateTable与List<T>相互转换 及JSON与DataTable(DataSet)相互转化
http://www.360doc.com/content/13/0712/09/10504424_299336674.shtml Linq处理List数据 http://blog.163.com/l ...
- CString和string的互相转换
CString->std::string 例子: CString strMfc=“test“; std::string strStl; strStl=strMfc.GetBuffer(0); s ...
- jsp中使用动态数据进行mySQL数据库的两种操作方法
使用动态数据进行数据库内容的增删改查操作有两种方法: 在此定义数据库连接为conn 假设有表单进行数据输入并提交到处理页面一种是使用预编译格式: 其格式如下: String name = reques ...
- 14的路 MySQL的btree索引和hash索引的区别
http://www.cnblogs.com/vicenteforever/articles/1789613.html ash 索引结构的特殊性,其检索效率非常高,索引的检索可以一次定位,不像B-Tr ...
- jquery Mobile点击显示加载等待效果
点击某个按钮或链接时,触发等待加载效果: <script> <!-- $(document).bind("mobileinit", function(){ }); ...
- Codeforces Educational Codeforces Round 3 A. USB Flash Drives 水题
A. USB Flash Drives 题目连接: http://www.codeforces.com/contest/609/problem/A Description Sean is trying ...
- 第七届ACM趣味程序设计竞赛第四场(正式赛) 题解
Final Pan's prime numbers 题目连接: http://acm.uestc.edu.cn/#/problem/show/1272 题意 给你n,要求你在[4,n]范围内找到一个最 ...