SlideShare uma empresa Scribd logo
1 de 24
CATiledLayer


Jesse Collis, Melbourne Cocoaheads, February 2012
       jesse@jcmultimedia.com.au / @sirjec
CATiledLayer
Tiled Views in 2009

           Tiled UIScrollViews

           iOS 3 Compatibility

                @2x.png
CATiledLayer in iOS4
My main goals

• CATiledLayer in a zooming scrollView
• Seamless integration into iOS4+
• Seamless integration between retina / non-
  retina screens
• provide only one sized tile (not @2x)
Success
Finally...
Success
Finally...
Here’s what I learnt
• UIViews are backed by CALayers
• All good CALayer documentation is OSX
  based
• NSView is not backed by CALayer
• drawRect: (more or less) equals
  drawLayer:inContext delegate method in
  OSX docs
What I learnt cont...
• Core Graphics helps with high resolution
  screens most of the time

• CATiledLayer is just a CALayer subclass
 • It’s instantiated as a UIView’s layer
 • It uses drawRect:
 • contentScale is set by it’s UIView
Basic Implementation
+(Class)layerClass
{
  return [CATiledLayer class];
}

- (id)initWithFrame:(CGRect)frame
{
  ...
  [(CATiledLayer *)self.layer setLevelsOfDetail:1];
  [(CATiledLayer *)self.layer setLevelsOfDetailBias:3];
  ...
}

- (void)drawRect:(CGRect)rect
{
  CGContextRef c = UIGraphicsGetCurrentContext();
  CGFloat scale = CGContextGetCTM(c).a;

    NSInteger col = (CGRectGetMinX(rect) * scale) / self.tileSize.width;
    NSInteger row = (CGRectGetMinY(rect) * scale) / self.tileSize.height;

    UIImage *tile_image = [self.tileSource tiledView:self
                                         imageForRow:row
                                    column:col scale:scale];
    [tile_image drawInRect:rect];
}
LOD & LODB

• LOD is the number of levels it will ask you
  for as you zoom out
• LODB is the number of levels it will use as
  you zoom in
UIScrollView zoomScale vs.
         CATiledLayer LODB


• zoomScale is measured on a linear scale
• zoomScale has an exponential effect on
  pixels
• Each LODB/LOD is a power of two more
  or less than the previous level of detail.
UIScrollView zoomScale vs.
         CATiledLayer LODB


• zoomScale is measured on a linear scale
• zoomScale has an exponential effect on
  pixels
• Each LODB/LOD is a power of two more
  or less than the previous level of detail.
Some Notes
• Setting LOD and LODB to 0 causes a blank
  view
• Setting LOD and LODB to 1 seems to be
  undefined behaviour
• Setting LOD to 1, and LODB > 1 is fine,
  but I like to set LOD to 0 if I’m using
  LODB.
Code Time




JCTiledLayer
Retina Display
Apple’s docs on Supporting High Resolution screens:

[You] may need to adjust [Core Animation Layers] drawing code to account for
scale factors. Normally, when you draw in your view’s drawRect: method … of
the layer’s delegate, the system automatically adjusts the graphics context to
account for scale factors. However, knowing or changing that scale factor
might still be necessary when your view does one of the following:

 • Creates additional Core Animation layers with different scale factors and
   composites them into its own content.
A few things you need to do
•   layer.contentsScale == 2 so everything in pixel land
    is squared

•   Multiply the original tileSize by the contentsScale

•   Multiply drawRect: rect by the context scale giving
    us our 512x512 tileSize

•   divide rect by contentsScale to give us rect with
    256x256 dimensions

•   rect origin can now be used to grab the right tile

•   draw said tile it into rect; Core Graphics will do
    the rest
What’s next

• Add UIGestureRecognizer support for
  more map-like features
• Overlay support
• Replace UIImages with vectors or SVG
  drawing
JCTiledView is on
     Github


     •   github.com/jessedc/JCTiledScrollView
Thanks!

Jesse Collis
jesse@jcmultimedia.com.au
twitter.com/sirjec

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

UIStackView – Tom Bowden – Dec 2015 – Eventacular Inc
UIStackView – Tom Bowden – Dec 2015 – Eventacular IncUIStackView – Tom Bowden – Dec 2015 – Eventacular Inc
UIStackView – Tom Bowden – Dec 2015 – Eventacular Inc
 
오딘: 발할라 라이징 MMORPG의 성능 최적화 사례 공유 [카카오게임즈 - 레벨 300] - 발표자: 김문권, 팀장, 라이온하트 스튜디오...
오딘: 발할라 라이징 MMORPG의 성능 최적화 사례 공유 [카카오게임즈 - 레벨 300] - 발표자: 김문권, 팀장, 라이온하트 스튜디오...오딘: 발할라 라이징 MMORPG의 성능 최적화 사례 공유 [카카오게임즈 - 레벨 300] - 발표자: 김문권, 팀장, 라이온하트 스튜디오...
오딘: 발할라 라이징 MMORPG의 성능 최적화 사례 공유 [카카오게임즈 - 레벨 300] - 발표자: 김문권, 팀장, 라이온하트 스튜디오...
 
Quick overview of Openstack architecture
Quick overview of Openstack architectureQuick overview of Openstack architecture
Quick overview of Openstack architecture
 
Learned from KIND
Learned from KIND Learned from KIND
Learned from KIND
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven Design
 
Introduction to DDD
Introduction to DDDIntroduction to DDD
Introduction to DDD
 
Control Kubernetes Ingress and Egress Together with NGINX
Control Kubernetes Ingress and Egress Together with NGINXControl Kubernetes Ingress and Egress Together with NGINX
Control Kubernetes Ingress and Egress Together with NGINX
 
動的コンテンツをオリジンとしたCloudFrontを構築してみた
動的コンテンツをオリジンとしたCloudFrontを構築してみた動的コンテンツをオリジンとしたCloudFrontを構築してみた
動的コンテンツをオリジンとしたCloudFrontを構築してみた
 
NDC17. "솔리테어: 덱다웃" 유니티 모바일 테스팅 기법들
NDC17. "솔리테어: 덱다웃" 유니티 모바일 테스팅 기법들NDC17. "솔리테어: 덱다웃" 유니티 모바일 테스팅 기법들
NDC17. "솔리테어: 덱다웃" 유니티 모바일 테스팅 기법들
 
Introduction to Microservices
Introduction to MicroservicesIntroduction to Microservices
Introduction to Microservices
 
AWS基礎
AWS基礎AWS基礎
AWS基礎
 
Kubernetes Basics
Kubernetes BasicsKubernetes Basics
Kubernetes Basics
 
01 azure well architected framework
01 azure well architected framework01 azure well architected framework
01 azure well architected framework
 
Kubernetes architecture
Kubernetes architectureKubernetes architecture
Kubernetes architecture
 
Hands-On Introduction to Kubernetes at LISA17
Hands-On Introduction to Kubernetes at LISA17Hands-On Introduction to Kubernetes at LISA17
Hands-On Introduction to Kubernetes at LISA17
 
KubeVirt (Kubernetes and Cloud Native Toronto)
KubeVirt (Kubernetes and Cloud Native Toronto)KubeVirt (Kubernetes and Cloud Native Toronto)
KubeVirt (Kubernetes and Cloud Native Toronto)
 
From Monolithic to Microservices
From Monolithic to Microservices From Monolithic to Microservices
From Monolithic to Microservices
 
Introduction of kubernetes rancher
Introduction of kubernetes rancherIntroduction of kubernetes rancher
Introduction of kubernetes rancher
 
First Steps to DevOps
First Steps to DevOpsFirst Steps to DevOps
First Steps to DevOps
 
Dependency injection in Java, from naive to functional
Dependency injection in Java, from naive to functionalDependency injection in Java, from naive to functional
Dependency injection in Java, from naive to functional
 

Semelhante a Implementing CATiledLayer

iOS Development: What's New
iOS Development: What's NewiOS Development: What's New
iOS Development: What's New
NascentDigital
 
Rails and iOS with RestKit
Rails and iOS with RestKitRails and iOS with RestKit
Rails and iOS with RestKit
Andrew Culver
 
From Legacy Database to Domain Layer Using a New Cincom VisualWorks Tool
From Legacy Database to Domain Layer Using a New Cincom VisualWorks ToolFrom Legacy Database to Domain Layer Using a New Cincom VisualWorks Tool
From Legacy Database to Domain Layer Using a New Cincom VisualWorks Tool
ESUG
 

Semelhante a Implementing CATiledLayer (20)

Hi performance table views with QuartzCore and CoreText
Hi performance table views with QuartzCore and CoreTextHi performance table views with QuartzCore and CoreText
Hi performance table views with QuartzCore and CoreText
 
React Native: Introduction
React Native: IntroductionReact Native: Introduction
React Native: Introduction
 
The Great CALayer Tour
The Great CALayer TourThe Great CALayer Tour
The Great CALayer Tour
 
JS Fest 2018. Илья Иванов. Введение в React-Native
JS Fest 2018. Илья Иванов. Введение в React-NativeJS Fest 2018. Илья Иванов. Введение в React-Native
JS Fest 2018. Илья Иванов. Введение в React-Native
 
Core animation
Core animationCore animation
Core animation
 
iOS Development: What's New
iOS Development: What's NewiOS Development: What's New
iOS Development: What's New
 
ASP.NET MVC 5 - EF 6 - VS2015
ASP.NET MVC 5 - EF 6 - VS2015ASP.NET MVC 5 - EF 6 - VS2015
ASP.NET MVC 5 - EF 6 - VS2015
 
Awesome html with ujs, jQuery and coffeescript
Awesome html with ujs, jQuery and coffeescriptAwesome html with ujs, jQuery and coffeescript
Awesome html with ujs, jQuery and coffeescript
 
The Grid the Brad and the Ugly: Using Grids to Improve Your Applications
The Grid the Brad and the Ugly: Using Grids to Improve Your ApplicationsThe Grid the Brad and the Ugly: Using Grids to Improve Your Applications
The Grid the Brad and the Ugly: Using Grids to Improve Your Applications
 
Cocos2d for beginners
Cocos2d for beginnersCocos2d for beginners
Cocos2d for beginners
 
iOS Coding Best Practices
iOS Coding Best PracticesiOS Coding Best Practices
iOS Coding Best Practices
 
Rails and iOS with RestKit
Rails and iOS with RestKitRails and iOS with RestKit
Rails and iOS with RestKit
 
Introduction of openGL
Introduction  of openGLIntroduction  of openGL
Introduction of openGL
 
Synapse india reviews on i phone and android os
Synapse india reviews on i phone and android osSynapse india reviews on i phone and android os
Synapse india reviews on i phone and android os
 
Designing True Cross-Platform Apps
Designing True Cross-Platform AppsDesigning True Cross-Platform Apps
Designing True Cross-Platform Apps
 
Synapse india mobile apps update
Synapse india mobile apps updateSynapse india mobile apps update
Synapse india mobile apps update
 
From Legacy Database to Domain Layer Using a New Cincom VisualWorks Tool
From Legacy Database to Domain Layer Using a New Cincom VisualWorks ToolFrom Legacy Database to Domain Layer Using a New Cincom VisualWorks Tool
From Legacy Database to Domain Layer Using a New Cincom VisualWorks Tool
 
IOS Storyboards
IOS StoryboardsIOS Storyboards
IOS Storyboards
 
MongoDB Charts Meetup - 7-24-2018
MongoDB Charts Meetup - 7-24-2018MongoDB Charts Meetup - 7-24-2018
MongoDB Charts Meetup - 7-24-2018
 
COMP340 TOPIC 4 THREE.JS.pptx
COMP340 TOPIC 4 THREE.JS.pptxCOMP340 TOPIC 4 THREE.JS.pptx
COMP340 TOPIC 4 THREE.JS.pptx
 

Mais de Jesse Collis (6)

Preparing for iOS 9 - Melbourne Cocoaheads June 2015
Preparing for iOS 9 - Melbourne Cocoaheads June 2015Preparing for iOS 9 - Melbourne Cocoaheads June 2015
Preparing for iOS 9 - Melbourne Cocoaheads June 2015
 
A Brief History of Melbourne Cocoaheads - May 2013
A Brief History of Melbourne Cocoaheads - May 2013A Brief History of Melbourne Cocoaheads - May 2013
A Brief History of Melbourne Cocoaheads - May 2013
 
Epic Refactorings - Melbourne Cocoaheads June 2011
Epic Refactorings - Melbourne Cocoaheads June 2011Epic Refactorings - Melbourne Cocoaheads June 2011
Epic Refactorings - Melbourne Cocoaheads June 2011
 
CATiled Layer - Melbourne Cocoheads February 2012
CATiled Layer - Melbourne Cocoheads February 2012CATiled Layer - Melbourne Cocoheads February 2012
CATiled Layer - Melbourne Cocoheads February 2012
 
Xcode 4 shortcuts (Melbourne Cocoheads April 2011)
Xcode 4 shortcuts (Melbourne Cocoheads April 2011)Xcode 4 shortcuts (Melbourne Cocoheads April 2011)
Xcode 4 shortcuts (Melbourne Cocoheads April 2011)
 
Integrating iAds (Melbourne Cocoaheads November 2010)
Integrating iAds (Melbourne Cocoaheads November 2010)Integrating iAds (Melbourne Cocoaheads November 2010)
Integrating iAds (Melbourne Cocoaheads November 2010)
 

Último

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
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...
 
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...
 
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
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
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 Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
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...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
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
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
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...
 

Implementing CATiledLayer

  • 1. CATiledLayer Jesse Collis, Melbourne Cocoaheads, February 2012 jesse@jcmultimedia.com.au / @sirjec
  • 3. Tiled Views in 2009 Tiled UIScrollViews iOS 3 Compatibility @2x.png
  • 5.
  • 6.
  • 7.
  • 8.
  • 9. My main goals • CATiledLayer in a zooming scrollView • Seamless integration into iOS4+ • Seamless integration between retina / non- retina screens • provide only one sized tile (not @2x)
  • 12. Here’s what I learnt • UIViews are backed by CALayers • All good CALayer documentation is OSX based • NSView is not backed by CALayer • drawRect: (more or less) equals drawLayer:inContext delegate method in OSX docs
  • 13. What I learnt cont... • Core Graphics helps with high resolution screens most of the time • CATiledLayer is just a CALayer subclass • It’s instantiated as a UIView’s layer • It uses drawRect: • contentScale is set by it’s UIView
  • 14. Basic Implementation +(Class)layerClass { return [CATiledLayer class]; } - (id)initWithFrame:(CGRect)frame { ... [(CATiledLayer *)self.layer setLevelsOfDetail:1]; [(CATiledLayer *)self.layer setLevelsOfDetailBias:3]; ... } - (void)drawRect:(CGRect)rect { CGContextRef c = UIGraphicsGetCurrentContext(); CGFloat scale = CGContextGetCTM(c).a; NSInteger col = (CGRectGetMinX(rect) * scale) / self.tileSize.width; NSInteger row = (CGRectGetMinY(rect) * scale) / self.tileSize.height; UIImage *tile_image = [self.tileSource tiledView:self imageForRow:row column:col scale:scale]; [tile_image drawInRect:rect]; }
  • 15. LOD & LODB • LOD is the number of levels it will ask you for as you zoom out • LODB is the number of levels it will use as you zoom in
  • 16. UIScrollView zoomScale vs. CATiledLayer LODB • zoomScale is measured on a linear scale • zoomScale has an exponential effect on pixels • Each LODB/LOD is a power of two more or less than the previous level of detail.
  • 17. UIScrollView zoomScale vs. CATiledLayer LODB • zoomScale is measured on a linear scale • zoomScale has an exponential effect on pixels • Each LODB/LOD is a power of two more or less than the previous level of detail.
  • 18. Some Notes • Setting LOD and LODB to 0 causes a blank view • Setting LOD and LODB to 1 seems to be undefined behaviour • Setting LOD to 1, and LODB > 1 is fine, but I like to set LOD to 0 if I’m using LODB.
  • 20. Retina Display Apple’s docs on Supporting High Resolution screens: [You] may need to adjust [Core Animation Layers] drawing code to account for scale factors. Normally, when you draw in your view’s drawRect: method … of the layer’s delegate, the system automatically adjusts the graphics context to account for scale factors. However, knowing or changing that scale factor might still be necessary when your view does one of the following: • Creates additional Core Animation layers with different scale factors and composites them into its own content.
  • 21. A few things you need to do • layer.contentsScale == 2 so everything in pixel land is squared • Multiply the original tileSize by the contentsScale • Multiply drawRect: rect by the context scale giving us our 512x512 tileSize • divide rect by contentsScale to give us rect with 256x256 dimensions • rect origin can now be used to grab the right tile • draw said tile it into rect; Core Graphics will do the rest
  • 22. What’s next • Add UIGestureRecognizer support for more map-like features • Overlay support • Replace UIImages with vectors or SVG drawing
  • 23. JCTiledView is on Github • github.com/jessedc/JCTiledScrollView

Notas do Editor

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n