描画スクリーンサイズは480×640で設定している。
以下の例では原点は左上になる。
UIGraphicsBeginImageContext(CGSizeMake(480, 640)); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0); CGContextSetLineWidth(context, 3); CGContextMoveToPoint(context, 0, 0); CGContextAddLineToPoint(context, 100, 100); CGContextStrokePath(context); CGImageRef imageRef = CGBitmapContextCreateImage(context); self.view.layer.contents = (id)imageRef; CGImageRelease(imageRef); UIGraphicsEndImageContext(); |
以下の例では原点は左下になる。
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(NULL, 480, 640, 8, 0, colorSpace, kCGImageAlphaPremultipliedFirst); CGColorSpaceRelease(colorSpace); CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0); CGContextSetLineWidth(context, 3); CGContextMoveToPoint(context, 0, 0); CGContextAddLineToPoint(context, 100, 100); CGContextStrokePath(context); CGImageRef imageRef = CGBitmapContextCreateImage(context); self.view.layer.contents = (id)imageRef; CGImageRelease(imageRef); CGContextRelease(context); |
原点の移動は以下のように行う。これにより原点を左下から左上に変更できる。
CGContextTranslateCTM(context, 0, 640); CGContextScaleCTM(context, 1.0, -1.0); |