Commit dc0e77c3 authored by yuzheng.tz's avatar yuzheng.tz

新增gifPreviewMaxImagesCount属性配置,默认为200,避免播放gif时图片帧数过多内存飙升崩溃

parent 81fcf841
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#import "UIView+Layout.h" #import "UIView+Layout.h"
#import <ImageIO/ImageIO.h> #import <ImageIO/ImageIO.h>
#import "TZImageManager.h" #import "TZImageManager.h"
#import "TZImagePickerController.h"
@implementation TZImageCropManager @implementation TZImageCropManager
...@@ -131,17 +132,21 @@ ...@@ -131,17 +132,21 @@
animatedImage = [[UIImage alloc] initWithData:data]; animatedImage = [[UIImage alloc] initWithData:data];
} }
else { else {
// images数组过大时内存会飙升,在这里限制下最大count
NSInteger maxCount = [TZImagePickerConfig sharedInstance].gifPreviewMaxImagesCount ?: 200;
NSInteger interval = MAX((count + maxCount / 2) / maxCount, 1);
NSMutableArray *images = [NSMutableArray array]; NSMutableArray *images = [NSMutableArray array];
NSTimeInterval duration = 0.0f; NSTimeInterval duration = 0.0f;
for (size_t i = 0; i < count; i++) { for (size_t i = 0; i < count; i+=interval) {
CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL); CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);
if (!image) { if (!image) {
continue; continue;
} }
duration += [self sd_frameDurationAtIndex:i source:source]; duration += [self sd_frameDurationAtIndex:i source:source] * MIN(interval, 3);
[images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]]; [images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];
......
...@@ -272,4 +272,6 @@ ...@@ -272,4 +272,6 @@
@property(nonatomic, assign) BOOL allowPickingImage; @property(nonatomic, assign) BOOL allowPickingImage;
@property (nonatomic, assign) BOOL allowPickingVideo; @property (nonatomic, assign) BOOL allowPickingVideo;
@property (strong, nonatomic) NSBundle *languageBundle; @property (strong, nonatomic) NSBundle *languageBundle;
/// 默认是200,如果一个GIF过大,里面图片个数可能超过1000,会导致内存飙升而崩溃
@property (assign, nonatomic) NSInteger gifPreviewMaxImagesCount;
@end @end
...@@ -820,6 +820,7 @@ ...@@ -820,6 +820,7 @@
if (config == nil) { if (config == nil) {
config = [[TZImagePickerConfig alloc] init]; config = [[TZImagePickerConfig alloc] init];
config.preferredLanguage = nil; config.preferredLanguage = nil;
config.gifPreviewMaxImagesCount = 200;
} }
}); });
return config; return config;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment