iPhone图形开发绘图小结

http://wsqwsq000.iteye.com/blog/1317018

高亮uiimage

@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、绘图总结:

绘图前设置:

 
 
  1. CGContextSetRGBFillColor/CGContextSetFillColorWithColor  //填充色   
  2. CGContextSetRGBStrokeColor/CGContextSetStrokeColorWithColor //笔颜色   
  3. CGContextSetLineWidth   //线宽度  

绘图后设置:

注:  画完图后,必须 先用CGContextStrokePath来描线,即形状,后用CGContextFillPath来填充形状内的颜色.

2.常见图形绘制:

 
 
  1. CGContextFillRect/CGContextFillRects   
  2. CGContextFillEllipseInRect   
  3. CGContextAddRect/CGContextAddRects   
  4. CGContextAddEllipseInRect   
  5. CGContextAddLines   
  6. CGContextMoveToPoint   
  7. CGContextAddLineToPoint  

3.常见控制方法:

 
 
  1. CGContextSaveGState   
  2. CGContextRestoreGState  

4.创建内存图像context:

 
 
  1. CGBitmapContextCreate       <-----CGContextRlease释放   
  2. CGColorSpaceCreateWithName    (KCGColorSpaceGenericRGB)   
  3. CGColorSpaceRlease   
  4. CGBitmapContextCreateImage()   <-----CGImageRlease 释放.   
  5. eg:   
  6. CGContextRefMyCreateBitmapContext(intpixelsWide,intpixelsHigh)   
  7. {   
  8. CGContextRef    context=NULL;   
  9. CGColorSpaceRefcolorSpace;   
  10. void*          bitmapData;   
  11. int             bitmapByteCount;   
  12. int             bitmapBytesPerRow;   
  13. bitmapBytesPerRow   =(pixelsWide*4);   
  14. bitmapByteCount     =(bitmapBytesPerRow*pixelsHigh);   
  15. colorSpace=CGColorSpaceCreateDeviceRGB();   
  16. bitmapData=malloc(bitmapByteCount);   
  17. if(bitmapData==NULL)   
  18. {   
  19. fprintf(stderr,"Memorynotallocated!");   
  20. returnNULL;   
  21. }   
  22. context=CGBitmapContextCreate(bitmapData,   
  23.  pixelsWide,    pixelsHigh,    8,    
  24. bitmapBytesPerRow,    colorSpace,   
  25.  kCGImageAlphaPremultipliedLast);   
  26. if(context==NULL)   
  27. {   
  28. free(bitmapData);   
  29. fprintf(stderr,"Contextnotcreated!");   
  30. returnNULL;   
  31. }   
  32. CGColorSpaceRelease(colorSpace);   
  33. returncontext;   
  34. }  

5.图形的变换:

 
 
  1. CGContextTranslateCTM   
  2. CGContextRotateCTM   
  3. CGContextScaleCTM  

6.常用函数:

 
 
  1.   CGRectContainsPoint();   
  2. CGRectContainsRect();   
  3. CGRectIntersectsRect();   
  4. CGRectIntersection();   
  5. CGPointEqualToPoint();   
  6. CGSizeEqualToSize();  

7.从原图片中取小图.

 
 
  1. CGImageCreateWithImageInRect  

8.屏幕快照:

 
 
  1. #import "QuartzCore/QuartzCore.h"   
  2.  
  3. UIGraphicsBeginImageContext(yourView.frame.size);   
  4. [[yourView layer] renderInContext:UIGraphicsGetCurrentContext()];   
  5. UIImage*screenshot =UIGraphicsGetImageFromCurrentImageContext();   
  6. UIGraphicsEndImageContext();   
  7. from:http://www.cppblog.com/zhangyuntaoshe/articles/123066.html  

合并两张bit图到一张image的方法

 
 
  1. To graphically merge two images into a new image, you do something like this:   
  2. UIImage *result = nil;   
  3. unsignedchar *data = calloc(1,size.width*size.height*kBytesPerPixel);   
  4. if (data != NULL) {   
  5. // kCGImageAlphaPremultipliedLast 为预记录的#define value   
  6. // 设置context上下文   
  7. CGContextRef context = CGBitmapContextCreate(   
  8. data, size.width, size.height, 8, size.width*kBytesPerPixel,   
  9. CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedLast);   
  10. if (context != NULL) {   
  11. UIGraphicsPushContext(context);   
  12. //  Image 为下载的背景图片,用于比较context   
  13. CGContextTranslateCTM(context, 0, size.height);   
  14. CGContextScaleCTM(context, 1, -1);   
  15. [image drawInRect:imageRect];   
  16. [image2 drawInRect:image2Rect];   
  17. UIGraphicsPopContext();   
  18. CGImageRef imageRef = CGBitmapContextCreateImage(context);   
  19. if (imageRef != NULL) {   
  20. result = [UIImageimageWithCGImage:imageRef];   
  21. CGImageRelease(imageRef);   
  22. }   
  23. CGContextRelease(context);   
  24. }   
  25. free(data);   
  26. }   
  27. return result;  

关键方法: 

 
 
  1. CGContextRef context = CGBitmapContextCreate();   
  2. CGContextTranslateCTM();   
  3. CGContextScaleCTM();   
  4. CGImageRef imageRef = CGBitmapContextCreateImage(context);   
  5. 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- ( 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 UIImage

This 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:

1
UIImageWriteToSavedPhotosAlbum ( 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 ] ;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值