SlideShare une entreprise Scribd logo
1  sur  11
Core Graphics Threads
                  keeping your UI responsive


                       Glenn Marcus
                   Founder, Cliq Consulting



@cliqconsulting                      glenn@cliqconsulting.com
Who Am I?

     • Glenn Marcus - Tech Exec / Programmer
     • American living in São Paulo
     • Founder of Cliq Consulting


@cliqconsulting               glenn@cliqconsulting.com
Core Graphics
               Why ?                    Challenges
    •   Custom rendering of    •   C based API
        views
                               •   Pixels, not points
    •   Render on background
        threads                •   Extra state to manage

                               •   Requires CoreText for
                                   background rendering


@cliqconsulting                      glenn@cliqconsulting.com
Demo App


@cliqconsulting         glenn@cliqconsulting.com
Technique

     1. Create a backing layer to draw into

     2. On background thread, draw to layer

     3. On main thread, draw layer into view



@cliqconsulting                  glenn@cliqconsulting.com
Create a Layer
// Get the scale of the device
CGFloat contentScale = [[UIScreen mainScreen] scale];

// Calculate the pixels based on scale
CGSize layerSize = CGSizeMake(
               self.bounds.size.width * contentScale,
               self.bounds.size.height * contentScale);

// Create the layer with the current graphics context
destLayer = CGLayerCreateWithContext(
               UIGraphicsGetCurrentContext(),
               layerSize, NULL);

// Get the resulting layer
destContext = CGLayerGetContext(destLayer);

// Scale the layer context to match points
CGContextScaleCTM(destContext, contentScale, contentScale);

@cliqconsulting                       glenn@cliqconsulting.com
Conditional drawRect

if (self.layerIsReady)

  // Draw the layer into the view
  CGContextDrawLayerInRect(
        UIGraphicsGetCurrentContext(), rect, destLayer);
else

  // Prepare the layer in the background
  [self performSelectorInBackground:
        @selector(drawInBackground) withObject:nil];




@cliqconsulting                       glenn@cliqconsulting.com
Draw to Layer
// Do your usual CG rendering, using the layer Context
...
CGContextMoveToPoint(destContext, ...
CGContextAddLineToPoint(destContext, ...
CGContextStrokePath(destContext);
...

// When done, call a method on main thread to refresh view
self.layerIsReady = YES;
[self performSelectorOnMainThread:
       @selector(refreshImage)
       withObject:nil waitUntilDone:NO];




@cliqconsulting                       glenn@cliqconsulting.com
Refresh View

// setNeedsDisplay will call drawRect on the next event loop
- (void)refreshImage;
{
    [self setNeedsDisplay];
}




@cliqconsulting                       glenn@cliqconsulting.com
Questions


@cliqconsulting         glenn@cliqconsulting.com
Thanks!!!
                      Glenn Marcus
                  Founder, Cliq Consulting



@cliqconsulting                     glenn@cliqconsulting.com

Contenu connexe

En vedette

Core Graphics & Core Animation
Core Graphics & Core AnimationCore Graphics & Core Animation
Core Graphics & Core Animation
Andreas Blick
 

En vedette (13)

Animation in iOS
Animation in iOSAnimation in iOS
Animation in iOS
 
iOS Developer Interview Questions
iOS Developer Interview QuestionsiOS Developer Interview Questions
iOS Developer Interview Questions
 
20 iOS developer interview questions
20 iOS developer interview questions20 iOS developer interview questions
20 iOS developer interview questions
 
Core Graphics & Core Animation
Core Graphics & Core AnimationCore Graphics & Core Animation
Core Graphics & Core Animation
 
Graphics Libraries
Graphics LibrariesGraphics Libraries
Graphics Libraries
 
Objective-C for Java Developers
Objective-C for Java DevelopersObjective-C for Java Developers
Objective-C for Java Developers
 
Drawing with Quartz on iOS
Drawing with Quartz on iOSDrawing with Quartz on iOS
Drawing with Quartz on iOS
 
try! Swift - Advanced Graphics with Core Animation
try! Swift - Advanced Graphics with Core Animationtry! Swift - Advanced Graphics with Core Animation
try! Swift - Advanced Graphics with Core Animation
 
Designing better user interfaces
Designing better user interfacesDesigning better user interfaces
Designing better user interfaces
 
iOS Coding Best Practices
iOS Coding Best PracticesiOS Coding Best Practices
iOS Coding Best Practices
 
iOS design: a case study
iOS design: a case studyiOS design: a case study
iOS design: a case study
 
Quartz 2D with Swift 3
Quartz 2D with Swift 3Quartz 2D with Swift 3
Quartz 2D with Swift 3
 
iOS Scroll Performance
iOS Scroll PerformanceiOS Scroll Performance
iOS Scroll Performance
 

Dernier

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Dernier (20)

Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 

Core Graphics on background threads

  • 1. Core Graphics Threads keeping your UI responsive Glenn Marcus Founder, Cliq Consulting @cliqconsulting glenn@cliqconsulting.com
  • 2. Who Am I? • Glenn Marcus - Tech Exec / Programmer • American living in São Paulo • Founder of Cliq Consulting @cliqconsulting glenn@cliqconsulting.com
  • 3. Core Graphics Why ? Challenges • Custom rendering of • C based API views • Pixels, not points • Render on background threads • Extra state to manage • Requires CoreText for background rendering @cliqconsulting glenn@cliqconsulting.com
  • 4. Demo App @cliqconsulting glenn@cliqconsulting.com
  • 5. Technique 1. Create a backing layer to draw into 2. On background thread, draw to layer 3. On main thread, draw layer into view @cliqconsulting glenn@cliqconsulting.com
  • 6. Create a Layer // Get the scale of the device CGFloat contentScale = [[UIScreen mainScreen] scale]; // Calculate the pixels based on scale CGSize layerSize = CGSizeMake( self.bounds.size.width * contentScale, self.bounds.size.height * contentScale); // Create the layer with the current graphics context destLayer = CGLayerCreateWithContext( UIGraphicsGetCurrentContext(), layerSize, NULL); // Get the resulting layer destContext = CGLayerGetContext(destLayer); // Scale the layer context to match points CGContextScaleCTM(destContext, contentScale, contentScale); @cliqconsulting glenn@cliqconsulting.com
  • 7. Conditional drawRect if (self.layerIsReady) // Draw the layer into the view CGContextDrawLayerInRect( UIGraphicsGetCurrentContext(), rect, destLayer); else // Prepare the layer in the background [self performSelectorInBackground: @selector(drawInBackground) withObject:nil]; @cliqconsulting glenn@cliqconsulting.com
  • 8. Draw to Layer // Do your usual CG rendering, using the layer Context ... CGContextMoveToPoint(destContext, ... CGContextAddLineToPoint(destContext, ... CGContextStrokePath(destContext); ... // When done, call a method on main thread to refresh view self.layerIsReady = YES; [self performSelectorOnMainThread: @selector(refreshImage) withObject:nil waitUntilDone:NO]; @cliqconsulting glenn@cliqconsulting.com
  • 9. Refresh View // setNeedsDisplay will call drawRect on the next event loop - (void)refreshImage; { [self setNeedsDisplay]; } @cliqconsulting glenn@cliqconsulting.com
  • 10. Questions @cliqconsulting glenn@cliqconsulting.com
  • 11. Thanks!!! Glenn Marcus Founder, Cliq Consulting @cliqconsulting glenn@cliqconsulting.com

Notes de l'éditeur

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n