Skip to content

Commit 26d4fb1

Browse files
authored
Merge pull request #38 from SDWebImage/fix_max_file_size_passes
Update the example and test case for the max file size
2 parents e25194e + 6c2651b commit 26d4fb1

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

Example/SDWebImageWebPCoderExample/ViewController.m

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ - (void)viewDidLoad {
4242
NSLog(@"%@", @"Static WebP load success");
4343
}
4444
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
45-
NSData *webpData = [image sd_imageDataAsFormat:SDImageFormatWebP];
45+
NSUInteger maxFileSize = 4096;
46+
NSData *webpData = [SDImageWebPCoder.sharedCoder encodedDataWithImage:image format:SDImageFormatWebP options:@{SDImageCoderEncodeMaxFileSize : @(maxFileSize)}];
4647
if (webpData) {
48+
NSCAssert(webpData.length <= maxFileSize, @"WebP Encoding with max file size limit works");
4749
NSLog(@"%@", @"WebP encoding success");
4850
}
4951
});

Tests/SDWebImageWebPCoderTests.m

+14
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,20 @@ - (void)test34StaticImageNotCreateCGContext {
182182
XCTAssert(canvas == NULL);
183183
}
184184

185+
- (void)test45WebPEncodingMaxFileSize {
186+
NSURL *staticWebPURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestImageStatic" withExtension:@"webp"];
187+
NSData *data = [NSData dataWithContentsOfURL:staticWebPURL];
188+
UIImage *image = [UIImage sd_imageWithWebPData:data];
189+
NSData *dataWithNoLimit = [SDImageWebPCoder.sharedCoder encodedDataWithImage:image format:SDImageFormatWebP options:nil];
190+
XCTAssertNotNil(dataWithNoLimit);
191+
NSUInteger maxFileSize = 8192;
192+
NSData *dataWithLimit = [SDImageWebPCoder.sharedCoder encodedDataWithImage:image format:SDImageFormatWebP options:@{SDImageCoderEncodeMaxFileSize : @(maxFileSize)}];
193+
XCTAssertNotNil(dataWithLimit);
194+
XCTAssertGreaterThan(dataWithNoLimit.length, dataWithLimit.length);
195+
XCTAssertGreaterThan(dataWithNoLimit.length, maxFileSize);
196+
XCTAssertLessThanOrEqual(dataWithLimit.length, maxFileSize);
197+
}
198+
185199
@end
186200

187201
@implementation SDWebImageWebPCoderTests (Helpers)

0 commit comments

Comments
 (0)