Skip to content

Commit 15ea811

Browse files
committed
Add the maxFramePerSecond and maxFrameSize API
1 parent 944dc89 commit 15ea811

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

Diff for: Example/SDWebImageVideoCoder/SDViewController.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ - (void)viewDidLoad
2727
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
2828
[self.view addSubview:self.imageView];
2929

30-
NSURL *gifvURL = [NSURL URLWithString:@"https://i.imgur.com/FY1AbSo.mp4"];
30+
NSURL *gifvURL = [NSURL URLWithString:@"https://imgur.com/download/NL1CXAF/LEGO+my+pizza"];
3131

3232
[self.imageView sd_setImageWithURL:gifvURL completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
3333
if (image) {

Diff for: SDWebImageVideoCoder/Classes/AVAssetImageGenerator+SDAdditions.h

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111

1212
@interface AVAssetImageGenerator (SDAdditions) <SDAnimatedImageProvider>
1313

14+
/// The desired animation image FPS. By default it's the Video FPS, larger value will consume much more CPU (25 FPS may consume 200% CPU)
15+
@property (nonatomic, assign) NSTimeInterval sd_framePerSecond;
16+
17+
/// Create a image generator with video data and desired format type
18+
/// @param data video data
19+
/// @param type video format type
1420
+ (nullable instancetype)sd_assetImageGeneratorWithVideoData:(nonnull NSData *)data contentType:(nullable AVFileType)type;
1521

1622
@end

Diff for: SDWebImageVideoCoder/Classes/AVAssetImageGenerator+SDAdditions.m

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ - (instancetype)initWithData:(NSData *)data contentType:(AVFileType)type;
2222
@interface AVAssetImageGenerator ()
2323

2424
@property (nonatomic, strong) NSData *sd_videoData;
25-
@property (nonatomic, assign) NSTimeInterval sd_framePerSecond;
2625

2726
@end
2827

Diff for: SDWebImageVideoCoder/Classes/SDImageVideoCoder.h

+12
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,20 @@
88

99
@import SDWebImage;
1010

11+
/// A coder plugin which can play Video format, such as MP4/MOV.
12+
/// For normal image, it generate the poster thumb image of video.
13+
/// For animated image, it use the `AVAssetImageGenerator` to generate individual frames by times.
1114
@interface SDImageVideoCoder : NSObject <SDAnimatedImageCoder>
1215

16+
/// The shared instance of this coder
1317
@property (nonatomic, class, readonly, nonnull) SDImageVideoCoder *sharedCoder;
1418

19+
/// The maximum FPS for animated image playback. This can dramatically impact the performance.
20+
/// By default, this value set to 10, which means the frame duration is 0.1s.
21+
@property (nonatomic, assign) NSTimeInterval maxFramePerSecond;
22+
23+
/// The maximum dimensions for the animated image playback. Zero specifies the asset’s unscaled dimensions.
24+
/// By default, this value set to Zero. You can limit the size to reduce CPU and memory usage.
25+
@property (nonatomic, assign) CGSize maxFrameSize;
26+
1527
@end

Diff for: SDWebImageVideoCoder/Classes/SDImageVideoCoder.m

+14-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ + (SDImageVideoCoder *)sharedCoder {
2626
return coder;
2727
}
2828

29+
- (instancetype)init {
30+
self = [super init];
31+
if (self) {
32+
self.maxFramePerSecond = 10;
33+
self.maxFrameSize = CGSizeZero;
34+
}
35+
return self;
36+
}
37+
2938
#pragma mark - Helper
3039

3140
- (AVFileType)videoContentTypeWithData:(NSData *)data {
@@ -78,7 +87,7 @@ - (nullable NSData *)encodedDataWithImage:(nullable UIImage *)image format:(SDIm
7887
#pragma mark - SDAnimatedImageCoder
7988

8089
- (nullable instancetype)initWithAnimatedImageData:(nullable NSData *)data options:(nullable SDImageCoderOptions *)options {
81-
self = [super init];
90+
self = [self init];
8291
if (self) {
8392
if (!data) {
8493
return nil;
@@ -88,6 +97,10 @@ - (nullable instancetype)initWithAnimatedImageData:(nullable NSData *)data optio
8897
if (!generator) {
8998
return nil;
9099
}
100+
if (generator.sd_framePerSecond > self.maxFramePerSecond) {
101+
generator.sd_framePerSecond = self.maxFramePerSecond;
102+
}
103+
generator.maximumSize = self.maxFrameSize;
91104
self.generator = generator;
92105
}
93106
return self;

0 commit comments

Comments
 (0)