ios - Drawing in another thread with CGImage / CGLayer -


i have custom uicollectionviewcell subclass draw clipping, stroking , transparency. works pretty on simulator , iphone 5, on older devices there noticeable performance problems.

so want move time-consuming drawing background thread. since -drawrect method called on main thread, ended saving drawn context cgimage (original question contained code using cglayer, sort of obsolete matt long pointed out).

here implementation of drawrect method inside class:

-(void)drawrect:(cgrect)rect {      cgcontextref ctx = uigraphicsgetcurrentcontext();     if (self.renderedsymbol != nil) {         cgcontextdrawimage(ctx, self.bounds, self.renderedsymbol);     } } 

rendering method defines renderedsymbol property:

- (void) rendercurrentsymbol {      [self.queue addoperationwithblock:^{  // creating custom context draw there (contexts not thread safe)         cgcolorspaceref space = cgcolorspacecreatedevicergb();         cgcontextref ctx = cgbitmapcontextcreate(nil, self.bounds.size.width, self.bounds.size.height, 8, self.bounds.size.width * (cgcolorspacegetnumberofcomponents(space) + 1), space, kcgimagealphapremultipliedlast);         cgcolorspacerelease(space);  // custom drawing goes here using 'ctx' context  // saving context cgimageref property used in drawrect         self.renderedsymbol = cgbitmapcontextcreateimage(ctx);  // asking main thread update ui             [[nsoperationqueue mainqueue] addoperationwithblock:^{             [self setneedsdisplayinrect:self.bounds];         }];          cgcontextrelease(ctx);      }]; } 

this setup works on main thread, when wrap nsoperationqueue or gcd, i'm getting lots of different "invalid context 0x0" errors. app doesn't crash itself, drawing doesn't happen. suppose there problem releasing custom created cgcontextref, don't know it.

here's property declarations. (i tried using atomic versions, didn't help)

@property (nonatomic) cgimageref renderedsymbol; @property (nonatomic, strong) nsoperationqueue *queue; @property (nonatomic, strong) nsstring *symbol; // used in custom drawing 

custom setters / getters properties:

-(nsoperationqueue *)queue {     if (!_queue) {         _queue = [[nsoperationqueue alloc] init];         _queue.name = @"background rendering";     }     return _queue; }    -(void)setsymbol:(nsstring *)symbol {     _symbol = symbol;     self.renderedsymbol = nil;     [self setneedsdisplayinrect:self.bounds]; }  -(cgimageref) renderedsymbol {     if (_renderedsymbol == nil) {         [self rendercurrentsymbol];     }     return _renderedsymbol; } 

what can do?

did notice document on cglayer you're referencing hasn't been updated since 2006? assumption you've made cglayer right solution incorrect. apple has abandoned technology , should too: http://iosptl.com/posts/cglayer-no-longer-recommended/ use core animation.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -