Skip to content

Using the wrapper on watchOS AnimatedImage as well, try to solve the SwiftUI bug of aspectRatio #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions SDWebImageSwiftUI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@
326E480B23431C0F00C633E9 /* ImageViewWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 326E480923431C0F00C633E9 /* ImageViewWrapper.swift */; };
326E480C23431C0F00C633E9 /* ImageViewWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 326E480923431C0F00C633E9 /* ImageViewWrapper.swift */; };
326E480D23431C0F00C633E9 /* ImageViewWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 326E480923431C0F00C633E9 /* ImageViewWrapper.swift */; };
3276EB00237760D800B385D4 /* SDAnimatedImageInterfaceWrapper.h in Headers */ = {isa = PBXBuildFile; fileRef = 3276EAFE237760D800B385D4 /* SDAnimatedImageInterfaceWrapper.h */; settings = {ATTRIBUTES = (Public, ); }; };
3276EB01237760D800B385D4 /* SDAnimatedImageInterfaceWrapper.h in Headers */ = {isa = PBXBuildFile; fileRef = 3276EAFE237760D800B385D4 /* SDAnimatedImageInterfaceWrapper.h */; settings = {ATTRIBUTES = (Public, ); }; };
3276EB02237760D800B385D4 /* SDAnimatedImageInterfaceWrapper.h in Headers */ = {isa = PBXBuildFile; fileRef = 3276EAFE237760D800B385D4 /* SDAnimatedImageInterfaceWrapper.h */; settings = {ATTRIBUTES = (Public, ); }; };
3276EB03237760D800B385D4 /* SDAnimatedImageInterfaceWrapper.h in Headers */ = {isa = PBXBuildFile; fileRef = 3276EAFE237760D800B385D4 /* SDAnimatedImageInterfaceWrapper.h */; settings = {ATTRIBUTES = (Public, ); }; };
3276EB04237760D800B385D4 /* SDAnimatedImageInterfaceWrapper.m in Sources */ = {isa = PBXBuildFile; fileRef = 3276EAFF237760D800B385D4 /* SDAnimatedImageInterfaceWrapper.m */; };
3276EB05237760D800B385D4 /* SDAnimatedImageInterfaceWrapper.m in Sources */ = {isa = PBXBuildFile; fileRef = 3276EAFF237760D800B385D4 /* SDAnimatedImageInterfaceWrapper.m */; };
3276EB06237760D800B385D4 /* SDAnimatedImageInterfaceWrapper.m in Sources */ = {isa = PBXBuildFile; fileRef = 3276EAFF237760D800B385D4 /* SDAnimatedImageInterfaceWrapper.m */; };
3276EB07237760D800B385D4 /* SDAnimatedImageInterfaceWrapper.m in Sources */ = {isa = PBXBuildFile; fileRef = 3276EAFF237760D800B385D4 /* SDAnimatedImageInterfaceWrapper.m */; };
32B933E523659A1900BB7CAD /* Transition.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32B933E423659A1900BB7CAD /* Transition.swift */; };
32B933E623659A1900BB7CAD /* Transition.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32B933E423659A1900BB7CAD /* Transition.swift */; };
32B933E723659A1900BB7CAD /* Transition.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32B933E423659A1900BB7CAD /* Transition.swift */; };
Expand Down Expand Up @@ -119,6 +127,8 @@
326B8486236335110011BDFB /* ActivityIndicator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ActivityIndicator.swift; sourceTree = "<group>"; };
326B848B236335400011BDFB /* ProgressIndicator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ProgressIndicator.swift; sourceTree = "<group>"; };
326E480923431C0F00C633E9 /* ImageViewWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageViewWrapper.swift; sourceTree = "<group>"; };
3276EAFE237760D800B385D4 /* SDAnimatedImageInterfaceWrapper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SDAnimatedImageInterfaceWrapper.h; sourceTree = "<group>"; };
3276EAFF237760D800B385D4 /* SDAnimatedImageInterfaceWrapper.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SDAnimatedImageInterfaceWrapper.m; sourceTree = "<group>"; };
32B933E423659A1900BB7CAD /* Transition.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Transition.swift; sourceTree = "<group>"; };
32C43DCC22FD540D00BE87F5 /* SDWebImageSwiftUI.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SDWebImageSwiftUI.framework; sourceTree = BUILT_PRODUCTS_DIR; };
32C43DDC22FD54C600BE87F5 /* ImageManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageManager.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -177,6 +187,8 @@
children = (
324F61C5235E07EC003973B8 /* SDAnimatedImageInterface.h */,
324F61C6235E07EC003973B8 /* SDAnimatedImageInterface.m */,
3276EAFE237760D800B385D4 /* SDAnimatedImageInterfaceWrapper.h */,
3276EAFF237760D800B385D4 /* SDAnimatedImageInterfaceWrapper.m */,
);
path = ObjC;
sourceTree = "<group>";
Expand Down Expand Up @@ -262,6 +274,7 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
3276EB00237760D800B385D4 /* SDAnimatedImageInterfaceWrapper.h in Headers */,
324F61C7235E07EC003973B8 /* SDAnimatedImageInterface.h in Headers */,
32C43DE622FD54CD00BE87F5 /* SDWebImageSwiftUI.h in Headers */,
);
Expand All @@ -271,6 +284,7 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
3276EB01237760D800B385D4 /* SDAnimatedImageInterfaceWrapper.h in Headers */,
324F61C8235E07EC003973B8 /* SDAnimatedImageInterface.h in Headers */,
32C43E2222FD583A00BE87F5 /* SDWebImageSwiftUI.h in Headers */,
);
Expand All @@ -280,6 +294,7 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
3276EB02237760D800B385D4 /* SDAnimatedImageInterfaceWrapper.h in Headers */,
324F61C9235E07EC003973B8 /* SDAnimatedImageInterface.h in Headers */,
32C43E2322FD583B00BE87F5 /* SDWebImageSwiftUI.h in Headers */,
);
Expand All @@ -289,6 +304,7 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
3276EB03237760D800B385D4 /* SDAnimatedImageInterfaceWrapper.h in Headers */,
324F61CA235E07EC003973B8 /* SDAnimatedImageInterface.h in Headers */,
32C43E2422FD583C00BE87F5 /* SDWebImageSwiftUI.h in Headers */,
);
Expand Down Expand Up @@ -457,6 +473,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
3276EB04237760D800B385D4 /* SDAnimatedImageInterfaceWrapper.m in Sources */,
32B933E523659A1900BB7CAD /* Transition.swift in Sources */,
32C43E1722FD583700BE87F5 /* WebImage.swift in Sources */,
326B848C236335400011BDFB /* ProgressIndicator.swift in Sources */,
Expand All @@ -474,6 +491,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
3276EB05237760D800B385D4 /* SDAnimatedImageInterfaceWrapper.m in Sources */,
32B933E623659A1900BB7CAD /* Transition.swift in Sources */,
32C43E1A22FD583700BE87F5 /* WebImage.swift in Sources */,
326B848D236335400011BDFB /* ProgressIndicator.swift in Sources */,
Expand All @@ -491,6 +509,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
3276EB06237760D800B385D4 /* SDAnimatedImageInterfaceWrapper.m in Sources */,
32B933E723659A1900BB7CAD /* Transition.swift in Sources */,
32C43E1D22FD583800BE87F5 /* WebImage.swift in Sources */,
326B848E236335400011BDFB /* ProgressIndicator.swift in Sources */,
Expand All @@ -508,6 +527,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
3276EB07237760D800B385D4 /* SDAnimatedImageInterfaceWrapper.m in Sources */,
32B933E823659A1900BB7CAD /* Transition.swift in Sources */,
32C43E2022FD583800BE87F5 /* WebImage.swift in Sources */,
326B848F236335400011BDFB /* ProgressIndicator.swift in Sources */,
Expand Down
25 changes: 5 additions & 20 deletions SDWebImageSwiftUI/Classes/AnimatedImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ import SDWebImageSwiftUIObjC

// Convenient
#if os(watchOS)
public typealias AnimatedImageViewWrapper = SDAnimatedImageInterface
extension SDAnimatedImageInterface {
var wrapped: SDAnimatedImageInterface {
return self
}
}
public typealias AnimatedImageViewWrapper = SDAnimatedImageInterfaceWrapper
#endif

// Coordinator Life Cycle Binding Object
Expand Down Expand Up @@ -122,7 +117,7 @@ public struct AnimatedImage : PlatformViewRepresentable {
#else
let image = SDAnimatedImage(named: name, in: bundle, compatibleWith: nil)
#endif
self.image = image
_image = .init(wrappedValue: image)
}

/// Create an animated image with data and scale.
Expand All @@ -139,7 +134,7 @@ public struct AnimatedImage : PlatformViewRepresentable {
public init(data: Data, scale: CGFloat = 0, isAnimating: Binding<Bool>) {
self._isAnimating = isAnimating
let image = SDAnimatedImage(data: data, scale: scale)
self.image = image
_image = .init(wrappedValue: image)
}

#if os(macOS)
Expand Down Expand Up @@ -432,6 +427,8 @@ public struct AnimatedImage : PlatformViewRepresentable {
// Antialiased
view.shouldAntialias = self.antialiased
#endif

view.invalidateIntrinsicContentSize()
}

func configureView(_ view: AnimatedImageViewWrapper, context: Context) {
Expand Down Expand Up @@ -562,19 +559,7 @@ extension AnimatedImage {
var result = self
result.aspectRatio = aspectRatio
result.contentMode = contentMode
#if os(macOS) || os(iOS) || os(tvOS)
return result.modifier(EmptyModifier()).aspectRatio(aspectRatio, contentMode: contentMode)
#else
return Group {
if aspectRatio != nil {
result.modifier(EmptyModifier()).aspectRatio(aspectRatio, contentMode: contentMode)
} else {
// on watchOS, there are no workaround like `AnimatedImageViewWrapper` to override `intrinsicContentSize`, so the aspect ratio is undetermined and cause sizing issues
// To workaround, we do not call default implementation for this case, using original solution instead
result
}
}
#endif
}

/// Constrains this view's dimensions to the aspect ratio of the given size.
Expand Down
1 change: 1 addition & 0 deletions SDWebImageSwiftUI/Classes/ObjC/SDAnimatedImageInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
NS_ASSUME_NONNULL_BEGIN

/// Do not use this class directly in WatchKit or Storyboard. This class is implementation detail and will be removed in the future.
/// This is not public API at all.
@interface SDAnimatedImageInterface : WKInterfaceImage

@property (nonatomic, assign, getter=isAnimating, readonly) BOOL animating;
Expand Down
36 changes: 14 additions & 22 deletions SDWebImageSwiftUI/Classes/ObjC/SDAnimatedImageInterface.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,6 @@

#pragma mark - SPI

static UIImage * SharedEmptyImage(void) {
// This is used for placeholder on `WKInterfaceImage`
// Do not using `[UIImage new]` because WatchKit will ignore it
static dispatch_once_t onceToken;
static UIImage *image;
dispatch_once(&onceToken, ^{
UIColor *color = UIColor.clearColor;
CGRect rect = WKInterfaceDevice.currentDevice.screenBounds;
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
});
return image;
}

@protocol CALayerProtocol <NSObject>
@property (nullable, strong) id contents;
@property CGFloat contentsScale;
Expand All @@ -43,6 +25,16 @@ @protocol UIViewProtocol <NSObject>
@property (nonatomic) CGFloat alpha;
@property (nonatomic, getter=isHidden) BOOL hidden;
@property (nonatomic, getter=isOpaque) BOOL opaque;
@property (nonatomic) CGRect frame;
@property (nonatomic) CGRect bounds;
@property (nonatomic) CGPoint center;
@property (nonatomic, readonly) CGSize intrinsicContentSize;
@property(nonatomic) NSInteger tag;

- (void)invalidateIntrinsicContentSize;
- (void)layoutSubviews;
- (CGSize)sizeThatFits:(CGSize)size;
- (void)sizeToFit;

@end

Expand All @@ -59,8 +51,9 @@ @interface WKInterfaceObject ()

// This is needed for dynamic created WKInterfaceObject, like `WKInterfaceMap`
- (instancetype)_initForDynamicCreationWithInterfaceProperty:(NSString *)property;
- (NSDictionary *)interfaceDescriptionForDynamicCreation;
// This is remote UIView
@property (nonatomic, strong, readonly) id<UIImageViewProtocol> _interfaceView;
@property (nonatomic, strong, readwrite) id<UIViewProtocol> _interfaceView;

@end

Expand Down Expand Up @@ -97,7 +90,6 @@ - (NSDictionary *)interfaceDescriptionForDynamicCreation {
return @{
@"type" : @"image",
@"property" : self.interfaceProperty,
@"image" : SharedEmptyImage()
};
}

Expand All @@ -113,8 +105,7 @@ - (void)setImage:(UIImage *)image {
self.currentFrameIndex = 0;
self.currentLoopCount = 0;

[super setImage:image];
[self _interfaceView].image = image;
((id<UIImageViewProtocol>)[self _interfaceView]).image = image;
if ([image.class conformsToProtocol:@protocol(SDAnimatedImage)]) {
// Create animted player
self.player = [SDAnimatedImagePlayer playerWithProvider:(id<SDAnimatedImage>)image];
Expand Down Expand Up @@ -258,4 +249,5 @@ - (void)sd_setImageWithURL:(nullable NSURL *)url
}

@end

#endif
27 changes: 27 additions & 0 deletions SDWebImageSwiftUI/Classes/ObjC/SDAnimatedImageInterfaceWrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* This file is part of the SDWebImage package.
* (c) DreamPiggy <lizhuoli1126@126.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

#import "SDAnimatedImageInterface.h"

#if SD_WATCH
NS_ASSUME_NONNULL_BEGIN

/// Do not use this class directly in WatchKit or Storyboard. This class is implementation detail and will be removed in the future.
/// This is not public API at all.
@interface SDAnimatedImageInterfaceWrapper : WKInterfaceGroup

@property (nonatomic, strong, nonnull) SDAnimatedImageInterface *wrapped;

- (instancetype)init WK_AVAILABLE_WATCHOS_ONLY(6.0);

- (void)invalidateIntrinsicContentSize;

@end

NS_ASSUME_NONNULL_END
#endif
Loading