Merge pull request #3206 from dreampiggy/revert_imageio_terminate_decode_crash

Revert the "Don't encode/decode image when app will terminate"
This commit is contained in:
DreamPiggy 2021-04-07 11:02:44 +08:00 committed by GitHub
commit 3fcf40d12f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 7 additions and 75 deletions

View File

@ -13,7 +13,6 @@
#import "SDImageCoderHelper.h" #import "SDImageCoderHelper.h"
#import "SDAnimatedImageRep.h" #import "SDAnimatedImageRep.h"
#import "UIImage+ForceDecode.h" #import "UIImage+ForceDecode.h"
#import "SDInternalMacros.h"
// Specify DPI for vector format in CGImageSource, like PDF // Specify DPI for vector format in CGImageSource, like PDF
static NSString * kSDCGImageSourceRasterizationDPI = @"kCGImageSourceRasterizationDPI"; static NSString * kSDCGImageSourceRasterizationDPI = @"kCGImageSourceRasterizationDPI";
@ -30,9 +29,6 @@ static NSString * kSDCGImageDestinationRequestedFileSize = @"kCGImageDestination
@implementation SDImageIOCoderFrame @implementation SDImageIOCoderFrame
@end @end
static BOOL applicationWillTerminate = NO;
SD_LOCK_DECLARE_STATIC(applicationWillTerminateLock);
@implementation SDImageIOAnimatedCoder { @implementation SDImageIOAnimatedCoder {
size_t _width, _height; size_t _width, _height;
CGImageSourceRef _imageSource; CGImageSourceRef _imageSource;
@ -46,39 +42,8 @@ SD_LOCK_DECLARE_STATIC(applicationWillTerminateLock);
CGSize _thumbnailSize; CGSize _thumbnailSize;
} }
+ (void)initialize { - (void)dealloc
if (self == SDImageIOAnimatedCoder.class) { {
SD_LOCK_INIT(applicationWillTerminateLock);
#if SD_UIKIT
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillTerminate:)
name:UIApplicationWillTerminateNotification
object:nil];
#endif
#if SD_MAC
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillTerminate:)
name:NSApplicationWillTerminateNotification
object:nil];
#endif
}
}
+ (void)applicationWillTerminate:(NSNotification *)notification {
SD_LOCK(applicationWillTerminateLock);
applicationWillTerminate = YES;
SD_UNLOCK(applicationWillTerminateLock);
}
+ (BOOL)willTerminate {
SD_LOCK(applicationWillTerminateLock);
BOOL willTerminate = applicationWillTerminate;
SD_UNLOCK(applicationWillTerminateLock);
return willTerminate;
}
- (void)dealloc {
if (_imageSource) { if (_imageSource) {
CFRelease(_imageSource); CFRelease(_imageSource);
_imageSource = NULL; _imageSource = NULL;
@ -88,7 +53,8 @@ SD_LOCK_DECLARE_STATIC(applicationWillTerminateLock);
#endif #endif
} }
- (void)didReceiveMemoryWarning:(NSNotification *)notification { - (void)didReceiveMemoryWarning:(NSNotification *)notification
{
if (_imageSource) { if (_imageSource) {
for (size_t i = 0; i < _frameCount; i++) { for (size_t i = 0; i < _frameCount; i++) {
CGImageSourceRemoveCacheAtIndex(_imageSource, i); CGImageSourceRemoveCacheAtIndex(_imageSource, i);
@ -222,11 +188,6 @@ SD_LOCK_DECLARE_STATIC(applicationWillTerminateLock);
} }
+ (UIImage *)createFrameAtIndex:(NSUInteger)index source:(CGImageSourceRef)source scale:(CGFloat)scale preserveAspectRatio:(BOOL)preserveAspectRatio thumbnailSize:(CGSize)thumbnailSize options:(NSDictionary *)options { + (UIImage *)createFrameAtIndex:(NSUInteger)index source:(CGImageSourceRef)source scale:(CGFloat)scale preserveAspectRatio:(BOOL)preserveAspectRatio thumbnailSize:(CGSize)thumbnailSize options:(NSDictionary *)options {
// Earily return when application will be terminated.
if (SDImageIOAnimatedCoder.willTerminate) {
return nil;
}
// Some options need to pass to `CGImageSourceCopyPropertiesAtIndex` before `CGImageSourceCreateImageAtIndex`, or ImageIO will ignore them because they parse once :) // Some options need to pass to `CGImageSourceCopyPropertiesAtIndex` before `CGImageSourceCreateImageAtIndex`, or ImageIO will ignore them because they parse once :)
// Parse the image properties // Parse the image properties
NSDictionary *properties = (__bridge_transfer NSDictionary *)CGImageSourceCopyPropertiesAtIndex(source, index, (__bridge CFDictionaryRef)options); NSDictionary *properties = (__bridge_transfer NSDictionary *)CGImageSourceCopyPropertiesAtIndex(source, index, (__bridge CFDictionaryRef)options);
@ -414,11 +375,11 @@ SD_LOCK_DECLARE_STATIC(applicationWillTerminateLock);
CGSize thumbnailSize = CGSizeZero; CGSize thumbnailSize = CGSizeZero;
NSValue *thumbnailSizeValue = options[SDImageCoderDecodeThumbnailPixelSize]; NSValue *thumbnailSizeValue = options[SDImageCoderDecodeThumbnailPixelSize];
if (thumbnailSizeValue != nil) { if (thumbnailSizeValue != nil) {
#if SD_MAC #if SD_MAC
thumbnailSize = thumbnailSizeValue.sizeValue; thumbnailSize = thumbnailSizeValue.sizeValue;
#else #else
thumbnailSize = thumbnailSizeValue.CGSizeValue; thumbnailSize = thumbnailSizeValue.CGSizeValue;
#endif #endif
} }
_thumbnailSize = thumbnailSize; _thumbnailSize = thumbnailSize;
BOOL preserveAspectRatio = YES; BOOL preserveAspectRatio = YES;
@ -435,10 +396,6 @@ SD_LOCK_DECLARE_STATIC(applicationWillTerminateLock);
} }
- (void)updateIncrementalData:(NSData *)data finished:(BOOL)finished { - (void)updateIncrementalData:(NSData *)data finished:(BOOL)finished {
// Earily return when application will be terminated.
if (SDImageIOAnimatedCoder.willTerminate) {
return;
}
if (_finished) { if (_finished) {
return; return;
} }

View File

@ -138,10 +138,6 @@ static NSString * kSDCGImageDestinationRequestedFileSize = @"kCGImageDestination
} }
- (void)updateIncrementalData:(NSData *)data finished:(BOOL)finished { - (void)updateIncrementalData:(NSData *)data finished:(BOOL)finished {
// Earily return when application will be terminated.
if (SDImageIOAnimatedCoder.willTerminate) {
return;
}
if (_finished) { if (_finished) {
return; return;
} }

View File

@ -24,6 +24,5 @@
+ (nullable UIImage *)createFrameAtIndex:(NSUInteger)index source:(nonnull CGImageSourceRef)source scale:(CGFloat)scale preserveAspectRatio:(BOOL)preserveAspectRatio thumbnailSize:(CGSize)thumbnailSize options:(nullable NSDictionary *)options; + (nullable UIImage *)createFrameAtIndex:(NSUInteger)index source:(nonnull CGImageSourceRef)source scale:(CGFloat)scale preserveAspectRatio:(BOOL)preserveAspectRatio thumbnailSize:(CGSize)thumbnailSize options:(nullable NSDictionary *)options;
+ (BOOL)canEncodeToFormat:(SDImageFormat)format; + (BOOL)canEncodeToFormat:(SDImageFormat)format;
+ (BOOL)canDecodeFromFormat:(SDImageFormat)format; + (BOOL)canDecodeFromFormat:(SDImageFormat)format;
+ (BOOL)willTerminate;
@end @end

View File

@ -311,26 +311,6 @@
} }
} }
- (void)test22DoNotDecodeImageWhenApplicationWillTerminate {
[[SDImageCodersManager sharedManager] addCoder:SDImageIOCoder.sharedCoder];
XCTestExpectation *expectation = [self expectationWithDescription:@"doNotDecodeImageWhenApplicationWillTerminate"];
NSString *testImagePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"TestImageLarge" ofType:@"png"];
NSData *testImageData = [NSData dataWithContentsOfFile:testImagePath];
[[SDImageCache sharedImageCache] storeImageDataToDisk:testImageData forKey:@"TestImageLarge"];
NSOperation *operation = [[SDImageCache sharedImageCache] queryCacheOperationForKey:@"TestImageLarge" done:^(UIImage *image, NSData *data, SDImageCacheType cacheType) {
expect(data).to.equal(testImageData);
expect(image).to.beNil;
[[SDImageCache sharedImageCache] removeImageForKey:@"TestImageLarge" withCompletion:^{
[expectation fulfill];
}];
}];
expect(operation).toNot.beNil;
[operation start];
[[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationWillTerminateNotification object:nil];
[self waitForExpectationsWithCommonTimeout];
}
#pragma mark - Utils #pragma mark - Utils
- (void)verifyCoder:(id<SDImageCoder>)coder - (void)verifyCoder:(id<SDImageCoder>)coder