This repository has been archived by the owner on Sep 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🎸 gif supported through SDWebImage (#32)
* feat: 🎸 gif supported by SDWebImage. * feat: 🎸 placeholder and failure support alse README updated * docs: ✏️ update README
- Loading branch information
1 parent
1df3912
commit ba6cf23
Showing
11 changed files
with
256 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
Sources/SAPCAI/UI/Common/UIKit/ImageViewRepresentable.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import SDWebImage | ||
import SwiftUI | ||
|
||
struct ImageViewRepresentable: UIViewRepresentable { | ||
var url: URL? | ||
var imageManager: ImageManager | ||
let wrapper = UIImageViewWrapper() | ||
|
||
func makeUIView(context: Self.Context) -> UIImageViewWrapper { | ||
self.wrapper | ||
} | ||
|
||
func updateUIView(_ uiView: UIImageViewWrapper, context: UIViewRepresentableContext<ImageViewRepresentable>) { | ||
uiView.imageView.contentMode = self.imageManager.contentMode | ||
if case ImageManager.ImageState.error = self.imageManager.state, uiView.imageView.image == nil { | ||
self.loadImage() | ||
} | ||
} | ||
|
||
func loadImage() { | ||
self.wrapper.imageView.sd_setImage(with: self.url, | ||
placeholderImage: nil) { image, error, _, _ in | ||
if let error = error { | ||
imageManager.state = .error(error) | ||
} else if let image = image { | ||
imageManager.state = .completed(image: image) | ||
} else { | ||
imageManager.state = .error(NSError(domain: "cai.com", code: 1, userInfo: nil)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
class UIImageViewWrapper: UIView { | ||
lazy var imageView: UIImageView = { | ||
let imageView = UIImageView() | ||
imageView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal) | ||
imageView.setContentCompressionResistancePriority(.defaultLow, for: .vertical) | ||
imageView.setContentHuggingPriority(.defaultLow, for: .vertical) | ||
imageView.setContentHuggingPriority(.defaultLow, for: .horizontal) | ||
return imageView | ||
}() | ||
|
||
override init(frame: CGRect) { | ||
super.init(frame: frame) | ||
addSubview(self.imageView) | ||
self.imageView.bindFrameToSuperviewBounds() | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
super.init(coder: coder) | ||
addSubview(self.imageView) | ||
self.imageView.bindFrameToSuperviewBounds() | ||
} | ||
} |
Oops, something went wrong.