Skip to content

Commit ddbc69b

Browse files
committed
fix: handle gen2 ios pen race condition
1 parent 968d110 commit ddbc69b

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

ios/SketchCanvas/RNSketchCanvas.m

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,27 @@ - (void)didMoveToWindow {
250250
}
251251

252252
- (void)setFrozenImageNeedsUpdate {
253+
if (_frozenImage) {
253254
CGImageRelease(_frozenImage);
254-
CGImageRelease(_translucentFrozenImage);
255255
_frozenImage = nil;
256+
}
257+
if (_translucentFrozenImage) {
258+
CGImageRelease(_translucentFrozenImage);
256259
_translucentFrozenImage = nil;
260+
}
261+
}
262+
263+
- (void)ensureFrozenImagesUpToDate {
264+
if (!_drawingContext || !_translucentDrawingContext) {
265+
[self createDrawingContext];
266+
}
267+
if (!_frozenImage && _drawingContext) {
268+
_frozenImage = CGBitmapContextCreateImage(_drawingContext);
269+
}
270+
if (_currentPath && _currentPath.isTranslucent &&
271+
!_translucentFrozenImage && _translucentDrawingContext) {
272+
_translucentFrozenImage = CGBitmapContextCreateImage(_translucentDrawingContext);
273+
}
257274
}
258275

259276
- (BOOL)openSketchFile:(NSString *)filename directory:(NSString*) directory contentMode:(NSString*)mode {
@@ -454,6 +471,7 @@ - (void) clear {
454471
}
455472

456473
- (UIImage*)createImageWithTransparentBackground: (BOOL) transparent includeImage:(BOOL)includeImage includeText:(BOOL)includeText cropToImageSize:(BOOL)cropToImageSize {
474+
[self ensureFrozenImagesUpToDate];
457475
if (_backgroundImage && cropToImageSize) {
458476
CGRect rect = CGRectMake(0, 0, _backgroundImage.size.width, _backgroundImage.size.height);
459477
UIGraphicsBeginImageContextWithOptions(rect.size, !transparent, 1);
@@ -474,10 +492,12 @@ - (UIImage*)createImageWithTransparentBackground: (BOOL) transparent includeImag
474492
}
475493
}
476494
}
477-
478-
CGContextDrawImage(context, targetRect, _frozenImage);
479-
CGContextDrawImage(context, targetRect, _translucentFrozenImage);
480-
495+
496+
if (_frozenImage) CGContextDrawImage(context, targetRect, _frozenImage);
497+
if (_translucentFrozenImage && _currentPath && _currentPath.isTranslucent) {
498+
CGContextDrawImage(context, targetRect, _translucentFrozenImage);
499+
}
500+
481501
// Include current incomplete path in saved image
482502
if (_currentPath) {
483503
[_currentPath drawInContext:context];
@@ -516,8 +536,10 @@ - (UIImage*)createImageWithTransparentBackground: (BOOL) transparent includeImag
516536
}
517537
}
518538

519-
CGContextDrawImage(context, rect, _frozenImage);
520-
CGContextDrawImage(context, rect, _translucentFrozenImage);
539+
if (_frozenImage) CGContextDrawImage(context, rect, _frozenImage);
540+
if (_translucentFrozenImage && _currentPath && _currentPath.isTranslucent) {
541+
CGContextDrawImage(context, rect, _translucentFrozenImage);
542+
}
521543

522544
// Include current incomplete path in saved image
523545
if (_currentPath) {

0 commit comments

Comments
 (0)