From 2bb5228e7da02378962708f3438ed93ef72b3ad7 Mon Sep 17 00:00:00 2001 From: ipad4 Date: Fri, 2 Feb 2024 10:59:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dios17=E7=9A=84UIGraphicsBegin?= =?UTF-8?q?ImageContextWithOptions=E6=96=AD=E8=A8=80=E5=B4=A9=E6=BA=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TYSnapshotAuxiliaryPDFTool.m | 20 +++++++++++-------- .../TYSnapshotAuxiliary/TYSpellImage.m | 4 ++++ .../UIScrollView+TYSnapshotAuxiliary.m | 4 ++++ TYSnapshotScroll/Tools/UIImage+TYSnapshot.m | 4 ++++ TYSnapshotScroll/UIScrollView+TYSnapshot.m | 5 +++++ TYSnapshotScroll/UIScrollView+TYSplice.m | 5 +++++ TYSnapshotScroll/UITextView+TYSnapshot.m | 5 +++++ TYSnapshotScroll/UIView+TYSnapshot.m | 5 +++++ 8 files changed, 44 insertions(+), 8 deletions(-) diff --git a/TYSnapshotScroll/TYSnapshotAuxiliary/TYSnapshotAuxiliaryPDFTool.m b/TYSnapshotScroll/TYSnapshotAuxiliary/TYSnapshotAuxiliaryPDFTool.m index eb47076..19bf410 100644 --- a/TYSnapshotScroll/TYSnapshotAuxiliary/TYSnapshotAuxiliaryPDFTool.m +++ b/TYSnapshotScroll/TYSnapshotAuxiliary/TYSnapshotAuxiliaryPDFTool.m @@ -82,14 +82,18 @@ + (void)getPdfImageWithUrl:(NSURL *)fileUrl finishBlock:(void(^)(UIImage *image) UIImage *image; if (!CGRectEqualToRect(rect, CGRectNull)) { - UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0); - CGAffineTransform drawingTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, rect, 0, true); - CGContextRef ctx = UIGraphicsGetCurrentContext(); - CGContextConcatCTM(ctx, drawingTransform); - CGContextDrawPDFPage(ctx, page); - image = UIGraphicsGetImageFromCurrentImageContext(); - image = [UIImage imageWithCGImage:image.CGImage scale:1.0f orientation:UIImageOrientationDownMirrored]; - UIGraphicsEndImageContext(); + if (rect.size.width <= 0.0 || rect.size.height <= 0.0) { + image = nil; + } else { + UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0); + CGAffineTransform drawingTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, rect, 0, true); + CGContextRef ctx = UIGraphicsGetCurrentContext(); + CGContextConcatCTM(ctx, drawingTransform); + CGContextDrawPDFPage(ctx, page); + image = UIGraphicsGetImageFromCurrentImageContext(); + image = [UIImage imageWithCGImage:image.CGImage scale:1.0f orientation:UIImageOrientationDownMirrored]; + UIGraphicsEndImageContext(); + } } diff --git a/TYSnapshotScroll/TYSnapshotAuxiliary/TYSpellImage.m b/TYSnapshotScroll/TYSnapshotAuxiliary/TYSpellImage.m index 2fd4e3e..fd9f211 100644 --- a/TYSnapshotScroll/TYSnapshotAuxiliary/TYSpellImage.m +++ b/TYSnapshotScroll/TYSnapshotAuxiliary/TYSpellImage.m @@ -11,6 +11,10 @@ @implementation TYSpellImage + (UIImage *)tya_spellImageOf:(CGSize )imageSize paths:(NSArray *)paths{ + if (imageSize.width <= 0.0 || imageSize.height <= 0.0) { + return nil; + } + UIGraphicsBeginImageContextWithOptions(imageSize, YES, 0.0); CGFloat drawHeight = 0; diff --git a/TYSnapshotScroll/TYSnapshotAuxiliary/UIScrollView+TYSnapshotAuxiliary.m b/TYSnapshotScroll/TYSnapshotAuxiliary/UIScrollView+TYSnapshotAuxiliary.m index e9b03fa..5608d21 100644 --- a/TYSnapshotScroll/TYSnapshotAuxiliary/UIScrollView+TYSnapshotAuxiliary.m +++ b/TYSnapshotScroll/TYSnapshotAuxiliary/UIScrollView+TYSnapshotAuxiliary.m @@ -104,6 +104,10 @@ - (void )tya_scrollToDraw:(NSInteger )index maxIndex:(NSInteger )maxIndex finish dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.delayTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + if (snapshotSize.width <= 0.0 || snapshotSize.height <= 0.0) { + !finishBlock?:finishBlock(); + return; + } UIGraphicsBeginImageContextWithOptions(snapshotSize, NO, [UIScreen mainScreen].scale); [snapshotView drawViewHierarchyInRect:snapshotBounds afterScreenUpdates:YES]; diff --git a/TYSnapshotScroll/Tools/UIImage+TYSnapshot.m b/TYSnapshotScroll/Tools/UIImage+TYSnapshot.m index 56049db..151df69 100644 --- a/TYSnapshotScroll/Tools/UIImage+TYSnapshot.m +++ b/TYSnapshotScroll/Tools/UIImage+TYSnapshot.m @@ -17,6 +17,10 @@ + (UIImage *)getImageFromImagesArray:(NSArray *)imagesArr UIImage *image; @autoreleasepool{ CGSize imageTotalSize = [self getImageTotalSizeFromImagesArray:imagesArr]; + if (imageTotalSize.width <= 0.0 || imageTotalSize.height <= 0.0) { + return nil; + } + UIGraphicsBeginImageContextWithOptions(imageTotalSize, NO, [UIScreen mainScreen].scale); //拼接图片 diff --git a/TYSnapshotScroll/UIScrollView+TYSnapshot.m b/TYSnapshotScroll/UIScrollView+TYSnapshot.m index 4e22ac7..202f960 100755 --- a/TYSnapshotScroll/UIScrollView+TYSnapshot.m +++ b/TYSnapshotScroll/UIScrollView+TYSnapshot.m @@ -104,6 +104,11 @@ - (void )snapshotNormalImageWith:(UIView *)snapshotMaskView contentSize:(CGSize CGFloat delayTime = [TYSnapshotManager defaultManager].delayTime; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + if (self.layer.frame.size.width <= 0.0 || self.layer.frame.size.height <= 0.0) { + if (finishBlock) finishBlock(nil); + return; + + } UIImage* snapshotImage = nil; self.contentOffset = CGPointZero; diff --git a/TYSnapshotScroll/UIScrollView+TYSplice.m b/TYSnapshotScroll/UIScrollView+TYSplice.m index 60ee0dc..31c17cc 100644 --- a/TYSnapshotScroll/UIScrollView+TYSplice.m +++ b/TYSnapshotScroll/UIScrollView+TYSplice.m @@ -27,6 +27,11 @@ - (void )snapshotSpliceImageWith:(UIView *)snapshotMaskView contentSize:(CGSize self.contentOffset = CGPointZero; scrollViewBounds = self.bounds; }); + + if (contentSize.width <= 0.0 || contentSize.height <= 0.0) { + !finishBlock?:finishBlock(nil); + return; + } //计算快照屏幕数 NSUInteger snapshotScreenCount = floorf(contentSize.height / scrollViewBounds.size.height); diff --git a/TYSnapshotScroll/UITextView+TYSnapshot.m b/TYSnapshotScroll/UITextView+TYSnapshot.m index 6a9cb43..0b4d447 100644 --- a/TYSnapshotScroll/UITextView+TYSnapshot.m +++ b/TYSnapshotScroll/UITextView+TYSnapshot.m @@ -60,6 +60,11 @@ - (void )snapshotNormalImageWith:(UIView *)snapshotMaskView contentSize:(CGSize CGFloat delayTime = [TYSnapshotManager defaultManager].delayTime; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + if (self.layer.frame.size.width <= 0.0 || self.layer.frame.size.height <= 0.0) { + if (finishBlock) finishBlock(nil); + return; + + } UIImage* snapshotImage = nil; self.contentOffset = CGPointZero; diff --git a/TYSnapshotScroll/UIView+TYSnapshot.m b/TYSnapshotScroll/UIView+TYSnapshot.m index 4c7b313..2a1a5b6 100644 --- a/TYSnapshotScroll/UIView+TYSnapshot.m +++ b/TYSnapshotScroll/UIView+TYSnapshot.m @@ -30,6 +30,11 @@ - (void )screenSnapshotNeedMask:(BOOL)needMask addMaskAfterBlock:(void(^)(void)) selfLayer = self.layer; }); + if (bounds.size.width <= 0.0 || bounds.size.height <= 0.0) { + if (finishBlock) finishBlock(nil); + return; + } + UIGraphicsBeginImageContextWithOptions(bounds.size,NO,[UIScreen mainScreen].scale); CGContextRef context = UIGraphicsGetCurrentContext();