iOSの描画の座標について

178 views
Skip to first unread message

m_yukio

unread,
Feb 15, 2012, 6:23:15 PM2/15/12
to cocoa-d...@googlegroups.com
こんにちは、村上です。iOSの描画の座標について質問です。
Appleの文書から、UIKItとCoreAnimationは左上が原点で、Core Graphicsは左下が原点という話は知っていたのですが、これを試してみました。
UIViewのサブクラスを用意して、そこで、UIImageを座標(10.0, 10.0)に描画してみました。

- (void)drawRect:(CGRect)rect
{
    [self.upperLeftOriginImage drawAtPoint:CGPointMake(10.0, 10.0)];
}

左上に描画されました。
次に、Core Graphicsのパスを座標(10.0, 10.0)に描画してみました。

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 4.0);
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, 10.0, 30.0);
    CGContextAddLineToPoint(context, 10, 10);
    CGContextDrawPath(context, kCGPathStroke);
    CGContextMoveToPoint(context, 10.0, 10.0);
    CGContextAddLineToPoint(context, 30.0, 10.0);
    CGContextDrawPath(context, kCGPathStroke);
}

左下に描画される事を期待していたのですが、左上に描画されました。
何かを勘違いしているのでしょうか?

Yasushi MIYAZAKI

unread,
Feb 15, 2012, 7:02:54 PM2/15/12
to cocoa-d...@googlegroups.com
みやざきです。

Drawing and Printing Guide for iOS
- Graphics and Drawing in iOS
-- Default Coordinate Systems and Drawing in iOS

https://developer.apple.com/library/ios/documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GraphicsDrawingOverview/GraphicsDrawingOverview.html#//apple_ref/doc/uid/TP40010156-CH14-SW14

Drawing to the Screen の項にそれらしい記述があります。
===== 引用ここから =====
If you want to use Core Graphics functions to draw in a UIKit view,
you should use the ULO coordinate system of UIKit for drawing
operations. Alternatively, you can apply a flip transform to the CTM
and then draw an object in the UIKit view using Core Graphics native
LLO coordinate system.
===== 引用ここまで =====
※ULO; Upper-Left-Origin, LLO: Lower-Left-Origin

あと本題とは関係のない老婆心ですが、
> CGContextAddLineToPoint(context, 10, 10);

> CGContextAddLineToPoint(context, 10.0, 10.0);
にしておいたほうがよいかもしれません…(^^;

2012/2/16 m_yukio <yukio.m...@gmail.com>:

--
------
(み)

m_yukio

unread,
Feb 15, 2012, 7:59:18 PM2/15/12
to cocoa-d...@googlegroups.com
村上です。返信ありがとうございます。
動きを考えてみれば、drawRect:を呼び出す際に座標を左上に設定しているはずなので、当たり前の結果という事に気がつきました。
助かりました。

m_yukio

unread,
Feb 15, 2012, 8:24:00 PM2/15/12
to cocoa-d...@googlegroups.com
村上です。
整数と小数の件を忘れていました。
指摘、ありがとうございます。
Reply all
Reply to author
Forward
0 new messages