1414
1515
1616#if AS_AT_LEAST_IOS13
17- #define PERFORM_WORK_WITH_TRAIT_COLLECTION (work, traitCollection ) \
17+ #define ASPerformBlockWithTraitCollection (work, traitCollection ) \
1818 if (@available(iOS 13.0 , *)) { \
1919 UITraitCollection *uiTraitCollection = ASPrimitiveTraitCollectionToUITraitCollection (traitCollection); \
2020 [uiTraitCollection performAsCurrentTraitCollection: ^{ \
2424 work (); \
2525 }
2626#else
27- #define PERFORM_WORK_WITH_TRAIT_COLLECTION (work, traitCollection ) work();
27+ #define ASPerformBlockWithTraitCollection (work, traitCollection ) work();
2828#endif
2929
3030
@@ -43,10 +43,10 @@ NS_INLINE void ASConfigureExtendedRange(UIGraphicsImageRendererFormat *format)
4343 asdisplaynode_iscancelled_block_t NS_NOESCAPE isCancelled,
4444 void (^NS_NOESCAPE work)())
4545{
46- return ASGraphicsCreateImageWithTraitCollectionAndOptions (ASPrimitiveTraitCollectionMakeDefault (), size, opaque, scale, sourceImage, work);
46+ return ASGraphicsCreateImage (ASPrimitiveTraitCollectionMakeDefault (), size, opaque, scale, sourceImage, isCancelled , work);
4747}
4848
49- UIImage *ASGraphicsCreateImageWithTraitCollectionAndOptions (ASPrimitiveTraitCollection traitCollection, CGSize size, BOOL opaque, CGFloat scale, UIImage * sourceImage, void (NS_NOESCAPE ^work)()) {
49+ UIImage *ASGraphicsCreateImage (ASPrimitiveTraitCollection traitCollection, CGSize size, BOOL opaque, CGFloat scale, UIImage * sourceImage, asdisplaynode_iscancelled_block_t NS_NOESCAPE isCancelled , void (NS_NOESCAPE ^work)()) {
5050 if (AS_AVAILABLE_IOS_TVOS (10 , 10 )) {
5151 if (ASActivateExperimentalFeature (ASExperimentalDrawingGlobal)) {
5252 // If they used default scale, reuse one of two preferred formats.
@@ -98,17 +98,39 @@ NS_INLINE void ASConfigureExtendedRange(UIGraphicsImageRendererFormat *format)
9898 ASConfigureExtendedRange (format);
9999 }
100100
101- return [[[UIGraphicsImageRenderer alloc ] initWithSize: size format: format] imageWithActions: ^(UIGraphicsImageRendererContext *rendererContext) {
102- ASDisplayNodeCAssert (UIGraphicsGetCurrentContext (), @" Should have a context!" );
103- PERFORM_WORK_WITH_TRAIT_COLLECTION (work, traitCollection)
104- }];
101+ // Avoid using the imageWithActions: method because it does not support cancellation at the
102+ // last moment i.e. before actually creating the resulting image.
103+ __block UIImage *image;
104+ NSError *error;
105+ [[[UIGraphicsImageRenderer alloc ] initWithSize: size format: format]
106+ runDrawingActions: ^(UIGraphicsImageRendererContext *rendererContext) {
107+ ASDisplayNodeCAssert (UIGraphicsGetCurrentContext (), @" Should have a context!" );
108+ ASPerformBlockWithTraitCollection (work, traitCollection);
109+ }
110+ completionActions: ^(UIGraphicsImageRendererContext *rendererContext) {
111+ if (isCancelled == nil || !isCancelled ()) {
112+ image = rendererContext.currentImage ;
113+ }
114+ }
115+ error: &error];
116+ if (error) {
117+ NSCAssert (NO , @" Error drawing: %@ " , error);
118+ }
119+ return image;
105120 }
106121 }
107122
108123 // Bad OS or experiment flag. Use UIGraphics* API.
109124 UIGraphicsBeginImageContextWithOptions (size, opaque, scale);
110- PERFORM_WORK_WITH_TRAIT_COLLECTION (work, traitCollection)
111- UIImage *image = UIGraphicsGetImageFromCurrentImageContext ();
125+ ASPerformBlockWithTraitCollection (work, traitCollection)
126+ UIImage *image = nil ;
127+ if (isCancelled == nil || !isCancelled ()) {
128+ image = UIGraphicsGetImageFromCurrentImageContext ();
129+ }
112130 UIGraphicsEndImageContext ();
113131 return image;
114132}
133+
134+ UIImage *ASGraphicsCreateImageWithTraitCollectionAndOptions (ASPrimitiveTraitCollection traitCollection, CGSize size, BOOL opaque, CGFloat scale, UIImage * sourceImage, void (NS_NOESCAPE ^work)()) {
135+ return ASGraphicsCreateImage (traitCollection, size, opaque, scale, sourceImage, nil , work);
136+ }
0 commit comments