Contenu connexe Similaire à GCD in Action Similaire à GCD in Action (20) GCD in Action1. GCD in Action
• three practical use cases •
@mindbrix
Nigel Timothy Barber
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. OpenGL + UIScrollView
problem: OpenGL rendering on main thread
breaks UIScrollView timing loop
aim: smooth, zoomable canvas
solution: Grand Central Dispatch...
@mindbrix
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
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
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
14. GCD in Action
• three practical use cases •
@mindbrix
Nigel Timothy Barber