GCD in Action

1,048 views

Published on

Published in: Technology, Business
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,048
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
0
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

GCD in Action

  1. 1. GCD in Action • three practical use cases • @mindbrix Nigel Timothy Barber
  2. 2. • Blocks • Queues • Sources • Groups • Semaphores Grand Central Dispatch http://developer.apple.com/library/ios/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html @mindbrix
  3. 3. OpenGL + UIScrollView problem: OpenGL rendering on main thread breaks UIScrollView timing loop aim: smooth, zoomable canvas solution: Grand Central Dispatch... @mindbrix
  4. 4. dispatch_queue_t glQueue = dispatch_queue_create( "uk.co.mindbrix.glQueue", DISPATCH_QUEUE_SERIAL ); dispatch_semaphore_t frameRenderingSemaphore = dispatch_semaphore_create( 1 ); -(void)drawViewQueued:(CADisplayLink *)displayLink { if( dispatch_semaphore_wait( frameRenderingSemaphore, DISPATCH_TIME_NOW ) != 0 ) { return; } dispatch_async( glQueue, ^{ [ EAGLContext setCurrentContext: m_context ]; [ self render ]; dispatch_semaphore_signal( frameRenderingSemaphore ); }); } OpenGL + UIScrollView @mindbrix
  5. 5. OpenGL + UIScrollView @mindbrix
  6. 6. UITableViewCell problem: recursive pattern and PDF UIImage rendering is slow aim: smooth thumbnail scrolling solution: Grand Central Dispatch... @mindbrix
  7. 7. -(void)loadTableCellImage:(VSNarrowCell *)cell fromURL:(NSURL *)url atSize:(CGSize)size withPlaceholder:(UIImage *)placeholder { if(![ cell.imageURL isEqual:url ] || ( cell.imageView == nil )) { cell.imageView.image = placeholder; cell.imageURL = url; dispatch_async( renderingQueue, ^() { UIImage *image = nil; if([[ url pathExtension ] caseInsensitiveCompare:@"pdf" ] == NSOrderedSame ) { image = [ UIImage imageWithPDFURL:url atSize:size ]; } else { image = [ UIImage imageWithPathTreeURL:url atSize:size ]; } dispatch_async( dispatch_get_main_queue(), ^() { if([ cell.imageURL isEqual:url ]) { cell.imageView.image = image; } }); }); } } UITableViewCell @mindbrix
  8. 8. UITableViewCell @mindbrix
  9. 9. UITableViewCell @mindbrix
  10. 10. UILabel + NSAttributedString problem: slow UILabel drawing makes the main loop stutter aim: continuous attributed label updates solution: Grand Central Dispatch... @mindbrix
  11. 11. UILabel + NSAttributedString -(void)drawAfterUpdate:(void (^)(void))updateBlock { if( self.view && updateBlock ) { if (dispatch_semaphore_wait( _renderingSemaphore, DISPATCH_TIME_NOW ) != 0 ) { return; } dispatch_async( _renderingQueue, ^() { updateBlock(); UIImage *image = [ self imageFromView:self.view ]; dispatch_async( dispatch_get_main_queue(), ^() { self.image = image; }); dispatch_semaphore_signal( _renderingSemaphore ); }); } } -(id)initWithView:(UIView *)view { self = [ super initWithFrame:view.frame ]; if( self ) { self.view = view; if( view.superview ) { [ view removeFromSuperview ]; } _renderingSemaphore = dispatch_semaphore_create( 1 ); _renderingQueue = dispatch_queue_create( "uk.co.mindbrix.NTBProxyImageView", DISPATCH_QUEUE_SERIAL ); } return self; } [ self.valueLabelProxy drawAfterUpdate:^{ if([ self.valueLabel respondsToSelector:@selector(setAttributedText:)]) { NSString *HTML = @"<font name='Helvetica' color='#FFFFFF' size='12'><b>Hello, World</b></ font>; self.valueLabel.attributedText = [ NSAttributedString stringFromHTML: HTML ]; } else { self.valueLabel.text = @"Hello, World"; } }]; self.valueLabel = [[ UILabel alloc ] initWithFrame:valueLabelFrame ]; self.valueLabel.backgroundColor = [ UIColor clearColor ]; self.valueLabel.textColor = [ UIColor whiteColor ]; self.valueLabel.textAlignment = NSTextAlignmentCenter; [ rootView addSubview:self.valueLabel ]; [ self.valueLabel release ]; self.valueLabelProxy = [[ NTBProxyImageView alloc ] initWithView:self.valueLabel ]; [ rootView addSubview:self.valueLabelProxy ]; [ self.valueLabelProxy release ]; NTBProxyImageView NTBProxyImageView @mindbrix
  12. 12. UILabel + NSAttributedString @mindbrix
  13. 13. Links @mindbrix https://github.com/mindbrix/UIImage-PDF UIImage+PDF https://gist.github.com/mindbrix/5832707 NTBProxyImageView
  14. 14. GCD in Action • three practical use cases • @mindbrix Nigel Timothy Barber

×