ios coregraphics详解 graphics是异步绘制的吗

3628人阅读
IOS 绘制心电图
这两天公司做一个医疗的项目,其中一个需求是接受传感器的病人心跳数据,将之在UI上面绘制成心电图. 在网上看了很多demo,废话不多说直接切入.(漏洞百出,欢迎批评指正)&
因为除了逻辑其实很简单,代码就没有放到githup上面。 这是demo的下载地址
一.Core Graphics
Core Graphics 是一个基于c的api编写的图形核心绘制引擎。提供比较底层的,轻量级的二维渲染和非常好的输出保真度
使用Core Graphics画出你想要的东西,一个很重要的实情就是 你需要一个能够让你画图的地方(俗称 画布),基本上三中方法
//搬运别人的 总结就是这样
第一种:创建图片类型的画布。调用UIGraphicsBeginImageContextWithOptions函数就可获得用来处理图片的图形上下文。
第二种:利用cocoa自动为你生成画布。当你子类化了一个UIView并实现了自己的drawRect:方法后,一旦drawRect:方法被调用,Cocoa就会为你创建一个图形上下文,
这上面2种都是自己弄的画布,没有引用当前图像所在的画布,如果自己不想创建画布,可以用当前的画布(类型是:CGContextRef)
具体看代码:
画图展示需要我们自定义一个UIView,重写初始化方法.
self.clearsContextBeforeDrawing = YES;//这个方法 是保证我们的改变挥着绘图能够生效- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.clearsContextBeforeDrawing = YES;
}在重写系统方法- (void)drawRect:(CGRect)rect 这时候系统就知道 你要发挥你的绘画天赋了,并已经准好了
- (void)drawRect:(CGRect)rect {
[self drawCurve]; // 绘制心电图型
[self drawGrid]; //绘制心电图 背景网格
//因为画的是心电图& 所以 必须要有心跳的背景网格对不对 所以第一步 就是先画出背景的网格
//绘制背景的网格
- (void)drawGrid{
CGContextRef context = UIGraphicsGetCurrentContext();//获取上下文 知道你要画图的地方 (就是画布)
CGFloat height = self.frame.size.
CGFloat width = self.frame.size.
// NSLog(@&高 &&%f 宽 && width %f&,height,width);
CGFloat cell_square_width = 30;//设置每一个格子的宽度
CGContextSetLineWidth(context, 1); //设置线宽
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); //设置线的颜色
设置绘制表格的起始点
NSInteger pos_x = 1;
while (pos_x &= width) {
CGContextSetLineWidth(context, 0.2);
CGContextMoveToPoint(context, pos_x, 1);
CGContextAddLineToPoint(context, pos_x, height);
pos_x += cell_square_
CGContextStrokePath(context);
NSInteger pos_y = 1;
while (pos_y &= height) {
CGContextMoveToPoint(context, 1, pos_y);
CGContextAddLineToPoint(context, width,pos_y);
pos_y += cell_square_
CGContextStrokePath(context);
cell_square_width = cell_square_width / 5; //刚才设置大格
现在 绘制小格
pos_x = 1;
while (pos_x &= width) {
CGContextSetLineWidth(context, 0.2);
CGContextMoveToPoint(context, pos_x, 1);
CGContextAddLineToPoint(context, pos_x, height);
pos_x += cell_square_
CGContextStrokePath(context);
pos_y = 1;
while (pos_y &= height) {
CGContextMoveToPoint(context, 1, pos_y);
CGContextAddLineToPoint(context, width,pos_y);
pos_y += cell_square_
CGContextStrokePath(context);
绘制玩背景 就要在背景上 开始描绘心跳了
这是绘制曲线的
- (void)drawCurve{
if (self.cunrrentPointCount == 0) { //当前点得数量 没有点 就不画了
CGFloat curveWidth = 0.8; //宽度
CGContextRef currentContext = UIGraphicsGetCurrentContext(); //跟上面一样
CGContextSetLineWidth(currentContext, curveWidth);
设置心跳的颜色
CGContextSetStrokeColorWithColor(currentContext, [UIColor greenColor].CGColor);
 CGContextMoveToPoint(currentContext, self.points[0].x, self.points[0].y);
//self.points 是一个CGPoint的属性
for (int i = 0; i != self.cunrrentPointC ++ i) {
if (self.points[i - 1].x & self.points[i].x) {
//这里的意思是 在已经获知 所有的点 我判断连个点得位置
//如果我右边 还有点 我就连上去 如果没有 我就移动到那
//什么也不干(等待下一组数据)
 CGContextAddLineToPoint(currentContext, self.points[i].x, self.points[i].y);
CGContextMoveToPoint(currentContext, self.points[i].x, self.points[i].y);
CGContextStrokePath(currentContext);
实现心电图有两种方式 一种是是平移的 就是我们经常在医院或者电视上看到的那种走纸带的,还有一种就是一个亮点在屏幕上来回移动画出心电图
- (void)addPointAsRefreshChangeform:(CGPoint)point{
static NSInteger currentPointsCount = 0;
//设置当前开始从0 刷新 使用static 保证只执行一次 不产生其他影响
if (currentPointsCount & kMaxContainerCapacity) {
self.numberOfRefreshElements = currentPointsCount + 1;
self.refreshPointContainer[currentPointsCount] =
currentPointsCount ++;
NSInteger workIndex = 0;
while (workIndex != kMaxContainerCapacity - 1) {
self.refreshPointContainer[workIndex] = self.refreshPointContainer[workIndex + 1];
workIndex ++;
self.refreshPointContainer[kMaxContainerCapacity - 1] =
self.numberOfRefreshElements = kMaxContainerC
经过一番折腾 为了便于理解画图 &讲drawCruve得方法 简化
- (void)drawCurve:(CGContextRef)ctx{
CGContextSetLineWidth(ctx, lineWidth_LiveMonitor);
CGContextSetStrokeColorWithColor(ctx, [UIColor greenColor].CGColor);
//将数组里的点画成连续的线
点进去介绍的很明白&span style=&font-family: Arial, Helvetica, sans-&&
CGContextAddLines(ctx, drawingPoints, _pointArray.count );
//这样 以后 我们只要把 要画的点传进drawdingPoints 然后调用 drawrect 就行
CGContextStrokePath(ctx);
有了drawrect里面的两个方法 就能完成画图 现在怎么样传递数据 &
写一个方法 用于外部调用. 用来传递数据
- (void)fireDrawing{
// 外面的数据先传给 pointArray
再将pointArray的值取出来 转成坐标
for (int i = 0 ; i & _pointArray. i++) {
//textX 是我定义的X的坐标 当它 & 屏幕的宽度是 就一直++ (实现左移)
pointMartin 是每次左移的距离
if (testX & self.width) {
drawingPoints[i] =
CGPointMake(testX,self.height/2 - [_pointArray[i] intValue] / 10);
testX = testX + pointM
testX = 0;
一部分 需要重画
在这里设置
心电图中间的黑格
因为 drawingPoints[1].x
一直在位移
所以重画的部分 也在偏移
&span style=&white-space:pre&& &/span&//
设置每次需要重新绘制的rect
CGRect reck = CGRectMake(testX - _pointArray.count * pointMartin, 0,
10, self.height);
dispatch_async(dispatch_get_main_queue(), ^{
[self setNeedsDisplayInRect:reck];&pre name=&code& class=&objc&&//
每次画图的第一个点 应该是上次画的最后一个点 testX = testX - pointM }); }
外部 调用fireDrawing的时候 传进来数据 就可以实现绘制
在控制器中引入 头文件 开始使用数据绘图
- (void)viewWillAppear:(BOOL)animated{
[self popDataTimerEvent];
- (void)popDataTimerEvent{
&span style=&white-space:pre&& &/span&//注意定时器的用法
如何释放 这里定时器会让self的引用计数 + 1,
self.popDataTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(popData) userInfo:nil repeats:YES];
_index = 0;
[[NSRunLoop currentRunLoop] addTimer:self.popDataTimer forMode:NSDefaultRunLoopMode];
- (void)popData{
TripleECGView *view = self.ecgViewsArr[0];
NSMutableArray *temparr = [NSMutableArray array];
for (int i = 0; i & _drawingCountO i++) {
[temparr addObject:_testArr[_index + i]];
//传入数据
view.pointArray =
for (int i = 0; i & 3; i++) {
[view.pointArray addObject:_testArr[_index + i]];
&span style=&white-space:pre&& &/span&// 调用画图
[view fireDrawing];
_index += _drawingCountO
if (_index & 420) {
_index = 0;
这时候 模拟器就会出现传递数据汇出的图形
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:10511次
排名:千里之外
原创:18篇
评论:24条
(1)(1)(1)(2)(1)(3)(9)(2)[摘要:同步画造专题
编纂图片的几个方式
先用UIImage工具减载一张图片
然后转化成CGImageRef放到CGContext中往编纂
用CGImageCreate函数建立CGI]
异步绘制专题
1&&&&图片处理
1.1&&&&编辑图片的几个方法
先用UIImage对象加载一张图片
然后转化成CGImageRef放到CGContext中去编辑
用CGImageCreate函数创建CGImageRef
然后把CGImageRef放到CGContext中去编辑
用CGImageCreateCopy或者CGImageCreateCopyWithColorSpace
CGImageRef CGImageCreate (
&& size_t width, //图片的宽度
&& size_t height, //图片的高度
&& size_t bitsPerComponent,& //图片每个颜色的bits,比如rgb颜色,有可能是<span style="color:#或者
&& size_t bitsPerPixel,& //每一个像素占用的buts,<span style="color:#位<span style="color:#位
&& size_t bytesPerRow, //每一行占用多少bytes注意是bytes不是bits
&1byte= 8bit
&& CGColorSpaceRef colorspace,& //颜色空间,比如rgb
&& CGBitmapInfo bitmapInfo,& //layout,像素中bit的布局,是rgba还是
argb,==
&& CGDataProviderRefprovider,& //数据源提供者,url或者内存==
&& const CGFloat decode[],& //一个解码数组
&& bool shouldInterpolate,& //抗锯齿参数
&& CGColorRenderingIntent intent
//图片渲染相关参数
1.2&&&&示例代码
CGImageRef CGImageCreate(size_t width,size_t
&& size_tbitsPerComponent,size_t bitsPerPixel,size_t
bytesPerRow,
&& CGColorSpaceRef space, CGBitmapInfo bitmapInfo, CGDataProviderRefprovider,
&& constCGFloat decode[],bool shouldInterpolate,
&& CGColorRenderingIntent intent);
通过这个方法,我们可以创建出一个CGImageRef类型的对象,下面分别对参数进行解释:
sizt_t是定义的一个可移植性的单位,在64位机器中为8字节,32位位4字节。
width:图片宽度像素
height:图片高度像素
bitsPerComponent:每个颜色的比特数,例如在rgba-32模式下为<span style="color:#
bitsPerPixel:每个像素的总比特数
bytesPerRow:每一行占用的字节数,注意这里的单位是字节
space:颜色空间模式,例如constCFStringRef kCGColorSpaceGenericRGB这个函数可以返回一个颜色空间对象。
bitmapInfo:位图像素布局,枚举如下:
typedef&CF_OPTIONS(uint32_t,&CGBitmapInfo)&{
&&kCGBitmapAlphaInfoMask&=&0x1F,
&&kCGBitmapFloatComponents&=&(1&&&&8),
&&kCGBitmapByteOrderMask&=&0x7000,
&&kCGBitmapByteOrderDefault&=&(0&&&&12),
&&kCGBitmapByteOrder16Little&=&(1&&&&12),
&&kCGBitmapByteOrder32Little&=&(2&&&&12),
&&kCGBitmapByteOrder16Big&=&(3&&&&12),
&&kCGBitmapByteOrder32Big&=&(4&&&&12)
provider:数据源提供者
decode[]:解码渲染数组
shouldInterpolate:是否抗锯齿
intent:图片相关参数
CGImageRef CGImageMaskCreate(size_t width,size_t
& & size_t bitsPerComponent,size_t bitsPerPixel,size_t
bytesPerRow,
& & CGDataProviderRef provider,constCGFloat
decode[],bool shouldInterpolate)
这个方法用于创建mask图片图层,可以设置其显示部分与不显示部分达到特殊的效果,参数意义同上。
CGImageRef CGImageCreateCopy(CGImageRef image)
这个方法可以复制一个CGImageRef对象
CGImageRef CGImageCreateWithJPEGDataProvider(CGDataProviderRef
& & source, constCGFloat decode[],bool
shouldInterpolate,
& & CGColorRenderingIntent intent)
通过JPEG数据源获取图像
CGImageRef CGImageCreateWithPNGDataProvider(CGDataProviderRef source,
& & constCGFloat decode[],bool shouldInterpolate,
& & CGColorRenderingIntent intent)
通过PNG数据源获取图像
CGImageRef CGImageCreateWithImageInRect(CGImageRef image,
& & CGRect rect)
截取图像的一个区域重绘图像
CGImageRef CGImageCreateWithMask(CGImageRef image,CGImageRef
截取mask图像的某一区域重绘
CGImageRef CGImageCreateWithMaskingColors(CGImageRef image,
& & constCGFloat components[])
通过颜色分量数组创建位图
CGImageRef CGImageCreateCopyWithColorSpace(CGImageRef image,
& & CGColorSpaceRef space)
通过颜色空间模式复制位图
CGImageRef CGImageRetain(CGImageRef image)
引用&#43;1
void CGImageRelease(CGImageRef image)
bool CGImageIsMask(CGImageRef image)
返回是否为Mask图层
size_t CGImageGetWidth(CGImageRef image)
获取宽度像素
size_t CGImageGetHeight(CGImageRef image)
获取高度像素
下面这些方法分别获取相应属性
size_t CGImageGetBitsPerComponent(CGImageRef image)
size_t CGImageGetBitsPerPixel(CGImageRef image)
size_t CGImageGetBytesPerRow(CGImageRef image)
CGColorSpaceRef CGImageGetColorSpace(CGImageRef image)CG_EXTERNCGImageAlphaInfo
CGImageGetAlphaInfo(CGImageRef image)
CGDataProviderRef CGImageGetDataProvider(CGImageRef image)
constCGFloat *CGImageGetDecode(CGImageRef image)
bool CGImageGetShouldInterpolate(CGImageRef image)
CGColorRenderingIntent CGImageGetRenderingIntent(CGImageRef image)
CGBitmapInfo CGImageGetBitmapInfo(CGImageRef image)
1.3&&&&PNG与JPEG优劣比较
速度:JPG更快
压缩比:JPG更大;
图片质量:JPG更好
JPG不支持透明效果;
UIImageJPEGRepresentation方法在耗时上比较少 而UIImagePNGRepresentation耗时操作时间比较长
UIImageJPEGRepresentation函数需要两个参数:图片的引用和压缩系数.而UIImagePNGRepresentation只需要图片引用作为参数.通过在实际使用过程中,比较发现:UIImagePNGRepresentation(UIImage* image) 要比UIImageJPEGRepresentation(UIImage*image, 1.0) 返回的图片数据量大很多.譬如,同样是读取拍摄的同样景色的,
UIImagePNGRepresentation()返回的数据量大小为199K ,而 UIImageJPEGRepresentation(UIImage* image, 1.0)返回的数据量大小只为140KB,比前者少了50多KB.如果对图片的清晰度要求不高,还可以通过设置 UIImageJPEGRepresentation函数的第二个参数,大幅度降低图片数据量.譬如,刚才拍摄的图片, 通过调用UIImageJPEGRepresentation(UIImage*image, 1.0)读取数据时,返回的数据大小为140KB,但更改压缩系数后,通过调用UIImageJPEGRepresentation(UIImage*
image, 0.5)读取数据时,返回的数据大小只有11KB多,大大压缩了图片的数据量 ,而且从视角角度看,图片的质量并没有明显的降低.因此,在读取图片数据内容时,建议优先使用UIImageJPEGRepresentation,并可根据自己的实际使用场景,设置压缩系数,进一步降低图片数据量大小.
1.4&&&&图片缩放
图片缩放的三个函数
/pengyingh/articles/2355052.html
中一个界面用到了好多张大图,内存报警告了,所以做了一下图片缩放,在网上找了别人写的代码
//把图片做等比缩放,生成一个新图片
1 - (UIImage*) imageByScalingProportionallyToSize:(CGSize)targetSize sourceImage:(UIImage*)sourceImage {
&3&&&&UIGraphicsBeginImageContext(targetSize);
&4&&&&&&&& [sourceImage drawInRect:CGRectMake(0,0,targetSize.width, targetSize.height)];
&5&&&&&&&&UIImage* scaledImage =UIGraphicsGetImageFromCurrentImageContext();
&6&&&&&&&& UIGraphicsEndImageContext();
&7&&&&&&&&
returnscaledI
&9&&&&UIImage *newImage =
10&&&& CGSize imageSize = sourceImage.
11&&&& CGFloat width = imageSize.
12&&&& CGFloat height = imageSize.
13&&&& CGFloat targetWidth = targetSize.
14&&&& CGFloat targetHeight = targetSize.
15&&&& CGFloat scaleFactor =
16&&&& CGFloat scaledWidth = targetW
17&&&& CGFloat scaledHeight = targetH
18&&&& CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
20&&&& UIGraphicsBeginImageContext(targetSize);// this will crop
22&&&& CGRect thumbnailRect = CGRectZ
23&&&& thumbnailRect.origin = thumbnailP
24&&&& thumbnailRect.size.width& = scaledW
25&&&& thumbnailRect.size.height = scaledH
27&&&& [sourceImage drawInRect:thumbnailRect];
29&&&& newImage =UIGraphicsGetImageFromCurrentImageContext();
if(newImage== nil)
31&&&&&&&& NSLog(@&couldnot scale image&);
//pop thecontext to get back to the default
34&&&& UIGraphicsEndImageContext();
returnnewI
//把图片按照新大小进行裁剪,生成一个新图片
1 - (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize image:(UIImage *)sourceImage
&3&&&&//&&&UIImage *sourceImage =
&4&&&&UIImage *newImage =
&5&&&&CGSize imageSize = sourceImage.
&6&&&&CGFloat width = imageSize.
&7&&&&CGFloat height = imageSize.
&8&&&&CGFloat targetWidth = targetSize.
&9&&&&CGFloat targetHeight = targetSize.
10&&&& CGFloat scaleFactor =
11&&&& CGFloat scaledWidth = targetW
12&&&& CGFloat scaledHeight = targetH
13&&&& CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
if(CGSizeEqualToSize(imageSize, targetSize) == NO)
17&&&&&&&& CGFloat widthFactor = targetWidth /
18&&&&&&&& CGFloat heightFactor = targetHeight /
20&&&&&&&&
if(widthFactor & heightFactor)
21&&&&&&&&&&&& scaleFactor = widthF// scale to fit height
22&&&&&&&&
23&&&&&&&&&&&& scaleFactor = heightF// scale to fit width
24&&&&&&&& scaledWidth& = width * scaleF
25&&&&& &&&scaledHeight = height * scaleF
27&&&&&&&&
// centerthe image
28&&&&&&&&
if(widthFactor & heightFactor)
29&&&&&&&& {
30&&&&&&&&&&&& thumbnailPoint.y = (targetHeight -scaledHeight) *0.5;
31&&&&&&&& }
32&&&&&&&&
33&&&&&&&&&&&&
if(widthFactor & heightFactor)
34&&&&&&&&&&&& {
35&&&&&&&&&&&&&&&& thumbnailPoint.x =(targetWidth - scaledWidth) *0.5;
36&&&&&&&&&&&& }
37&&&& }&&&&&&
39&&&& UIGraphicsBeginImageContext(targetSize);// this will crop
41&&&& CGRect thumbnailRect = CGRectZ
42&&&& thumbnailRect.origin = thumbnailP
43&&&& thumbnailRect.size.width& = scaledW
44&&&& thumbnailRect.size.height = scaledH
46&&&& [sourceImage drawInRect:thumbnailRect];
48&&&& newImage =UIGraphicsGetImageFromCurrentImageContext();
if(newImage== nil)
50&&&&&&&& NSLog(@&couldnot scale image&);
//pop thecontext to get back to the default
53&&&& UIGraphicsEndImageContext();
returnnewI
1 - (UIImage*)generatePhotoThumbnail:(UIImage *)image
&3&&&&// Create a thumbnail
of the imagefor the event object.
&4&&&&CGSize size = image.
&5&&&&CGSize croppedS
&6&&&&CGFloat ratio =
64.0;//这个是设置转换后图片的尺寸大小
&7&&&&CGFloat offsetX =
&8&&&&CGFloat offsetY =
// check thesize of the image, we want to make it
// a square with sides the size of the smallest dimension
if(size.width & size.height) {
13&&&&&&&& offsetX = (size.height - size.width) /2;
14&&&&&&&& croppedSize = CGSizeMake(size.height,size.height);
16&&&&&&&& offsetY = (size.width - size.height) /2;
17&&&&&&&& croppedSize = CGSizeMake(size.width,size.width);
// Crop theimage before resize
21&&&& CGRect clippedRect = CGRectMake(offsetX *-1, offsetY * -1,croppedSize.width, croppedSize.height);
22&//裁剪图片
CGImageRef imageRef =CGImageCreateWithImageInRect([image CGImage], clippedRect);
// Donecropping
// Resize the image
26&&&& CGRect rect = CGRectMake(0.0,0.0,ratio, ratio);
28&&&& UIGraphicsBeginImageContext(rect.size);
29&&&& [[UIImage imageWithCGImage:imageRef]drawInRect:rect];
30&&&& UIImage *thumbnail =UIGraphicsGetImageFromCurrentImageContext();
31&&&& UIGraphicsEndImageContext();
// DoneResizing
实际应用简化
- (UIImage *)generatePhotoThumbnail:(UIImage*)image
&&&CGRect rect=CGRectMake(0,
0, 60, 78);
//裁剪图片
&&&CGImageRef imageRef=CGImageCreateWithImageInRect([image CGImage],CGRectMake(0,0,140,182));
&&&UIGraphicsBeginImageContext(rect.size);
&&&[[UIImage imageWithCGImage:imageRef]drawInRect:rect];
//如果不裁剪图片可以直接画
&&&//[image drawInRect:CGRectMake(0, 0, theSize.width, theSize.height)];
&&&UIImage *thumbnail=UIGraphicsGetImageFromCurrentImageContext();
&&&UIGraphicsEndImageContext();
UIImage类并没有提供缩放图片需要用到的API,是不是觉得很吃惊?没关系,我们自己来添加一个。
定义缩放图片的Category
//& UIImage&#43;Scale.h
@ UIImage (scale)
-(UIImage*)scaleToSize:(CGSize)
实现这个Category的定义
// UIImage&#43;Scale.m&
#import &UIImage&#43;Scale.h&
@implementation UIImage (scale)
-(UIImage*)scaleToSize:(CGSize)size
{// 创建一个bitmap的context//并把它设置成为当前正在使用的context
&UIGraphicsBeginImageContext(size);
&// 绘制改变大小的图片
[self drawInRect:CGRectMake(0,0,size.width, size.height)];
&// 从当前context中创建一个改变大小后的图片
&UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
&// 使当前的context出堆栈
&UIGraphicsEndImageContext();
&// 返回新的改变大小后的图片
return scaledI
// 创建图片
UIImage *image =[UIImage imageNamed:@&myImage.png&];
// 更改图片大小
UIImage *scaledImage =[image scaleToSize:CGSizeMake(25.0f,35.0f)]
1.5&&&&参考链接
IOS-图片操作集合
http://blog.csdn.net/ch_soft/article/details/7685753
UIImagePNGRepresentation 存在缓慢问题
.cn/s/blog_95aws.html
&iOS&UIImage变为NSData并进行压缩
/robinkey/archive//2869930.html
UIImageJPEGRepresentation和UIImagePNGRepresentation
http://blog.csdn.net/mideveloper/article/details/
png有透明通道,JPEG无
/chentong/blog/static//
透明PNG圖片有黑邊的解決方法
用UIImage和UIButton画出的按钮,使用透明的png图片,为什么会出现白边
JPG、PNG和GIF图片的基本原理及优化方法
//400.html
JPEG 原理详细
http://blog.chinaunix.net/uid--id-3220554.html
IOS开发中图片资源使用png还是jpg&#26684;式
/wengzilin/p/3485298.html
(good)ios开发图片&#26684;式的选择:png和jpg
http://m.blog.csdn.net/blog/awaylin113/
IOS开发之保存图片到Documents目录及PNG,JPEG&#26684;式相互转换
http://blog.csdn.net/sanpintian/article/details/7418755
iOS过滤png图片透明部分点击事件
/industry/2.html
JPEG压缩原理
http://blog.csdn.net/xfortius/article/details/8904012
png压缩原理
http://blog.csdn.net/zykun/article/details/1825086
iOS开发,图片使用png好还是jpg好?
/bbs/read.php?tid=110115
2&&&&绘制文本&
2.1&&&&NSMutableAttributedString绘制
CGRect textViewRect =
CGRectMake(ICON_SPACE*1-2,_imageHeight &#43;ICON_SPACE,_postContentTextView.frame.size.width,_labelSize);//ceilf(_labelSize)
//&&&&&&&&&&& [_postContentTextViewdrawRect:textViewRect];
&&&&&&&&&&&NSMutableAttributedString *str = [[NSMutableAttributedStringalloc]initWithString:_vm.contentText];
&&&&&&&&&&&
&&&&&&&&&&&[str addAttribute:NSForegroundColorAttributeNamevalue:kContentTextColorrange:NSMakeRange(0,[_vm.contentTextlength])];
&&&&&&&&&&&[str addAttribute:NSFontAttributeNamevalue:kContentTextFontrange:NSMakeRange(0,[_vm.contentTextlength])];
&&&&&&&&&&&[str addAttribute:NSBackgroundColorDocumentAttributevalue:[UIColorwhiteColor]range:NSMakeRange(0,[_vm.contentTextlength])];
&&&&&&&&&&&
//&&&&&&&&&&& NSLog(@&ContentText:%@, Frame:%@&,_vm.contentText, NSStringFromCGRect(_postContentTextView.frame));
//&&&&&&&&&&&NSLog(@&TextViewRect:%@&,NSStringFromCGRect(textViewRect));
&& &&&&&&&&&[str drawInRect:textViewRect];&
2.2&&&&参考资料
IOS开发(78)之绘制文本
/kf/045.html
iOS 界面上绘制不同字体 颜色 大小的
http://blog.csdn.net/wsk_123_123/article/details/
初探NSAttributedString和NSMutableAttributedString的使用 -LiuWJ
/articles/Fvqia2
iOS 字符属性NSAttributedString描述
http://my.oschina.net/lanrenbar/blog/395909
NSAttributedString 详解
/zhw511006/archive//2696700.html
3&&&&异步绘制
3.1&&&&异步绘制示例
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
&&&&&&&CGRect drawRect =
_bgImageView.frame;
&&&&&&&UIGraphicsBeginImageContextWithOptions(drawRect.size,YES,0);
&&&&&&&CGContextRef context =
UIGraphicsGetCurrentContext();
&&&&&&&if (!context) {
&&&&&&&&&&&return;
&&&&&&&[[UIColor
whiteColor] set];
&&&&&&&CGContextFillRect(context, drawRect);
&&&&&&&CGRect imgRect =
CGRectZero;
&&&&&&&if ([_vm.contentImgPathlength] &0) {
&&&&&&&&&&&imgRect = CGRectMake(0,0,BODY_HEIGTH,_imageHeight);//_postContentImageView.
&&&&&&&&&&&[_vm.contentImagedrawInRect:_postContentImageView.frameblendMode:kCGBlendModeNormalalpha:1];
&&&&&&&CGRect textViewRect =
CGRectZero;
&&&&&& &if ([_vm.contentTextlength] &0) {
&&&&&&&&&&& NSMutableAttributedString*
&&&&&&&&&&& if (!_isContentDisplayCompletly) {
&&&&&&&&&&&&&&& if (_vm.digestText) {
&&&&&&&&&&&&&&&&&&& str = [[NSMutableAttributedStringalloc]initWithString:_vm.digestTextattributes:_postContentTextView.typingAttributes];
&&&&&&&&&&&&&&& } else
&&&&&&&&&&&&&&&&&&& str = [[NSMutableAttributedStringalloc]initWithString:_vm.contentTextattributes:_postContentTextView.typingAttributes];
&&&&&&&&&&& } else
&&&&&&&&&&&&&&& str = [[NSMutableAttributedStringalloc]initWithString:_vm.contentTextattributes:_postContentTextView.typingAttributes];
&&&&&&&&&&& [str drawInRect:_postContentTextView.frame];
&&&&&&& if (_subjectTitleHeight&0) {
&&&&&&&&&&&
&&&&&&& &&&&CGRectsubjectIconFrame =CGRectMake(_subjectButton.frame.origin.x,_subjectButton.frame.origin.y,10,12);//kContentToSubjectSpace
&&&&&&&&&&& UIImage*iconImg = [UIImageimageNamed:@&PostChannelFlageIcon&];
&&&&&&&&&&& subjectIconFrame.size =iconImg.size;
&&&&&&&&&&& [iconImg drawInRect:subjectIconFrameblendMode:kCGBlendModeNormalalpha:1];
&&&&&&&&&&&
&&&&&&&&&&& CGRectsubjectTitleFrame =CGRectMake(subjectIconFrame.origin.x &#43;subjectIconFrame.size.width
&#43;2,subjectIconFrame.origin.y-1,200,_subjectTitleHeight);
&&&&&&&&&&&
&&&&&&&&&&& [_subjectButton.titleLabel.attributedTextdrawInRect:subjectTitleFrame];
//&&&&&&&&&&& [_subjectButtondrawRect:_subjectButton.frame];
&&&&&&& UIImage*temp =
UIGraphicsGetImageFromCurrentImageContext();
&&&&&&& UIGraphicsEndImageContext();
&&&&&&& dispatch_async(dispatch_get_main_queue(),^{
&&&&&&&&&&& _bgImageView.image =nil;
&&&&&&&&&&& _bgImageView.image =
&&&&&&&&&&& [self
setHidden:NO];
&&&&&&& });
3.2&&&&DrawRect之后注意用hitTest:withEvent:方法处理事件接收
//用户触摸时第一时间加载内容
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event{
&&& UIView*result = [superhitTest:pointwithEvent:event];
&&& CGPointbuttonPoint = [_subjectButtonconvertPoint:pointfromView:self];
&&& if ([_subjectButtonpointInside:buttonPointwithEvent:event]){
&&&&&&& return
_subjectButton;
&&& return
3.3&&&&参考链接
[iOS Animation]-CALayer 绘图效率-异步绘制
http://my.oschina.net/u/2438875/blog/507545?fromerr=R4LnEaJ5
CGDataProviderCreateWithData对内存数据的释放
//cgdataprovidercreatewithdata_memory_release/#.VnJQ6jaitZF
IOS中使用像素位图(CGImageRef)对图片进行处理
http://my.oschina.net/u/2340880/blog/406437?p={{currentPage-1}}
4&&&&Asyncdisplaykit
4.1&&&&参考链接
Asyncdisplaykit 指南(一)
/Program/IOS/413.shtml
AsyncDisplayKit 教程:达到 60FPS 的滚动帧率
/swift/98.html
http://asyncdisplaykit.org/guide/
AsyncDisplayKit入门指南
/ios/5.html
5.1&&&&常见问题
5.1.1 CGBitmapContextCreateImage绘制后内存泄露导致内存告警
1、&&&&&&&&CGBitmapContextCreateImage绘制的图片会造成内存无法释放,应该换用CGDataProviderCreateWithCFData。
5.1.1.1&方案一:修改,入缓存前压缩
http://my.oschina.net/u/1244672/blog/510379
SDWebImage有一个SDWebImageDownloaderOperation类来执行下载操作的。里面有个下载完成的方法:
- (void )connectionDidFinishLoading:(NSURLConnection*)aConnection
SDWebImageDownloaderCompletedBlockcompletionBlock = pletedB
@synchronized <span s

我要回帖

更多关于 coregraphics 的文章

 

随机推荐