-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: wip * feat: wip * feat(scaleMode): wip * feat: resize mode * ci: comment build:ios
- Loading branch information
Showing
7 changed files
with
203 additions
and
92 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import Foundation | ||
import React | ||
import Kingfisher | ||
|
||
@objc | ||
enum ScaleMode: Int { | ||
|
||
/// Not scale the content. | ||
case none | ||
|
||
/// Scales the content to fit the size of the view by maintaining the aspect ratio. | ||
case aspectFit | ||
|
||
/// Scales the content to fill the size of the view. | ||
case aspectFill | ||
} | ||
|
||
extension ScaleMode { | ||
|
||
static func mapContentMode(by scaleMode: ScaleMode) -> ContentMode { | ||
var contentMode: Kingfisher.ContentMode | ||
switch scaleMode.rawValue { | ||
case 1: | ||
contentMode = .aspectFit | ||
case 2: | ||
contentMode = .aspectFill | ||
default: | ||
contentMode = .none | ||
} | ||
return contentMode | ||
} | ||
} | ||
|
||
|
||
extension RCTConvert { | ||
@objc(ScaleMode:) | ||
static func scaleMode(_ value: Any) -> ScaleMode { | ||
let ScaleModeMap: [String: ScaleMode] = [ | ||
"fill": .aspectFill, | ||
"fit": .aspectFit | ||
] | ||
|
||
guard let value = value as? String, | ||
let mv = ScaleModeMap[value] | ||
else { return .none } | ||
return mv | ||
} | ||
} | ||
|
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 |
---|---|---|
@@ -1,59 +1,89 @@ | ||
import Kingfisher | ||
import React | ||
|
||
class TurboImageView : UIView { | ||
|
||
var image: UIImage? | ||
@objc var color: String = "" { | ||
var imageView: UIImageView? | ||
private var needsReload: Bool = false | ||
|
||
var width: CGFloat? { | ||
didSet { | ||
guard let width = width else { return } | ||
imageView?.frame.size.width = width | ||
} | ||
} | ||
var height: CGFloat? { | ||
didSet { | ||
guard let height = height else { return } | ||
imageView?.frame.size.height = height | ||
} | ||
} | ||
|
||
@objc var source: String? { | ||
didSet { | ||
self.backgroundColor = hexStringToUIColor(hexColor: color) | ||
needsReload = true | ||
} | ||
} | ||
|
||
@objc var source: String? | ||
var scaleMode: ScaleMode? | ||
|
||
override init(frame: CGRect) { | ||
super.init(frame: frame) | ||
clipsToBounds = true | ||
imageView = UIImageView() | ||
addSubview(imageView!) | ||
} | ||
|
||
@objc | ||
func setHeight(_ height: CGFloat) { | ||
self.height = height | ||
} | ||
|
||
@objc | ||
func setWidth(_ width: CGFloat) { | ||
self.width = width | ||
} | ||
|
||
@objc | ||
func setScaleMode(_ scaleMode: ScaleMode) { | ||
self.scaleMode = scaleMode | ||
} | ||
|
||
override func didSetProps(_ changedProps: [String]!) { | ||
reloadImage() | ||
if needsReload { | ||
loadImage(with: source) | ||
} | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
} | ||
|
||
extension TurboImageView { | ||
|
||
private func reloadImage() { | ||
guard let url = URL(string: source!) else { return } | ||
let resource: KF.ImageResource = KF.ImageResource(downloadURL: url) | ||
KingfisherManager.shared.retrieveImage(with: resource) { [self] result in | ||
switch result { | ||
case .success(let response): | ||
image = response.image | ||
let imageView = UIImageView(image: image) | ||
addSubview(imageView) | ||
case .failure(let error): | ||
print("🐵 ---- error \(error)") // TODO: 🐵 handle error | ||
} | ||
} | ||
} | ||
|
||
func hexStringToUIColor(hexColor: String) -> UIColor { | ||
let stringScanner = Scanner(string: hexColor) | ||
|
||
if(hexColor.hasPrefix("#")) { | ||
stringScanner.scanLocation = 1 | ||
} | ||
var color: UInt32 = 0 | ||
stringScanner.scanHexInt32(&color) | ||
|
||
let r = CGFloat(Int(color >> 16) & 0x000000FF) | ||
let g = CGFloat(Int(color >> 8) & 0x000000FF) | ||
let b = CGFloat(Int(color) & 0x000000FF) | ||
func loadImage(with source: String?) { | ||
guard let source = source, | ||
let url = URL(string: source), | ||
let width = width, | ||
let height = height, | ||
let scaleMode = scaleMode | ||
else { return } | ||
|
||
return UIColor(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: 1) | ||
let resource: KF.ImageResource = KF.ImageResource(downloadURL: url) | ||
let scale = UIScreen.main.scale | ||
let contentMode = ScaleMode.mapContentMode(by: scaleMode) | ||
let referenceSize = CGSize(width: width * scale, height: height * scale) | ||
let processor = ResizingImageProcessor(referenceSize: referenceSize, | ||
mode: contentMode) | ||
let options: KingfisherOptionsInfo = [.processor(processor)] | ||
imageView?.kf.indicatorType = .activity | ||
imageView?.kf.setImage(with: resource, | ||
placeholder: nil, | ||
options: options, | ||
progressBlock: nil | ||
) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,8 +1,14 @@ | ||
#import <React/RCTViewManager.h> | ||
#import "ReactNativeTurboImage-umbrella.h" | ||
//#import <ReactNativeTurboImage/ReactNativeTurboImage-Swift.h> | ||
|
||
@interface RCT_EXTERN_MODULE(TurboImageViewManager, RCTViewManager) | ||
|
||
RCT_EXPORT_VIEW_PROPERTY(color, NSString) | ||
RCT_EXPORT_VIEW_PROPERTY(source, NSString) | ||
RCT_EXPORT_VIEW_PROPERTY(width, double) | ||
RCT_EXPORT_VIEW_PROPERTY(height, double) | ||
RCT_EXPORT_VIEW_PROPERTY(scaleMode, ScaleMode) | ||
|
||
@end | ||
|
Oops, something went wrong.