您的位置:首页 > 其它

iPhone图形开发绘图小结

2012-02-22 09:05 453 查看
http://wsqwsq000.iteye.com/blog/1317018

高亮uiimage

[code]@interface UIImage (Tint)

- (UIImage *)tintedImageUsingColor:(UIColor *)tintColor;

@end

@implementation UIImage (Tint)

- (UIImage *)tintedImageUsingColor:(UIColor *)tintColor {
  UIGraphicsBeginImageContext(self.size);
  CGRect drawRect = CGRectMake(0, 0, self.size.width, self.size.height);
  [self drawInRect:drawRect];
  [tintColor set];
  UIRectFillUsingBlendMode(drawRect, kCGBlendModeSourceAtop);
  UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  return tintedImage;
}

@end


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图形开发绘图教程的内容介绍完了,希望本文对你有所帮助!
http://blog.itotem.com.cn/?p=47&replytocom=5
Combine two UIImages

To add two UIImages together you need to make use of Graphics Context.

123456789101112131415- (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2 {
UIGraphicsBeginImageContext(image1.size);

// Draw image1
[image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];

// Draw image2
[image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];

UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return resultingImage;
}
Create a UIImage from a part of another UIImageThis requires a round-trip to Core Graphics land:

1

2

3

4

5

6

-
(UIImage
*)imageFromImage:(UIImage
*)image
inRect:(CGRect)rect
{

CGImageRef
sourceImageRef
=
[image
CGImage];

CGImageRef
newImageRef
=
CGImageCreateWithImageInRect(sourceImageRef,
rect);

UIImage
*newImage
=
[UIImage
imageWithCGImage:newImageRef];

return
newImage;

}

Save UIImage to Photo Album

This is just a one-liner:

1UIImageWriteToSavedPhotosAlbum(image, self, @selector(imageSavedToPhotosAlbum: didFinishSavingWithError: contextInfo:), context);
And to know if the save was successful:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

-
(void)imageSavedToPhotosAlbum:(UIImage
*)image
didFinishSavingWithError:(NSError
*)error
contextInfo:(void
*)contextInfo
{

NSString
*message;

NSString
*title;

if
(!error)
{

title
=
NSLocalizedString(@"SaveSuccessTitle",
@"");

message
=
NSLocalizedString(@"SaveSuccessMessage",
@"");

}
else
{

title
=
NSLocalizedString(@"SaveFailedTitle",
@"");

message
=
[error
description];

}

UIAlertView
*alert
=
[[UIAlertView
alloc]
initWithTitle:title

message:message

delegate:nil

cancelButtonTitle:NSLocalizedString(@"ButtonOK",
@"")

otherButtonTitles:nil];

[alert
show];

[alert
release];

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: