自我混乱

Pam*_*amy 1 core-graphics ios

在这段代码中

CGRect contextRect = self.bounds;

会参考哪个界限?rectangle,imageRect或整个iOS视图.

我试图使用quartz2D来操作图像,我通过查看不同的示例来创建此代码,并且在//之间编写的代码来自Apple Quartz2D指南绘制文本.

在此先感谢,问候.

    - (void)drawRect:(CGRect)rect
    {
        CGSize cgs = CGSizeMake(250.0, 400.0);
        UIGraphicsBeginImageContext(cgs);

        CGRect rectangle = CGRectMake(0,0,cgs.width,cgs.height);
        CGRect imageRect = CGRectInset(rectangle, 5.4, 5.4);
        imageRect.size.height -= 100;

        UIImage *myImage = [UIImage imageNamed:@"pumpkin.jpg"]; 
        [myImage drawInRect:imageRect];    

        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetLineWidth(context, 10.0);
        CGContextSetRGBStrokeColor(context, 0.0, 0.0, 1.0, 1.0);
        CGContextStrokeRect(context, rectangle);

        //    

        CGRect contextRect = self.bounds;

        CGContextTranslateCTM(context, 0, contextRect.size.height);
        CGContextScaleCTM(context, 1, -1);

        float w, h;
        w = contextRect.size.width;
        h = contextRect.size.height;

        CGAffineTransform myTextTransform;
        CGContextSelectFont (context, "Helvetica-Bold", h/10, kCGEncodingMacRoman);
        CGContextSetCharacterSpacing (context, 10);
        CGContextSetTextDrawingMode (context, kCGTextFillStroke);

        CGContextSetRGBFillColor(context, 0, 1, 0, .5);
        CGContextSetRGBStrokeColor(context, 0, 0, 1, 1);
        myTextTransform =  CGAffineTransformMakeRotation(0);
        CGContextSetTextMatrix(context, myTextTransform);
        CGContextShowTextAtPoint(context, 0, 50, "Quartz 2D", 9);

        //    

        UIImage *testImg =  UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        [testImg drawAtPoint:CGPointMake(35, 10)];
}
Run Code Online (Sandbox Code Playgroud)

sch*_*sch 7

self.frame 表示父视图坐标系中视图的坐标和大小.

self.bounds表示视图在其坐标系中的坐标和大小.因此,它总是具有相同width和height作为self.frame,但它x并y等于0.

self.bounds等于CGRectMake(0, 0, self.frame.size.width, self.frame.size.height).

所以你的代码:

CGRect contextRect = self.bounds;
Run Code Online (Sandbox Code Playgroud)

是相同的:

CGRect contextRect = rect;
Run Code Online (Sandbox Code Playgroud)