Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Example/SDWebImageWebPCoderExample/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ - (void)viewDidLoad {
NSLog(@"%@", @"Static WebP load success");
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *webpData = [image sd_imageDataAsFormat:SDImageFormatWebP];
NSUInteger maxFileSize = 4096;
NSData *webpData = [SDImageWebPCoder.sharedCoder encodedDataWithImage:image format:SDImageFormatWebP options:@{SDImageCoderEncodeMaxFileSize : @(maxFileSize)}];
if (webpData) {
NSCAssert(webpData.length <= maxFileSize, @"WebP Encoding with max file size limit works");
NSLog(@"%@", @"WebP encoding success");
}
});
Expand Down
14 changes: 14 additions & 0 deletions Tests/SDWebImageWebPCoderTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,20 @@ - (void)test34StaticImageNotCreateCGContext {
XCTAssert(canvas == NULL);
}

- (void)test45WebPEncodingMaxFileSize {
NSURL *staticWebPURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestImageStatic" withExtension:@"webp"];
NSData *data = [NSData dataWithContentsOfURL:staticWebPURL];
UIImage *image = [UIImage sd_imageWithWebPData:data];
NSData *dataWithNoLimit = [SDImageWebPCoder.sharedCoder encodedDataWithImage:image format:SDImageFormatWebP options:nil];
XCTAssertNotNil(dataWithNoLimit);
NSUInteger maxFileSize = 8192;
NSData *dataWithLimit = [SDImageWebPCoder.sharedCoder encodedDataWithImage:image format:SDImageFormatWebP options:@{SDImageCoderEncodeMaxFileSize : @(maxFileSize)}];
XCTAssertNotNil(dataWithLimit);
XCTAssertGreaterThan(dataWithNoLimit.length, dataWithLimit.length);
XCTAssertGreaterThan(dataWithNoLimit.length, maxFileSize);
XCTAssertLessThanOrEqual(dataWithLimit.length, maxFileSize);
}

@end

@implementation SDWebImageWebPCoderTests (Helpers)
Expand Down