Skip to content

Instantly share code, notes, and snippets.

@laprasdrum
Created June 19, 2012 05:57
Show Gist options
  • Save laprasdrum/2952515 to your computer and use it in GitHub Desktop.
Save laprasdrum/2952515 to your computer and use it in GitHub Desktop.
UIKitの遅延描画(改)
#import <UIKit/UIKit.h>
@interface YMGraphCell : UIView
// オーバーライドして、遅延描画
- (void)drawRectAsync:(CGRect)rect andContext:(CGContextRef)context;
@end
#import "YMGraphCell.h"
@interface YMGraphCell()
{
UIImage* cache_;
}
- (void)callDrawRectAsync:(CGRect)rect;
@end
@implementation YMGraphCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)callDrawRectAsync:(CGRect)rect
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
CGFloat scale = [[UIScreen mainScreen] scale];
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, scale*defaultSize_.width, scale*defaultSize_.height, 8, scale*4*defaultSize_.width, colorSpace, kCGImageAlphaPremultipliedLast);
CGContextTranslateCTM(context, 0, scale*defaultSize_.height);
CGContextScaleCTM(context, scale, -scale);
[self drawGraphAsync:rect andContext:context];
CGImageRef image = CGBitmapContextCreateImage(context);
cache_ = [UIImage imageWithCGImage:image];
CGImageRelease(image);
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
dispatch_async(dispatch_get_main_queue(), ^{
self.layer.contents = (id)[cache_ CGImage];
});
});
}
- (void)drawRectAsync:(CGRect)rect andContext:(CGContextRef)context
{
// オーバーライドして遅延描画
}
- (void)drawRect:(CGRect)rect
{
if (!cache_) {
[self callDrawRectAsync:rect];
return;
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment