ios - UIGraphicsGetImageFromCurrentImageContext crash when using a queue -
i'm trying understand way code crash when using queue, working when using main thread. code works fine:
- (void)penmoved:(nsset *)touches { uitouch *touch = [touches anyobject]; cgpoint currentpoint = [touch locationinview:self]; cgpoint midpoint = midpoint(previouspoint, currentpoint); cgrect bounds = self.bounds; uigraphicsbeginimagecontextwithoptions(bounds.size, yes, [uiscreen mainscreen].scale); [path addquadcurvetopoint:midpoint controlpoint:previouspoint]; [incrementalimage drawatpoint:cgpointzero]; [path stroke]; previouspoint = currentpoint; incrementalimage = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); [self setneedsdisplay]; }
but version of code crashes after while when incrementalimage = uigraphicsgetimagefromcurrentimagecontext();
called.
- (void)penmoved:(nsset *)touches { dispatch_async(drawingqueue, ^{ uitouch *touch = [touches anyobject]; cgpoint currentpoint = [touch locationinview:self]; cgpoint midpoint = midpoint(previouspoint, currentpoint); cgrect bounds = self.bounds; uigraphicsbeginimagecontextwithoptions(bounds.size, yes, [uiscreen mainscreen].scale); [path addquadcurvetopoint:midpoint controlpoint:previouspoint]; [incrementalimage drawatpoint:cgpointzero]; [path stroke]; previouspoint = currentpoint; incrementalimage = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); dispatch_async(dispatch_get_main_queue(), ^{ [self setneedsdisplay]; }); }); }
drawingqueue defined ivar this:
dispatch_queue_t drawingqueue;
and initialized this:
drawingqueue = dispatch_queue_create("drawingqueue", null);
you talking touches. talking view. things not thread safe. must not talk them on background thread. , talking incrementalimage
property on 2 different threads. , that's obvious dangers; there other properties in there too.
Comments
Post a Comment