Skip to content

Commit 835eb13

Browse files
committed
Change the encoding test into official macOS unit test
Added `testWebPEncodingWithICCProfile`
1 parent daa8373 commit 835eb13

File tree

12 files changed

+407
-14
lines changed

12 files changed

+407
-14
lines changed

Diff for: Cartfile.resolved

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
github "SDWebImage/SDWebImage" "5.10.0"
2-
github "SDWebImage/libwebp-Xcode" "1.1.0"
1+
github "SDWebImage/SDWebImage" "5.18.4"
2+
github "SDWebImage/libwebp-Xcode" "1.3.2"

Diff for: Example/SDWebImageWebPCoderExample-macOS/Info.plist

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>NSAppTransportSecurity</key>
6+
<dict>
7+
<key>NSAllowsArbitraryLoads</key>
8+
<true/>
9+
</dict>
10+
</dict>
11+
</plist>

Diff for: Example/SDWebImageWebPCoderExample-macOS/ViewController.m

+46-9
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,59 @@
1010
#import <SDWebImageWebPCoder/SDWebImageWebPCoder.h>
1111
#import <SDWebImage/SDWebImage.h>
1212

13+
@interface ViewController ()
14+
15+
@property (nonatomic, strong) UIImageView *imageView1;
16+
@property (nonatomic, strong) SDAnimatedImageView *imageView2;
17+
18+
@end
19+
1320
@implementation ViewController
1421

1522
- (void)viewDidLoad {
1623
[super viewDidLoad];
17-
18-
// Test transcoding
19-
NSString *jpegPath = [NSBundle.mainBundle pathForResource:@"before.jpeg" ofType:nil inDirectory:@"examples/pexels-egor-kamelev-920163"];
20-
NSData *jpegData = [NSData dataWithContentsOfFile:jpegPath];
21-
NSImage *jpegImage = [[NSImage alloc] initWithData:jpegData];
2224

23-
NSData *webpData = [[SDImageWebPCoder sharedCoder] encodedDataWithImage:jpegImage format:SDImageFormatWebP options:nil];
24-
NSString *webpPath = [[jpegPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"after.webp"];
25-
BOOL success = [webpData writeToFile:webpPath atomically:YES];
26-
NSAssert(success, @"Encode WebP success");
25+
[SDImageCache.sharedImageCache clearDiskOnCompletion:nil];
26+
27+
[[SDImageCodersManager sharedManager] addCoder:[SDImageWebPCoder sharedCoder]];
28+
29+
self.imageView1 = [UIImageView new];
30+
self.imageView1.imageScaling = NSImageScaleProportionallyUpOrDown;
31+
[self.view addSubview:self.imageView1];
32+
33+
self.imageView2 = [SDAnimatedImageView new];
34+
self.imageView2.imageScaling = NSImageScaleProportionallyUpOrDown;
35+
[self.view addSubview:self.imageView2];
36+
37+
NSURL *staticWebPURL = [NSURL URLWithString:@"https://www.gstatic.com/webp/gallery/2.webp"];
38+
NSURL *animatedWebPURL = [NSURL URLWithString:@"http://littlesvr.ca/apng/images/world-cup-2014-42.webp"];
39+
40+
[self.imageView1 sd_setImageWithURL:staticWebPURL placeholderImage:nil options:0 context:@{SDWebImageContextImageScaleDownLimitBytes : @(1024 * 100)} progress:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
41+
NSCAssert(image.size.width < 200, @"Limit Bytes should limit image size to 186");
42+
if (image) {
43+
NSLog(@"%@", @"Static WebP load success");
44+
}
45+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
46+
NSUInteger maxFileSize = 4096;
47+
NSData *webpData = [SDImageWebPCoder.sharedCoder encodedDataWithImage:image format:SDImageFormatWebP options:@{SDImageCoderEncodeMaxFileSize : @(maxFileSize)}];
48+
if (webpData) {
49+
NSCAssert(webpData.length <= maxFileSize, @"WebP Encoding with max file size limit works");
50+
NSLog(@"%@", @"WebP encoding success");
51+
}
52+
});
53+
}];
54+
[self.imageView2 sd_setImageWithURL:animatedWebPURL placeholderImage:nil options:SDWebImageProgressiveLoad completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
55+
if (image) {
56+
NSLog(@"%@", @"Animated WebP load success");
57+
}
58+
}];
2759
}
2860

61+
- (void)viewWillLayout {
62+
[super viewWillLayout];
63+
self.imageView1.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height / 2);
64+
self.imageView2.frame = CGRectMake(0, self.view.bounds.size.height / 2, self.view.bounds.size.width, self.view.bounds.size.height / 2);
65+
}
2966

3067
- (void)setRepresentedObject:(id)representedObject {
3168
[super setRepresentedObject:representedObject];
Binary file not shown.

Diff for: Example/SDWebImageWebPCoderExample.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
329D55C52AFB8A1B008B4DA3 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
3636
329D55C72AFB8A1B008B4DA3 /* SDWebImageWebPCoderExample_macOS.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = SDWebImageWebPCoderExample_macOS.entitlements; sourceTree = "<group>"; };
3737
329D55E02AFB8C39008B4DA3 /* examples */ = {isa = PBXFileReference; lastKnownFileType = folder; path = examples; sourceTree = "<group>"; };
38+
329D55E22AFB916E008B4DA3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
3839
4038A28BEB6E86807E9286D7 /* Pods_SDWebImageWebPCoderExample_macOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SDWebImageWebPCoderExample_macOS.framework; sourceTree = BUILT_PRODUCTS_DIR; };
3940
803D79CE213597CB00C815FC /* SDWebImageWebPCoderExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SDWebImageWebPCoderExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
4041
803D79D1213597CB00C815FC /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
@@ -75,6 +76,7 @@
7576
329D55B92AFB8A1A008B4DA3 /* SDWebImageWebPCoderExample-macOS */ = {
7677
isa = PBXGroup;
7778
children = (
79+
329D55E22AFB916E008B4DA3 /* Info.plist */,
7880
329D55E02AFB8C39008B4DA3 /* examples */,
7981
329D55BA2AFB8A1A008B4DA3 /* AppDelegate.h */,
8082
329D55BB2AFB8A1A008B4DA3 /* AppDelegate.m */,
@@ -386,6 +388,7 @@
386388
COMBINE_HIDPI_IMAGES = YES;
387389
CURRENT_PROJECT_VERSION = 1;
388390
GENERATE_INFOPLIST_FILE = YES;
391+
INFOPLIST_FILE = "SDWebImageWebPCoderExample-macOS/Info.plist";
389392
INFOPLIST_KEY_NSHumanReadableCopyright = "Copyright © 2023 SDWebImage. All rights reserved.";
390393
INFOPLIST_KEY_NSMainStoryboardFile = Main;
391394
INFOPLIST_KEY_NSPrincipalClass = NSApplication;
@@ -418,6 +421,7 @@
418421
COMBINE_HIDPI_IMAGES = YES;
419422
CURRENT_PROJECT_VERSION = 1;
420423
GENERATE_INFOPLIST_FILE = YES;
424+
INFOPLIST_FILE = "SDWebImageWebPCoderExample-macOS/Info.plist";
421425
INFOPLIST_KEY_NSHumanReadableCopyright = "Copyright © 2023 SDWebImage. All rights reserved.";
422426
INFOPLIST_KEY_NSMainStoryboardFile = Main;
423427
INFOPLIST_KEY_NSPrincipalClass = NSApplication;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "1410"
4+
version = "1.3">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "329D55B72AFB8A1A008B4DA3"
18+
BuildableName = "SDWebImageWebPCoderExample-macOS.app"
19+
BlueprintName = "SDWebImageWebPCoderExample-macOS"
20+
ReferencedContainer = "container:SDWebImageWebPCoderExample.xcodeproj">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
</BuildActionEntries>
24+
</BuildAction>
25+
<TestAction
26+
buildConfiguration = "Debug"
27+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
28+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29+
shouldUseLaunchSchemeArgsEnv = "YES">
30+
<Testables>
31+
</Testables>
32+
</TestAction>
33+
<LaunchAction
34+
buildConfiguration = "Debug"
35+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
36+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
37+
launchStyle = "0"
38+
useCustomWorkingDirectory = "NO"
39+
ignoresPersistentStateOnLaunch = "NO"
40+
debugDocumentVersioning = "YES"
41+
debugServiceExtension = "internal"
42+
allowLocationSimulation = "YES">
43+
<BuildableProductRunnable
44+
runnableDebuggingMode = "0">
45+
<BuildableReference
46+
BuildableIdentifier = "primary"
47+
BlueprintIdentifier = "329D55B72AFB8A1A008B4DA3"
48+
BuildableName = "SDWebImageWebPCoderExample-macOS.app"
49+
BlueprintName = "SDWebImageWebPCoderExample-macOS"
50+
ReferencedContainer = "container:SDWebImageWebPCoderExample.xcodeproj">
51+
</BuildableReference>
52+
</BuildableProductRunnable>
53+
</LaunchAction>
54+
<ProfileAction
55+
buildConfiguration = "Release"
56+
shouldUseLaunchSchemeArgsEnv = "YES"
57+
savedToolIdentifier = ""
58+
useCustomWorkingDirectory = "NO"
59+
debugDocumentVersioning = "YES">
60+
<BuildableProductRunnable
61+
runnableDebuggingMode = "0">
62+
<BuildableReference
63+
BuildableIdentifier = "primary"
64+
BlueprintIdentifier = "329D55B72AFB8A1A008B4DA3"
65+
BuildableName = "SDWebImageWebPCoderExample-macOS.app"
66+
BlueprintName = "SDWebImageWebPCoderExample-macOS"
67+
ReferencedContainer = "container:SDWebImageWebPCoderExample.xcodeproj">
68+
</BuildableReference>
69+
</BuildableProductRunnable>
70+
</ProfileAction>
71+
<AnalyzeAction
72+
buildConfiguration = "Debug">
73+
</AnalyzeAction>
74+
<ArchiveAction
75+
buildConfiguration = "Release"
76+
revealArchiveInOrganizer = "YES">
77+
</ArchiveAction>
78+
</Scheme>

Diff for: Podfile

+7
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,10 @@ target 'SDWebImageWebPCoderTests' do
2222
pod 'Expecta'
2323
pod 'SDWebImageWebPCoder', :path => './'
2424
end
25+
26+
target 'SDWebImageWebPCoderTests-macOS' do
27+
platform :osx, '11.0'
28+
project test_project_path
29+
pod 'Expecta'
30+
pod 'SDWebImageWebPCoder', :path => './'
31+
end

Diff for: SDWebImageWebPCoder.xcworkspace/contents.xcworkspacedata

-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Tests/SDWebImageWebPCoderTests.m

+20
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,26 @@ - (void)testEncodingGrayscaleImage {
338338
expect(255 * b1).notTo.equal(255 * b2);
339339
}
340340

341+
- (void)testWebPEncodingWithICCProfile {
342+
// Test transcoding
343+
NSString *jpegPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"TestColorspaceBefore" ofType:@"jpeg"];
344+
NSData *jpegData = [NSData dataWithContentsOfFile:jpegPath];
345+
UIImage *jpegImage = [[UIImage alloc] initWithData:jpegData];
346+
347+
NSData *webpData = [[SDImageWebPCoder sharedCoder] encodedDataWithImage:jpegImage format:SDImageFormatWebP options:nil];
348+
// Re-decode to pick color
349+
UIImage *webpImage = [[SDImageWebPCoder sharedCoder] decodedImageWithData:webpData options:nil];
350+
CGPoint point1 = CGPointMake(310, 70);
351+
UIColor *color1 = [webpImage sd_colorAtPoint:point1];
352+
CGFloat r1;
353+
CGFloat g1;
354+
CGFloat b1;
355+
[color1 getRed:&r1 green:&g1 blue:&b1 alpha:nil];
356+
expect(255 * r1).beCloseToWithin(0, 5);
357+
expect(255 * g1).beCloseToWithin(38, 5);
358+
expect(255 * b1).beCloseToWithin(135, 5);
359+
}
360+
341361
@end
342362

343363
@implementation SDWebImageWebPCoderTests (Helpers)

0 commit comments

Comments
 (0)