如何使用iPhone进行绘图、重绘操作
iPhone的绘图操作是在UIView类的drawRect方法中完成的,所以如果我们要想在一个UIView中绘图,需要写一个扩展UIView的类,并重写drawRect方法,在这里进行绘图操作,程序会自动调用此方法进行绘图。
下面先说明一下绘图,比如,你想绘制一个方块,你需要写一个类来扩展UIView并在drawRect方法中填入如下代码:
- (void)drawRect:(CGRect)rect {
}
再说明一下重绘,重绘操作仍然在drawRect方法中完成,但是苹果不建议直接调用drawRect方法,当然如果你强直直接调用此方法,当然是没有效果的。苹果要求我们调用UIView类中的setNeedsDisplay方法,则程序会自动调用drawRect方法进行重绘。(调用setNeedsDisplay会自动调用drawRect)
在UIView中,重写drawRect: (CGRect)aRect方法,可以自己定义想要画的图案.且此方法一般情况下只会画一次.也就是说这个drawRect方法一般情况下只会被掉用一次.
当某些情况下想要手动重画这个View,只需要掉用[self setNeedsDisplay]方法即可.
drawRect掉用是在Controller->loadView,Controller->viewDidLoad两方法之后掉用的.所以不用担心在控制器中,这些View的drawRect就开始画了.这样可以在控制器中设置一些值给View(如果这些Viewdraw的时候需要用到某些变量值).
1.如果在UIView初始化时没有设置rect大小,将直接导致drawRect不被自动调用。
2.该方法在调用sizeThatFits后被调用,所以可以先调用sizeToFit计算出size。然后系统自动调用drawRect:方法。
3.通过设置contentMode属性值为UIViewContentModeRedraw。那么将在每次设置或更改frame的时候自动调用drawRect:。
4.直接调用setNeedsDisplay,或者setNeedsDisplayInRect:触发drawRect:,但是有个前提条件是rect不能为0.
以上1,2推荐;而3,4不提倡
1、若使用UIView绘图,只能在drawRect:方法中获取相应的contextRef并绘图。如果在其他方法中获取将获取到一个invalidate的ref并且不能用于画图。drawRect:方法不能手动显示调用,必须通过调用setNeedsDisplay或者 setNeedsDisplayInRect ,让系统自动调该方法。
2、若使用calayer绘图,只能在drawInContext:中(类似鱼drawRect)绘制,或者在delegate中的相应方法绘制。同样也是调用setNeedDisplay等间接调用以上方法。
3、若要实时画图,不能使用gestureRecognizer,只能使用touchbegan等方法来掉用setNeedsDisplay实时刷新屏幕
转自:http://blog.sina.com.cn/s/blog_726dee1101015gs8.html
本文标签:iPhone 图形 绘图
iPhone图形开发绘图教程是本文要介绍的内容,介绍了很多关于绘图类的使用,先来看详细内容讲解 。
1、绘图总结:
绘图前设置:
CGContextSetRGBFillColor/CGContextSetFillColorWithColor //填充色 CGContextSetRGBStrokeColor/CGContextSetStrokeColorWithColor //笔颜色
CGContextSetLineWidth //线宽度
绘图后设置:
注: 画完图后,必须 先用CGContextStrokePath来描线,即形状,后用CGContextFillPath来填充形状内的颜色.
2.常见图形绘制:
CGContextFillRect/CGContextFillRects
CGContextFillEllipseInRect
CGContextAddRect/CGContextAddRects
CGContextAddEllipseInRect
CGContextAddLines
CGContextMoveToPoint
CGContextAddLineToPoint
3.常见控制方法:
CGContextSaveGState CGContextRestoreGState
4.创建内存图像context:
CGBitmapContextCreate <-----CGContextRlease释放 CGColorSpaceCreateWithName (KCGColorSpaceGenericRGB) CGColorSpaceRlease CGBitmapContextCreateImage() <-----CGImageRlease 释放.
eg:
CGContextRefMyCreateBitmapContext(intpixelsWide,intpixelsHigh)
{ CGContextRef context=NULL;
CGColorSpaceRefcolorSpace;
void* bitmapData;
int bitmapByteCount;
int bitmapBytesPerRow;
bitmapBytesPerRow =(pixelsWide*4);
bitmapByteCount =(bitmapBytesPerRow*pixelsHigh);
colorSpace=CGColorSpaceCreateDeviceRGB();
bitmapData=malloc(bitmapByteCount);
if(bitmapData==NULL)
{
fprintf(stderr,"Memorynotallocated!");
returnNULL;
}
context=CGBitmapContextCreate(bitmapData, pixelsWide, pixelsHigh, 8, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast);
if(context==NULL)
{
free(bitmapData);
fprintf(stderr,"Contextnotcreated!");
returnNULL;
}
CGColorSpaceRelease(colorSpace);
returncontext;
}
5.图形的变换:
CGContextTranslateCTM CGContextRotateCTM CGContextScaleCTM
6.常用函数:
CGRectContainsPoint(); CGRectContainsRect(); CGRectIntersectsRect(); CGRectIntersection(); CGPointEqualToPoint(); CGSizeEqualToSize();
7.从原图片中取小图.
CGImageCreateWithImageInRect
8.屏幕快照:
#import "QuartzCore/QuartzCore.h"
UIGraphicsBeginImageContext(yourView.frame.size);
[[yourView layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage*screenshot =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
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)
{ // kCGImageAlphaPremultipliedLast 为预记录的#define value // 设置context上下文
CGContextRef context = CGBitmapContextCreate( data, size.width, size.height, 8, size.width*kBytesPerPixel, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedLast);
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 = CGBitmapContextCreateImage(context);
if (imageRef != NULL)
{ result = [UIImageimageWithCGImage:imageRef];
CGImageRelease(imageRef);
}
CGContextRelease(context);
}
free(data);
} return result;
关键方法:
CGContextRef context = CGBitmapContextCreate();
CGContextTranslateCTM();
CGContextScaleCTM();
CGImageRef imageRef = CGBitmapContextCreateImage(context);
CGImageRelease(imageRef);
小结:iPhone图形开发绘图教程的内容介绍完