2013年9月21日 星期六

CALayer setMask建立遮罩

//會讓Mask圖的黑色部份被顯示,白色部份則會透明
        //取得透明度遮罩的影像與大小
        UIImage *maskImage = [UIImage imageNamed:@"alphaMask.png"];
        CGRect rect = CGRectMake(0.0, 0.0, maskImage.size.width, maskImage.size.height);
       
        //製作一個透明遮罩的Layer
        CALayer *maskLayer = [[CALayer layer]retain];
        maskLayer.bounds = rect;
        maskLayer.contents = (id)maskImage.CGImage;
       
        //將imageView中的影像做透明度的遮罩
        [[middleViewController.view layer] setMask:maskLayer];
       
        [maskLayer release];

2013年9月16日 星期一

故意製造閃退的方法

abort();

KVC機制

The NSKeyValueCoding informal protocol defines a mechanism by which you can access the properties of an object indirectly by name (or key), rather than directly through invocation of an accessor method or as instance variables. Thus, all of an object’s properties can be accessed in a consistent manner.
void changeName(Person *p, NSString *newName)
{
    // using the KVC accessor (getter) method
    NSString *originalName = [p valueForKey:@"name"];
    // using the KVC  accessor (setter) method.
    [p setValue:newName forKey:@"name"];
    NSLog(@"Changed %@'s name to: %@", originalName, newName);
}

2013年9月13日 星期五

為何objective c 要在.m檔中再@interface ViewControllor ()一次?是category嗎?

在XCode建立的UIViewController中,我們會在.m檔中看到
@interface ViewControllor ()
@end

在其中的宣告的內容,效果跟在.h中一樣,但是不會在.h中被其他人看到。
所以我們可以用來
  1. 修改.h中對property的設定,例如對外是宣告為readonly,在這再宣告回readwrite。
  2. 宣告primary method。

接下來是說明category,其實上面的class extention做法就可以看成是category的一個特例
下面可以看出其中的差別:在category中括號內部要放入你要作用的Class Name(上面提的沒有)
@interface NSObject(MySuperObject)
-(void) printRetainCount;
+(void) sayHello;
@end
 
category是讓你針對你繼承的class來新增你要的method,
或者是改寫其中某一個原來就有的method。 

手動建立UIViewController.xib的命名原則

如果取不同名稱,那要用initWithNibName:來初始化才會把它預設的view設為你的xib

如果.xib與.h.m取相同名稱,則可以只用init初始化,系統會主動把預設的view設為你的xib

如果.xib與.h.m取相同名稱,但在之後加上_iPhone或是_iPad(這裡注意大小寫要完全相同),系統會自動判斷裝置來把正確的xib設為預設的view

2013年9月12日 星期四

UIButton改text要用setTitle!

self.nameButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.nameButton addTarget:self
               action:@selector(nameButtonClicked:)
     forControlEvents:UIControlEventTouchUpInside];
[self.nameButton setTitle:@"Show Target Name" forState:UIControlStateNormal];
 self.nameButton.frame = CGRectMake(60.0, 20.0, 200.0, 80.0);
 [self.view addSubview:self.nameButton];

 //UIButton改text要用setTitle!
//而不是用self.nameButton.titleLabel.text,因為它是readonly

2013年9月11日 星期三

串接zxing 與 QCAR

一,在EAGLView中,加入下列function,目的是把qcarImage轉成uiimage
- (UIImage *)createUIImage:(const QCAR::Image *)qcarImage

{
   
    int width = qcarImage->getWidth();
   
    int height = qcarImage->getHeight();
   
    int bitsPerComponent = 8;
   
    int bitsPerPixel = QCAR::getBitsPerPixel(QCAR::RGB888);
   
    int bytesPerRow = qcarImage->getBufferWidth() * bitsPerPixel / bitsPerComponent;
   
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
   
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaNone;
   
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
   
   
   
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, qcarImage->getPixels(), QCAR::getBufferSize(width, height, QCAR::RGB888), NULL);
   
   
   
    CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
   
    UIImage *image = [[UIImage imageWithCGImage:imageRef] retain];
   
   
   
    CGDataProviderRelease(provider);
   
    CGColorSpaceRelease(colorSpaceRef);
   
    CGImageRelease(imageRef);
   
   
   
    return image;
   
}
二,在- (void)renderFrameQCAR中加入下列code
//ZXing
    ZxingQRcode* zxing = [ZxingQRcode shareInstance];
    QCAR::setFrameFormat(QCAR::RGB888, true);
    QCAR::Frame frame = state.getFrame();
    for (int i = 0; i < frame.getNumImages(); i++)
    {
        const QCAR::Image *qcarImage = frame.getImage(i);
        if (qcarImage->getFormat() == QCAR::RGB888)
        {
            UIImage *image = [self createUIImage:qcarImage];
            [zxing captureWithUIImage:image];
        }
    }

三,其中ZxingQRcode是我自己寫的一個class用來控制zxing做解碼之後的動作
這裡要注意的是,QCAR中和zxing中各有一個OverlayView,如果整合的時候不做處理,就會相衝突導致無法compile成功。
(Apple Mach-O Linker Error clang: error: linker command failed with exit code 1)


P.S.在第二項中setFrameFormat是為了用來取得RGB編碼的畫面,
關於畫面編碼QCAR提供了下面幾種選項
/// Pixel encoding types
enum PIXEL_FORMAT {
    UNKNOWN_FORMAT = 0,         ///< Unknown format - default pixel type for
                                ///< undefined images
    RGB565 = 1,                 ///< A color pixel stored in 2 bytes using 5
                                ///< bits for red, 6 bits for green and 5 bits
                                ///< for blue
    RGB888 = 2,                 ///< A color pixel stored in 3 bytes using
                                ///< 8 bits each
    GRAYSCALE = 4,              ///< A grayscale pixel stored in one byte
    YUV = 8,                    ///< A color pixel stored in 12 or more bits
                                ///< using Y, U and V planes
    RGBA8888 = 16,              ///< A color pixel stored in 32 bits using 8 bits
                                ///< each and an alpha channel.
};

2013年9月10日 星期二

如何利用zxing透過image來decode

摘自http://stackoverflow.com/questions/7882781/ios-how-to-decode-image-with-zxing
This was the code that finally solved my problem - thanks to the help from smparkes

HEADER FILE

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "ApplicationConfiguration.h"
#import <ZXingWidgetController.h> 

@interface ScanViewController : UIViewController<DecoderDelegate>
{
    UIButton *scanButton;
}

@property (nonatomic, retain) IBOutlet UIButton *scanButton;
@property (nonatomic, retain ) NSSet *readers;

- (IBAction)doScanAction;
- (void)decoder:(Decoder *)decoder didDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset withResult:(TwoDDecoderResult *)result;
- (void)decoder:(Decoder *)decoder failedToDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset reason:(NSString *)reason;

@end

IMPLEMENTATION FILE

#import "ScanViewController.h"
#import <ZXingWidgetController.h> 
#import <QRCodeReader.h> 
#import "TwoDDecoderResult.h"

@implementation ScanViewController

@synthesize scanButton;
@synthesize readers;

-(IBAction)doScanAction{
    QRCodeReader* qrcodeReader = [[QRCodeReader alloc] init];
    self.readers = [[NSSet alloc ] initWithObjects:qrcodeReader,nil];
    [qrcodeReader release];

    Decoder *d = [[Decoder alloc] init];
    [d setDelegate:self];
    [d setReaders:self.readers];
    [readers retain];

    BOOL decodeSuccess= [d decodeImage:[UIImage imageNamed:@"QRcode.png"]];
    NSLog(@"BOOL = %@\n", (decodeSuccess ? @"YES" : @"NO"));
}

- (void)decoder:(Decoder *)decoder didDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset withResult:(TwoDDecoderResult *)result{
    [result retain];
    NSLog(@"Did Decode Image Result: %d",[result text]);
    [result release];
}

- (void)decoder:(Decoder *)decoder failedToDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset reason:(NSString *)reason;
{
    [reason retain];
    NSLog(@"Failed Decode Image Result: %d",reason);
    [reason release];
}

@end

如何把OpenGL ES的view做snapshot再轉成UIImage?

摘自https://developer.apple.com/library/ios/qa/qa1704/_index.html

Q:  How do I take a snapshot of my OpenGL ES view and save the result in a UIImage?

// IMPORTANT: Call this method after you draw and before -presentRenderbuffer:.

- (UIImage*)snapshot:(UIView*)eaglview

{

    GLint backingWidth, backingHeight;



    // Bind the color renderbuffer used to render the OpenGL ES view

    // If your application only creates a single color renderbuffer which is already bound at this point,

    // this call is redundant, but it is needed if you're dealing with multiple renderbuffers.

    // Note, replace "_colorRenderbuffer" with the actual name of the renderbuffer object defined in your class.

    glBindRenderbufferOES(GL_RENDERBUFFER_OES, _colorRenderbuffer);



    // Get the size of the backing CAEAGLLayer

    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);

    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);



    NSInteger x = 0, y = 0, width = backingWidth, height = backingHeight;

    NSInteger dataLength = width * height * 4;

    GLubyte *data = (GLubyte*)malloc(dataLength * sizeof(GLubyte));



    // Read pixel data from the framebuffer

    glPixelStorei(GL_PACK_ALIGNMENT, 4);

    glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);



    // Create a CGImage with the pixel data

    // If your OpenGL ES content is opaque, use kCGImageAlphaNoneSkipLast to ignore the alpha channel

    // otherwise, use kCGImageAlphaPremultipliedLast

    CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, data, dataLength, NULL);

    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();

    CGImageRef iref = CGImageCreate(width, height, 8, 32, width * 4, colorspace, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast,

                                    ref, NULL, true, kCGRenderingIntentDefault);



    // OpenGL ES measures data in PIXELS

    // Create a graphics context with the target size measured in POINTS

    NSInteger widthInPoints, heightInPoints;

    if (NULL != UIGraphicsBeginImageContextWithOptions) {

        // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration

        // Set the scale parameter to your OpenGL ES view's contentScaleFactor

        // so that you get a high-resolution snapshot when its value is greater than 1.0

        CGFloat scale = eaglview.contentScaleFactor;

        widthInPoints = width / scale;

        heightInPoints = height / scale;

        UIGraphicsBeginImageContextWithOptions(CGSizeMake(widthInPoints, heightInPoints), NO, scale);

    }

    else {

        // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext

        widthInPoints = width;

        heightInPoints = height;

        UIGraphicsBeginImageContext(CGSizeMake(widthInPoints, heightInPoints));

    }



    CGContextRef cgcontext = UIGraphicsGetCurrentContext();



    // UIKit coordinate system is upside down to GL/Quartz coordinate system

    // Flip the CGImage by rendering it to the flipped bitmap context

    // The size of the destination area is measured in POINTS

    CGContextSetBlendMode(cgcontext, kCGBlendModeCopy);

    CGContextDrawImage(cgcontext, CGRectMake(0.0, 0.0, widthInPoints, heightInPoints), iref);



    // Retrieve the UIImage from the current context

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();



    UIGraphicsEndImageContext();



    // Clean up

    free(data);

    CFRelease(ref);

    CFRelease(colorspace);

    CGImageRelease(iref);



    return image;

}

2013年9月6日 星期五

在NSDictionary中讀取和寫入CGRect

//讀取
CGRect newFrame = [[change objectForKey:@"new"]CGRectValue];

//寫入
dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSValue valueWithCGRect:CGRectMake(100,100,200,200)],
    @"Crocodile",
    nil];

2013年9月5日 星期四

objective-C中列舉(enum)的使用

第一種是原始的用法
.h檔
#import <Foundation/Foundation.h>
enum HHSlidaleViewModes{
    slideFromLeftToRight = 0,
    slideFromRightToLeft,
    rightSideFromCenter,
    leftSideFromCenter
};
@interface SlidableView : NSObject{
    enum HHSlidaleViewModes a;
}
.m檔
#import "SlidableView.h"
@implementation SlidableView
-(void)a{
    a = slideFromLeftToRight;
}
@end


第二種是通用的用法

首先,我們要先知道typedef是把型別名稱取一個別名
例如typedef int handsomeInt;
之後就可以宣告整數為 handsomeInt a = 100;

之後,我們就可以把上面的code改為:
.h檔
#import <Foundation/Foundation.h>
typedef enum HHSlidaleViewModes{
    slideFromLeftToRight = 0,
    slideFromRightToLeft,
    rightSideFromCenter,
    leftSideFromCenter
} hhSlidableViewModes;
@interface SlidableView : NSObject{
    hhSlidaleViewModes a;
}
.m檔
#import "SlidableView.h"
@implementation SlidableView
-(void)a{
    a = slideFromLeftToRight;
}
@end



//我自己的用法,利用typedef把後面的enum{running = 0,sleeping,header}改名為StepMode來使用
typedef enum{
    running = 0,
    sleeping,
    header

}StepMode;

2013年9月4日 星期三

@selector傳遞參數

//呼叫不用參數的method
[self performSelector:@selector(fooNoInputs)];
 
//呼叫一個參數的method
[self performSelector:@selector(fooOneInput:) withObject:@"first"];
 
//呼叫二個參數的method
[self performSelector:@selector(fooFirstInput:secondInput:) withObject:@"first" withObject:@"second"];

刮刮樂的可刮部份

//
//  ScratchableView.h
//  CGScratch
//
//  Created by Olivier Yiptong on 11-01-11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>


@interface ScratchableView : UIView {
    float width;
    float height;
    CGPoint    location;
    CGPoint    previousLocation;
    BOOL firstTouch;
    CGImageRef scratchable;
    CGImageRef scratched;
    CGContextRef alphaPixels;
    CGDataProviderRef provider;
}

- (void) renderLineFromPoint:(CGPoint)start toPoint:(CGPoint)end;

@end

//
//  ScratchableView.m
//  CGScratch
//
//  Created by Olivier Yiptong on 11-01-11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ScratchableView.h"


@implementation ScratchableView


- (id)initWithFrame:(CGRect)frame {
   
    self = [super initWithFrame:frame];
    if (self) {
        //self.bounds = CGRectMake(0.0, 0.0, frame.size.width, frame.size.height);
       
        //自行把用來刮的scratch.png放入專案
        scratchable = [UIImage imageNamed:@"scratch.png"].CGImage;
//修改成隨bounds大小變化 by Harvey 20130903
//          width = CGImageGetWidth(scratchable);
//          height = CGImageGetHeight(scratchable);
        width = self.bounds.size.width;
        height = self.bounds.size.height;
        self.opaque = NO;
        CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
       
        CFMutableDataRef pixels = CFDataCreateMutable( NULL , width * height );
        alphaPixels = CGBitmapContextCreate( CFDataGetMutableBytePtr( pixels ) , width , height , 8 , width , colorspace , kCGImageAlphaNone );
        provider = CGDataProviderCreateWithCFData(pixels);
       
       
        CGContextSetFillColorWithColor(alphaPixels, [UIColor blackColor].CGColor);
        CGContextFillRect(alphaPixels, frame);
       
        CGContextSetStrokeColorWithColor(alphaPixels, [UIColor whiteColor].CGColor);
        CGContextSetLineWidth(alphaPixels, 20.0);
        CGContextSetLineCap(alphaPixels, kCGLineCapRound);
       
        //CGImageRef mask = CGImageMaskCreate(width, height, 8, 8, width, provider, nil, NO);
        CGImageRef mask = CGImageMaskCreate(width, height, 8, 8, width, provider, nil, NO);
        scratched = CGImageCreateWithMask(scratchable, mask);
       
        CGImageRelease(mask);
        CGColorSpaceRelease(colorspace);
       

    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    CGContextDrawImage(UIGraphicsGetCurrentContext() , [self bounds] , scratched);
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event touchesForView:self] anyObject];
    firstTouch = YES;
    location = [touch locationInView:self];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event touchesForView:self] anyObject];
   
    if (firstTouch) {
        firstTouch = NO;
        previousLocation = [touch previousLocationInView:self];
    } else {
        location = [touch locationInView:self];
        previousLocation = [touch previousLocationInView:self];
    }
   
    // Render the stroke
    [self renderLineFromPoint:previousLocation toPoint:location];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event touchesForView:self] anyObject];
    if (firstTouch) {
        firstTouch = NO;
        previousLocation = [touch previousLocationInView:self];
       
        [self renderLineFromPoint:previousLocation toPoint:location];
    }
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
}

- (void) renderLineFromPoint:(CGPoint)start toPoint:(CGPoint)end {
   
    CGContextMoveToPoint(alphaPixels, start.x , start.y );
    CGContextAddLineToPoint(alphaPixels, end.x , end.y );
    CGContextStrokePath(alphaPixels);
    [self setNeedsDisplay];
}

- (void)dealloc {
    CGContextRelease(alphaPixels);
    CGImageRelease(scratchable);
    CGDataProviderRelease(provider);
    [super dealloc];
}


@end

NSZombieEnabled on Xcode 4.0(and up)


NSZombieEnabled只能在develop時使用,千萬別忘了在distrube時移掉,因為NSZombieEnabled不會真正釋放dealloc時的記憶體!

NSZombieEnabled

2013年9月2日 星期一

使用drawRect繪圖

引用自http://blog.csdn.net/zhibudefeng/article/details/8463268
  1. // Only override drawRect: if you perform custom drawing.  
  2. // An empty implementation adversely affects performance during animation.  
  3. - (void)drawRect:(CGRect)rect  
  4. {  
  5.     CGContextRef context = UIGraphicsGetCurrentContext();  
  6.        
  7.    
  8.        
  9.     /*NO.1画一条线 
  10.        
  11.      CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);//线条颜色 
  12.      CGContextMoveToPoint(context, 20, 20); 
  13.      CGContextAddLineToPoint(context, 200,20); 
  14.      CGContextStrokePath(context); 
  15.     */  
  16.    
  17.        
  18.        
  19.     /*NO.2写文字 
  20.        
  21.     CGContextSetLineWidth(context, 1.0); 
  22.     CGContextSetRGBFillColor (context, 0.5, 0.5, 0.5, 0.5); 
  23.     UIFont  *font = [UIFont boldSystemFontOfSize:18.0]; 
  24.     [@"公司:北京中软科技股份有限公司\n部门:ERP事业部\n姓名:McLiang" drawInRect:CGRectMake(20, 40, 280, 300) withFont:font]; 
  25.     */  
  26.    
  27.        
  28.     /*NO.3画一个正方形图形 没有边框 
  29.   
  30.     CGContextSetRGBFillColor(context, 0, 0.25, 0, 0.5); 
  31.     CGContextFillRect(context, CGRectMake(2, 2, 270, 270)); 
  32.     CGContextStrokePath(context); 
  33.     */  
  34.     
  35.        
  36.     /*NO.4画正方形边框 
  37.       
  38.     CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);//线条颜色 
  39.     CGContextSetLineWidth(context, 2.0); 
  40.     CGContextAddRect(context, CGRectMake(2, 2, 270, 270)); 
  41.     CGContextStrokePath(context); 
  42.     */  
  43.    
  44.        
  45.     /*NO.5画方形背景颜色 
  46.        
  47.     CGContextTranslateCTM(context, 0.0f, self.bounds.size.height); 
  48.     CGContextScaleCTM(context, 1.0f, -1.0f); 
  49.     UIGraphicsPushContext(context); 
  50.     CGContextSetLineWidth(context,320); 
  51.     CGContextSetRGBStrokeColor(context, 250.0/255, 250.0/255, 210.0/255, 1.0); 
  52.     CGContextStrokeRect(context, CGRectMake(0, 0, 320, 460)); 
  53.     UIGraphicsPopContext(); 
  54.     */  
  55.    
  56.     /*NO.6椭圆 
  57.        
  58.      CGRect aRect= CGRectMake(80, 80, 160, 100); 
  59.      CGContextSetRGBStrokeColor(context, 0.6, 0.9, 0, 1.0); 
  60.      CGContextSetLineWidth(context, 3.0); 
  61.      CGContextAddEllipseInRect(context, aRect); //椭圆 
  62.      CGContextDrawPath(context, kCGPathStroke); 
  63.     */  
  64.    
  65.     /*NO.7 
  66.     CGContextBeginPath(context); 
  67.     CGContextSetRGBStrokeColor(context, 0, 0, 1, 1); 
  68.     CGContextMoveToPoint(context, 100, 100); 
  69.     CGContextAddArcToPoint(context, 50, 100, 50, 150, 50); 
  70.     CGContextStrokePath(context); 
  71.     */  
  72.    
  73.     /*NO.8渐变 
  74.     CGContextClip(context); 
  75.     CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB(); 
  76.     CGFloat colors[] = 
  77.     { 
  78.         204.0 / 255.0, 224.0 / 255.0, 244.0 / 255.0, 1.00, 
  79.         29.0 / 255.0, 156.0 / 255.0, 215.0 / 255.0, 1.00, 
  80.         0.0 / 255.0,  50.0 / 255.0, 126.0 / 255.0, 1.00, 
  81.     }; 
  82.     CGGradientRef gradient = CGGradientCreateWithColorComponents 
  83.     (rgb, colors, NULL, sizeof(colors)/(sizeof(colors[0])*4)); 
  84.     CGColorSpaceRelease(rgb); 
  85.     CGContextDrawLinearGradient(context, gradient,CGPointMake 
  86.                                 (0.0,0.0) ,CGPointMake(0.0,self.frame.size.height), 
  87.                                 kCGGradientDrawsBeforeStartLocation); 
  88.      */  
  89.        
  90.       
  91.     /* NO.9四条线画一个正方形 
  92.     //画线 
  93.         UIColor *aColor = [UIColor colorWithRed:0 green:1.0 blue:0 alpha:0]; 
  94.     CGContextSetRGBStrokeColor(context, 1.0, 0, 0, 1.0); 
  95.        CGContextSetFillColorWithColor(context, aColor.CGColor); 
  96.     CGContextSetLineWidth(context, 4.0); 
  97.     CGPoint aPoints[5]; 
  98.     aPoints[0] =CGPointMake(60, 60); 
  99.     aPoints[1] =CGPointMake(260, 60); 
  100.     aPoints[2] =CGPointMake(260, 300); 
  101.     aPoints[3] =CGPointMake(60, 300); 
  102.     aPoints[4] =CGPointMake(60, 60); 
  103.     CGContextAddLines(context, aPoints, 5); 
  104.     CGContextDrawPath(context, kCGPathStroke); //开始画线 
  105.      */  
  106.        
  107.        
  108.        
  109.     /*  NO.10 
  110.     UIColor *aColor = [UIColor colorWithRed:0 green:1.0 blue:0 alpha:0]; 
  111.     CGContextSetRGBStrokeColor(context, 1.0, 0, 0, 1.0); 
  112.     CGContextSetFillColorWithColor(context, aColor.CGColor); 
  113.     //椭圆 
  114.     CGRect aRect= CGRectMake(80, 80, 160, 100); 
  115.     CGContextSetRGBStrokeColor(context, 0.6, 0.9, 0, 1.0); 
  116.     CGContextSetLineWidth(context, 3.0); 
  117.       CGContextSetFillColorWithColor(context, aColor.CGColor); 
  118.        CGContextAddRect(context, rect); //矩形 
  119.     CGContextAddEllipseInRect(context, aRect); //椭圆 
  120.     CGContextDrawPath(context, kCGPathStroke); 
  121.      */  
  122.    
  123.        
  124.        
  125.     /*  NO.11 
  126.      画一个实心的圆 
  127.    
  128.      CGContextFillEllipseInRect(context, CGRectMake(95, 95, 100.0, 100)); 
  129.     */  
  130.        
  131.        
  132.        
  133.     /*NO.12 
  134.      画一个菱形 
  135.     CGContextSetLineWidth(context, 2.0); 
  136.     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
  137.     CGContextMoveToPoint(context, 100, 100); 
  138.     CGContextAddLineToPoint(context, 150, 150); 
  139.     CGContextAddLineToPoint(context, 100, 200); 
  140.     CGContextAddLineToPoint(context, 50, 150); 
  141.     CGContextAddLineToPoint(context, 100, 100); 
  142.     CGContextStrokePath(context); 
  143.      */  
  144.    
  145.     /*NO.13 画矩形 
  146.     CGContextSetLineWidth(context, 2.0); 
  147.   
  148.     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
  149.   
  150.     CGRect rectangle = CGRectMake(60,170,200,80); 
  151.   
  152.     CGContextAddRect(context, rectangle); 
  153.       
  154.     CGContextStrokePath(context); 
  155.      */  
  156.        
  157.       
  158.     /*椭圆 
  159.     CGContextSetLineWidth(context, 2.0); 
  160.   
  161.     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
  162.   
  163.     CGRect rectangle = CGRectMake(60,170,200,80); 
  164.   
  165.     CGContextAddEllipseInRect(context, rectangle); 
  166.       
  167.     CGContextStrokePath(context); 
  168.      */  
  169.        
  170.     /*用红色填充了一段路径: 
  171.       
  172.     CGContextMoveToPoint(context, 100, 100); 
  173.     CGContextAddLineToPoint(context, 150, 150); 
  174.     CGContextAddLineToPoint(context, 100, 200); 
  175.     CGContextAddLineToPoint(context, 50, 150); 
  176.     CGContextAddLineToPoint(context, 100, 100); 
  177.     CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 
  178.     CGContextFillPath(context); 
  179.     */  
  180.        
  181.     /*填充一个蓝色边的红色矩形 
  182.     CGContextSetLineWidth(context, 2.0); 
  183.     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
  184.     CGRect rectangle = CGRectMake(60,170,200,80); 
  185.     CGContextAddRect(context, rectangle); 
  186.     CGContextStrokePath(context); 
  187.     CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 
  188.     CGContextFillRect(context, rectangle); 
  189.     */  
  190.        
  191.     /*画弧 
  192.      //弧线的是通过指定两个切点,还有角度,调用CGContextAddArcToPoint()绘制 
  193.     CGContextSetLineWidth(context, 2.0); 
  194.     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
  195.     CGContextMoveToPoint(context, 100, 100); 
  196.     CGContextAddArcToPoint(context, 100,200, 300,200, 100); 
  197.     CGContextStrokePath(context); 
  198.     */  
  199.       
  200.        
  201.     /* 
  202.     绘制贝兹曲线 
  203.     //贝兹曲线是通过移动一个起始点,然后通过两个控制点,还有一个中止点,调用CGContextAddCurveToPoint() 函数绘制 
  204.     CGContextSetLineWidth(context, 2.0); 
  205.   
  206.     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
  207.   
  208.     CGContextMoveToPoint(context, 10, 10); 
  209.   
  210.     CGContextAddCurveToPoint(context, 0, 50, 300, 250, 300, 400); 
  211.       
  212.     CGContextStrokePath(context); 
  213.      */  
  214.        
  215.     /*绘制二次贝兹曲线 
  216.       
  217.       CGContextSetLineWidth(context, 2.0); 
  218.   
  219.       CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
  220.   
  221.       CGContextMoveToPoint(context, 10, 200); 
  222.   
  223.       CGContextAddQuadCurveToPoint(context, 150, 10, 300, 200); 
  224.       
  225.       CGContextStrokePath(context); 
  226.      */  
  227.        
  228.     /*绘制虚线 
  229.     CGContextSetLineWidth(context, 5.0); 
  230.   
  231.     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
  232.   
  233.     CGFloat dashArray[] = {2,6,4,2}; 
  234.   
  235.     CGContextSetLineDash(context, 3, dashArray, 4);//跳过3个再画虚线,所以刚开始有6-(3-2)=5个虚点 
  236.       
  237.     CGContextMoveToPoint(context, 10, 200); 
  238.       
  239.     CGContextAddQuadCurveToPoint(context, 150, 10, 300, 200); 
  240.       
  241.     CGContextStrokePath(context); 
  242.     */  
  243. /*绘制图片 
  244.     NSString* imagePath = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"]; 
  245.     UIImage* myImageObj = [[UIImage alloc] initWithContentsOfFile:imagePath]; 
  246.     //[myImageObj drawAtPoint:CGPointMake(0, 0)]; 
  247.     [myImageObj drawInRect:CGRectMake(0, 0, 320, 480)]; 
  248.   
  249.     NSString *s = @"我的小狗"; 
  250.   
  251.     [s drawAtPoint:CGPointMake(100, 0) withFont:[UIFont systemFontOfSize:34.0]]; 
  252. */  
  253.        
  254.   /* 
  255.     NSString *path = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"]; 
  256.     UIImage *img = [UIImage imageWithContentsOfFile:path]; 
  257.     CGImageRef image = img.CGImage; 
  258.     CGContextSaveGState(context); 
  259.     CGRect touchRect = CGRectMake(0, 0, img.size.width, img.size.height); 
  260.     CGContextDrawImage(context, touchRect, image); 
  261.     CGContextRestoreGState(context); 
  262.    */  
  263.      
  264.        
  265.     /*NSString *path = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"]; 
  266.     UIImage *img = [UIImage imageWithContentsOfFile:path]; 
  267.     CGImageRef image = img.CGImage; 
  268.     CGContextSaveGState(context); 
  269.   
  270.     CGContextRotateCTM(context, M_PI); 
  271.     CGContextTranslateCTM(context, -img.size.width, -img.size.height); 
  272.   
  273.     CGRect touchRect = CGRectMake(0, 0, img.size.width, img.size.height); 
  274.     CGContextDrawImage(context, touchRect, image); 
  275.     CGContextRestoreGState(context);*/  
  276.    
  277. /* 
  278.     NSString *path = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"]; 
  279.     UIImage *img = [UIImage imageWithContentsOfFile:path]; 
  280.     CGImageRef image = img.CGImage; 
  281.       
  282.     CGContextSaveGState(context); 
  283.   
  284.     CGAffineTransform myAffine = CGAffineTransformMakeRotation(M_PI); 
  285.     myAffine = CGAffineTransformTranslate(myAffine, -img.size.width, -img.size.height); 
  286.     CGContextConcatCTM(context, myAffine); 
  287.   
  288.     CGContextRotateCTM(context, M_PI); 
  289.     CGContextTranslateCTM(context, -img.size.width, -img.size.height); 
  290.   
  291.     CGRect touchRect = CGRectMake(0, 0, img.size.width, img.size.height); 
  292.     CGContextDrawImage(context, touchRect, image); 
  293.     CGContextRestoreGState(context); 
  294. */