0
votes

I am really strugggling with Quartz2D for more then 10 days please help me understand few concepts I will be really grateful, please look at this code and screenshot url.

This code draw image with border and write text to it and the image become whole new image with border and text.

//part 1 
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:@"BMW.jpg"]; 
[myImage drawInRect:imageRect];    

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



//part 2

 1. CGRect contextRect = rectangle;
 2. CGContextTranslateCTM(context, 0, contextRect.size.height);
 3. CGContextScaleCTM(context, 1, -1);
 4. float w, h;
 5. w = contextRect.size.width;
 6. h = contextRect.size.height;
 7. CGContextSelectFont (context, "Helvetica-Bold", 25,
    kCGEncodingMacRoman);
 8. CGContextSetCharacterSpacing (context, 5);
 9. CGContextSetRGBFillColor(context, 0.0, 1.0, 1.0, 1.0);
 10. CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
 11. CGContextShowTextAtPoint(context, 45, 50, "Quartz 2D", 9);

//



//part 3
UIImage *testImg =  UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

[testImg drawAtPoint:CGPointMake(35, 10)];
// 

http://i40.tinypic.com/140aptv.png

part 1 and part 3 of the code is very clear to me

problem is regarding part 2

on line 2 and 3 coordinates are transformed so the text do not display upside down, but uiimage already take care of this internally, why it didn't transformed to upside down? why it is still displaying in correct position after transform is applied for text using same context? I am asking this because when uiimage coordinates are already modified then this coordinate transform will not make uiimage again upside down?

on line 9 and 10 fillcolor and strokecolor methods are called and fillcolor changes the text color, but strokecolor not doing any thing to text why? And why without CGContextSaveGState it modified the color of text not the border color?

regarding these both points I mentioned above the common confusion is why its working perfectly why this code didn't need CGContextSaveGState and CGContextRestoreGState. How it is possible that context is modified and it didn't effect the perviously drawing item like blue border in this case and coordinates transformation for text.

Please correct me if I am lacking in any way to make you understand my points.

Thanks in advance, Regards.

1

1 Answers

0
votes

Quartz 2D uses the "painter's model." That means, you draw one thing, and it's done. Then you draw another thing, and it goes on top of what you drew before. Then you draw another thing and that goes on top, etc. If I pick up a stamp, dip it in paint and press it to paper, then turn it over and do it again to another part of the paper, the first stamped image doesn't flip over just because I flipped the stamp.

Every time you see "stroke" or "draw," you're modifying the final image. Later changes to the context don't effect that.