iPhone图形开发绘图教程是本文要介绍的内容,介绍了很多关于绘图类的使用,先来看详细内容讲解。
1、绘图总结:
绘图前设置:
- CGContextSetRGBFillColor
/CGContextSetFillColorWit hColor //填充色 - CGContextSetRGBStrokeCol
or/CGContextSetStrokeColorW ithColor //笔颜色 - CGContextSetLineWidth
//线宽度
绘图后设置:
注:
2.常见图形绘制:
- CGContextFillRect/CGContextFillRects
- CGContextFillEllipseInRe
ct - CGContextAddRect/CGContextAddRects
- CGContextAddEllipseInRec
t - CGContextAddLines
- CGContextMoveToPoint
- CGContextAddLineToPoint
3.常见控制方法:
- CGContextSaveGState
- CGContextRestoreGState
4.创建内存图像context:
- CGBitmapContextCreate
<-----CGContextRlease释放 - CGColorSpaceCreateWithNa
me (KCGColorSpaceGenericRGB) - CGColorSpaceRlease
- CGBitmapContextCreateIma
ge() <-----CGImageRlease 释放. - eg:
- CGContextRefMyCreateBitm
apContext(intpixelsWide,intpixelsHigh) - {
- CGContextRef
context=NULL; - CGColorSpaceRefcolorSpac
e; - void*
bitmapData; - int
bitmapByteCount; - int
bitmapBytesPerRow; - bitmapBytesPerRow
=(pixelsWide*4); - bitmapByteCount
=(bitmapBytesPerRow*pixelsHigh); - colorSpace=CGColorSpaceCreateDevice
RGB(); - bitmapData=malloc(bitmapByteCount);
- if(bitmapData==NULL)
- {
- fprintf(stderr,"Memorynotallocated!");
- returnNULL;
- }
- context=CGBitmapContextCreate(bitmapData,
-
pixelsWide, pixelsHigh, 8, - bitmapBytesPerRow,
colorSpace, -
kCGImageAlphaPremultipli edLast); - if(context==NULL)
- {
- free(bitmapData);
- fprintf(stderr,"Contextnotcreated!");
- returnNULL;
- }
- CGColorSpaceRelease(colorSpace);
- returncontext;
- }
5.图形的变换:
- CGContextTranslateCTM
- CGContextRotateCTM
- CGContextScaleCTM
6.常用函数:
-
CGRectContainsPoint(); - CGRectContainsRect();
- CGRectIntersectsRect();
- CGRectIntersection();
- CGPointEqualToPoint();
- CGSizeEqualToSize();
7.从原图片中取小图.
- CGImageCreateWithImageIn
Rect
8.屏幕快照:
- #import
"QuartzCore/QuartzCore.h" -
- UIGraphicsBeginImageCont
ext(yourView.frame.size); - [[yourView
layer] renderInContext:UIGraphicsGetCurrentCont ext()]; - UIImage*screenshot
=UIGraphicsGetImageFromCu rrentImageContext(); - UIGraphicsEndImageContex
t(); - from:http://www.cppblog.com/zhangyuntaoshe/articles/123066.html
合并两张bit图到一张image的方法
- To
graphically merge two images into a new image, you do something like this: - UIImage
*result = nil; - unsignedchar
*data = calloc(1,size.width*size.height*kBytesPerPixel); - if
(data != NULL) { - //
kCGImageAlphaPremultipli edLast 为预记录的#define value - //
设置context上下文 - CGContextRef
context = CGBitmapContextCreate( - data,
size.width, size.height, 8, size.width*kBytesPerPixel, - CGColorSpaceCreateDevice
RGB(), kCGImageAlphaPremultipli edLast); - if
(context != NULL) { - UIGraphicsPushContext(context);
- //
Image 为下载的背景图片,用于比较context - CGContextTranslateCTM(context,
0, size.height); - CGContextScaleCTM(context,
1, -1); - [image
drawInRect:imageRect]; - [image2
drawInRect:image2Rect]; - UIGraphicsPopContext();
- CGImageRef
imageRef = CGBitmapContextCreateIma ge(context); - if
(imageRef != NULL) { - result
= [UIImageimageWithCGImage:imageRef]; - CGImageRelease(imageRef);
- }
- CGContextRelease(context);
- }
- free(data);
- }
- return
result;
关键方法:
- CGContextRef
context = CGBitmapContextCreate(); - CGContextTranslateCTM();
- CGContextScaleCTM();
- CGImageRef
imageRef = CGBitmapContextCreateIma ge(context); - CGImageRelease(imageRef);
小结:iPhone图形开发绘图教程的内容介绍完了,希望本文对你有所帮助!