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.比较运算符 > .< .== . >= .<= . != 运算符还可以跟逻辑运算符一起使用,&& , || ...
随机推荐
- C#调用C++编写的dll库
我用vs2008建的C++ dll类库,名字test_interopVC,和C#的CeshiVC项目 一:C++项目dll类库: 1.test_interopVC.h,主要简单的测试,所以就在一个类里 ...
- 编译ffmpeg(iOS)
一,x264库的编译 首先到http://www.videolan.org/developers/x264.html下载x264的库,然后解压,修改文件夹名称为x264 二,下载ffmpeg2 ...
- maven3实战之maven使用入门(使用archetype生成项目骨架)
maven3实战之maven使用入门(使用archetype生成项目骨架) ---------- maven提供了archetype以帮助我们快速勾勒出项目骨架.以Hello World为例,我们使用 ...
- thinkphp 3+ 观后详解 (1)
最近面试了一些公司,发现自己的对于架构能力的不足,于是决定开始从最基本的代码做起.先看看大牛们怎么架构整个框架的.鉴于国外的框架比较难懂,于是就选择了国内比较流行的thinkphp来进行研究. 下面写 ...
- MFC容器类介绍
我们知道如果是单个的少数几个值弄些int , long,float ,double等类型的变量来装这些值就行了.但如果值太多这样就比较麻烦.当然数据超级多时就直接放数据库里存着去了. 但如果数值不多不 ...
- Cocos2D-x权威指南:通过节点控制屏幕中的全体渲染对象
本节,已经能够利用我们眼下所学的知识做出一些有趣的东西.之前已经说过,CCNode类没有贴图,也就是说在屏幕上单独建立一个节点是没有不论什么效果的,可是能够通过这个"无形"的节点来 ...
- 博客标题栏增加一个"闪存“按钮
最近来博客园喜欢去闪存上看看,也就是一个类似微博的东西,但是貌似没看到哪里有这个按钮. 所以只要自己动手搞一个. 暴力猴js: // ==UserScript== // @name fwindpeak ...
- android标题栏(titlebar)显示进度条
在后台线程中执行各种操作(网络连接.大数据存储)的时候,我们希望让客户能看到后台有操作在进行,那么既能有效的提示用户,又不占用当前操作空间,最好的方法就是在标题栏有个进度条. [代码] [Java]代 ...
- [CSS] Introduction to CSS Columns
Learn how to use CSS columns to quickly lay out fluid columns that are responsive, degrade gracefull ...
- [AngularJS] Accessing Services from Console
Using the Chrome console, you can access your AngularJS injectable services. This is down and dirty ...