Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 626223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:23:42+00:00 2026-05-13T19:23:42+00:00

in my application I use have the following views hierarchy: UIView —-UIScrollView ——–TiledView (UIView

  • 0

in my application I use have the following views hierarchy:

UIView
----UIScrollView
--------TiledView (UIView subclass, uses CATiledLayer for drawing)
----OverlayView (UIView subclass)

In short – TiledView displays large tiled image I also apply custom rotation to that view:

tiledView.transform = CGAffineTransformMakeRotation(angle);

Drawing method for TiledView:

- (void)drawRect:(CGRect)rect {
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();  
    ...
  NSString *fileName = [NSString stringWithFormat:@"tile_%d_%d.jpg", y + 1, x + 1];
    UIImage *image = [UIImage imageNamed:fileName];
    [image drawInRect:rect];
 }

UIScrollView allows both scrolling and zooming of its contents.
Overlay view lays over UIScrollView, has transparent background and performs some custom drawing. I use separate view to make sure that line width and font size are not affected by zoom scale in scroll view.

Drawing method for OverlayView:

- (void)drawRect:(CGRect)rect {
    // Drawing code
    [super drawRect:rect];

    // Custom drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetRGBStrokeColor(context, 1.0, 1.0, 0, 1);
   CGContextSetLineWidth(context, lineWidth);

    CGContextBeginPath(context);
    CGContextMoveToPoint(context, (int)startPoint.x,(int) startPoint.y);

    if (usesMidPoint)
        CGContextAddLineToPoint(context, (int)midPoint.x, (int)midPoint.y); 
    CGContextAddLineToPoint(context, endPoint.x, endPoint.y);

    CGContextStrokePath(context);
}

Initially everything works ok, but after some playing with the view (e.g. scrolling it to and fro etc) it crashes on some random line in one of the drawing functions. As an example app crashes on line:

CGContextSetRGBStrokeColor(context, 1.0, 1.0, 0, 1); 

with stack:

#0  0x00503088 in CGColorEqualToColor
#1  0x00505430 in CGGStateSetStrokeColor
#2  0x005053b6 in setStrokeColorWithComponents
#3  0x0056150f in CGContextSetRGBStrokeColor
#4  0x000764ab in -[GADrawView drawRect:] at GADrawView.m:38
#5  0x016a7a78 in -[UIView(CALayerDelegate) drawLayer:inContext:]
#6  0x0077c007 in -[CALayer drawInContext:]

Am I missing any synchronization required between several graphics contexts? Or may be there’s better way to do what I’m trying?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:23:42+00:00Added an answer on May 13, 2026 at 7:23 pm

    Found solution to the problem. As described in Apple’s technical note CATiledLayer uses separate thread to fetch the contents for its tiles:

    The CATiledLayer implements its
    drawing by using a background thread
    to fetch the contents of each tile,
    however the drawing functions provided
    by UIKit rely on a global context
    stack, and as such when a CATiledLayer
    starts rendering, using any of UIKit’s
    drawing functions can result in a race
    condition.

    So the solution is to move all drawing code from -drawRect: method to -drawLayer:inContext: and draw using only Core Graphics functions. Drawing with CoreGraphics also requires some transformations between coordinates systems – this post from apple forums helped with them (see drawLayer: method implementation).

    So the correct drawing code for TiledView:

    - (void)drawRect:(CGRect)rect {
        //Still need to have this method implemented even if its empty!
    }
    
    -(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx
    {
        // Do all your drawing here. Do not use UIGraphics to do any drawing, use Core Graphics instead.
        // convert the CA coordinate system to the iPhone coordinate system
        CGContextTranslateCTM(ctx, 0.0f, 0.0f);
        CGContextScaleCTM(ctx, 1.0f, -1.0f);
    
        CGRect box = CGContextGetClipBoundingBox(ctx);
    
        // invert the Y-coord to translate between CA coords and iPhone coords
        CGPoint pixelTopLeft = CGPointApplyAffineTransform(box.origin, CGAffineTransformMakeScale(1.0f, -1.0f));
    
        NSString *tileUrlString = [self urlForPoint:pixelTopLeft];
    
        UIImage *image = [UIImage imageNamed:tileUrlString];
        CGContextDrawImage(ctx, box, [image CGImage]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to use an ugly C-library inside my c++ application. In the following
I have an MVC web application, the urls like following in my views\ folder:
I have the following code in my views: def __init__(self, obj='', json_opts={}, mimetype=application/json, *args,
Currently we have an application who use spring who support mysql. Some people prefer
i have application who use specific process during running and i want to sample
I have an Access application which use SQLServer 2008 R2 as database. When I
I have configured my application to use the Service configuration Local in the development
I have a Web Application that use Hibernate 3.0. When I restart my database
I have configured my application to use form based authentication and set up the
I have an C# form application that use an access database. This application works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.