Skip to content

Commit

Permalink
6 add resizemode (#7)
Browse files Browse the repository at this point in the history
* feat: wip

* feat: wip

* feat(scaleMode): wip

* feat: resize mode

* ci: comment build:ios
  • Loading branch information
duguyihou authored Aug 13, 2023
1 parent 9180cb9 commit a1c868f
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 92 deletions.
60 changes: 31 additions & 29 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,41 +103,41 @@ jobs:
build-ios:
runs-on: macos-latest
env:
TURBO_CACHE_DIR: .turbo/ios
# env:
# TURBO_CACHE_DIR: .turbo/ios
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup
uses: ./.github/actions/setup

- name: Cache turborepo for iOS
uses: actions/cache@v3
with:
path: ${{ env.TURBO_CACHE_DIR }}
key: ${{ runner.os }}-turborepo-ios-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-turborepo-ios-
- name: Check turborepo cache for iOS
run: |
TURBO_CACHE_STATUS=$(node -p "($(yarn --silent turbo run build:ios --cache-dir="${{ env.TURBO_CACHE_DIR }}" --dry=json)).tasks.find(t => t.task === 'build:ios').cache.status")
if [[ $TURBO_CACHE_STATUS == "HIT" ]]; then
echo "turbo_cache_hit=1" >> $GITHUB_ENV
fi
- name: Cache cocoapods
if: env.turbo_cache_hit != 1
id: cocoapods-cache
uses: actions/cache@v3
with:
path: |
**/ios/Pods
key: ${{ runner.os }}-cocoapods-${{ hashFiles('example/ios/Podfile.lock') }}
restore-keys: |
${{ runner.os }}-cocoapods-
# - name: Cache turborepo for iOS
# uses: actions/cache@v3
# with:
# path: ${{ env.TURBO_CACHE_DIR }}
# key: ${{ runner.os }}-turborepo-ios-${{ hashFiles('**/yarn.lock') }}
# restore-keys: |
# ${{ runner.os }}-turborepo-ios-

# - name: Check turborepo cache for iOS
# run: |
# TURBO_CACHE_STATUS=$(node -p "($(yarn --silent turbo run build:ios --cache-dir="${{ env.TURBO_CACHE_DIR }}" --dry=json)).tasks.find(t => t.task === 'build:ios').cache.status")

# if [[ $TURBO_CACHE_STATUS == "HIT" ]]; then
# echo "turbo_cache_hit=1" >> $GITHUB_ENV
# fi

# - name: Cache cocoapods
# if: env.turbo_cache_hit != 1
# id: cocoapods-cache
# uses: actions/cache@v3
# with:
# path: |
# **/ios/Pods
# key: ${{ runner.os }}-cocoapods-${{ hashFiles('example/ios/Podfile.lock') }}
# restore-keys: |
# ${{ runner.os }}-cocoapods-

- name: Install cocoapods
if: env.turbo_cache_hit != 1 && steps.cocoapods-cache.outputs.cache-hit != 'true'
Expand All @@ -148,4 +148,6 @@ jobs:

- name: Build example for iOS
run: |
yarn turbo run build:ios --cache-dir="${{ env.TURBO_CACHE_DIR }}"
# yarn turbo run build:ios --cache-dir="${{ env.TURBO_CACHE_DIR }}"
yarn turbo run build:ios
2 changes: 2 additions & 0 deletions example/ios/TurboImageExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CURRENT_PROJECT_VERSION = 1;
DEFINES_MODULE = NO;
ENABLE_BITCODE = NO;
INFOPLIST_FILE = TurboImageExample/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
Expand Down Expand Up @@ -513,6 +514,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CURRENT_PROJECT_VERSION = 1;
DEFINES_MODULE = NO;
INFOPLIST_FILE = TurboImageExample/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
Expand Down
15 changes: 10 additions & 5 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@ import { ScrollView, StyleSheet } from 'react-native';
import TurboImage from 'react-native-turbo-image';

export default function App() {
const image = { source: 'https://picsum.photos/seed/picsum/200/300' };
const images = Array(100).fill(image);
const image = { source: 'https://placedog.net/300/200?id=12' };
const images = Array(10).fill(image);
return (
<ScrollView
style={styles.container}
contentContainerStyle={styles.contentContainer}
>
{images.map((img, idx) => (
<TurboImage key={idx} source={img.source} style={styles.box} />
<TurboImage
key={idx}
source={img.source}
style={styles.box}
width={300}
height={200}
scaleMode="fill"
/>
))}
</ScrollView>
);
Expand All @@ -26,8 +33,6 @@ const styles = StyleSheet.create({
justifyContent: 'center',
},
box: {
width: 300,
height: 200,
marginVertical: 20,
},
});
49 changes: 49 additions & 0 deletions ios/TurboImage/RCTConvert+ScaleMode.swift
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
}
}

94 changes: 62 additions & 32 deletions ios/TurboImage/TurboImageView.swift
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
)
}
}
6 changes: 6 additions & 0 deletions ios/TurboImage/TurboImageViewManager.m
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

Loading

0 comments on commit a1c868f

Please sign in to comment.