2013年9月10日 星期二

如何把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. */