1

In this code

CGRect contextRect = self.bounds;

will refer which bound? rectangle, imageRect, or whole iOS view.

I am trying to manipulate image using quartz2D, i created this code by looking different examples, and code written between // is from Apple Quartz2D guide draw text.

Thanks in advance, Regards.

    - (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)];
}
Pamy
  • 193
  • 2
  • 13
  • You could always refer to the iOS reference: https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-SW1 – Evan Mulawski Feb 27 '12 at 13:26
  • 1
    Have you checked this? http://stackoverflow.com/questions/1210047/iphone-development-whats-the-difference-between-the-frame-and-the-bounds (Close as dupe?) – Scott Corscadden Feb 27 '12 at 13:33
  • thanks for the reply, I already know the difference between frame and bound but I was confused regarding my code. – Pamy Feb 27 '12 at 14:06

1 Answers1

7

self.frame indicates the coords and size of the view in its parent view coordinate system.

self.bounds indicates the coords and size of the view in its own coordinate system. So it always has the same width and height as self.frame, but it has x and y equal to 0.

self.bounds is equal to CGRectMake(0, 0, self.frame.size.width, self.frame.size.height).

So your code:

CGRect contextRect = self.bounds;

is the same as:

CGRect contextRect = rect;
sch
  • 27,436
  • 3
  • 68
  • 83