Skip to content

Commit

Permalink
35 border radius and rounded (#37)
Browse files Browse the repository at this point in the history
* feat(iOS): wip

* feat(iOS): rounded placeholder

* feat(iOS): set corner radius by placeholder's witdh
  • Loading branch information
duguyihou authored Nov 19, 2023
1 parent 6b2b993 commit 486c20f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 12 deletions.
8 changes: 6 additions & 2 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function App() {
style={styles.container}
contentContainerStyle={styles.contentContainer}
>
<Text>Turbo Image</Text>
<Text style={styles.title}>Turbo Image Example</Text>
{imageURLs.map((url, idx) => (
<TurboImage
key={idx}
Expand All @@ -28,6 +28,7 @@ export default function App() {
showActivityIndicator
fadeDuration={10}
base64Placeholder={base64Placeholder}
rounded
onSuccess={handleOnSuccess}
onError={handleOnError}
/>
Expand All @@ -45,10 +46,13 @@ const styles = StyleSheet.create({
alignItems: 'center',
justifyContent: 'center',
},
title: {
fontSize: 30,
fontWeight: 'bold',
},
box: {
marginVertical: 20,
width: 300,
height: 200,
backgroundColor: 'blue',
},
});
32 changes: 29 additions & 3 deletions ios/TurboImage/TurboImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import React

class TurboImageView : UIView {

private var placeholder: UIImage?
private var cornerRadius: CGFloat?
lazy var lazyImageView = UIImageView()
@objc var onError: RCTDirectEventBlock?
@objc var onSuccess: RCTDirectEventBlock?
Expand Down Expand Up @@ -30,13 +32,26 @@ class TurboImageView : UIView {
}
}

@objc var base64Placeholder: String?
@objc var base64Placeholder: String? {
didSet {
placeholder = UIImage(base64Placeholder: base64Placeholder)
}
}

@objc var fadeDuration: NSNumber = 0.5

@objc var rounded: Bool = false {
didSet {
guard let width = placeholder?.size.width else { return }
cornerRadius = width
placeholder = placeholder?.roundedCorner(with: width)
}
}

override init(frame: CGRect) {
super.init(frame: frame)
addSubview(lazyImageView)
layer.masksToBounds = true
lazyImageView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
lazyImageView.topAnchor.constraint(equalTo: topAnchor),
Expand All @@ -61,15 +76,26 @@ fileprivate extension TurboImageView {
guard let url = URL(string: url!)
else { return }

let processor = composeProcessor(rounded)

KF.url(url)
.fade(duration: TimeInterval(truncating: fadeDuration))
.placeholder(UIImage(base64Placeholder: base64Placeholder))
.onSuccess({ _ in
.placeholder(placeholder)
.setProcessor(processor)
.onSuccess({ result in
self.onSuccess?(["result": "success"])
})
.onFailure({ error in
self.onError?(["error": error.localizedDescription])
})
.set(to: lazyImageView)
}

func composeProcessor(_ rounded: Bool?) -> ImageProcessor {
var processor: ImageProcessor = DefaultImageProcessor.default
if rounded ?? false && (cornerRadius != nil) {
processor = processor |> RoundCornerImageProcessor(cornerRadius: cornerRadius!)
}
return processor
}
}
2 changes: 2 additions & 0 deletions ios/TurboImage/TurboImageViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ @interface RCT_EXTERN_MODULE(TurboImageViewManager, RCTViewManager)

RCT_EXPORT_VIEW_PROPERTY(fadeDuration, NSNumber)

RCT_EXPORT_VIEW_PROPERTY(rounded, BOOL)

RCT_EXPORT_VIEW_PROPERTY(onSuccess, RCTDirectEventBlock)

RCT_EXPORT_VIEW_PROPERTY(onError, RCTDirectEventBlock)
Expand Down
33 changes: 26 additions & 7 deletions ios/TurboImage/UIImage+Extensions.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
import UIKit

public extension UIImage {

convenience init?(base64Placeholder: String?) {
guard
let base64Placeholder,
let data = Data(base64Encoded: base64Placeholder, options: .ignoreUnknownCharacters)
else { return nil }
self.init(data: data)

convenience init?(base64Placeholder: String?) {
guard
let base64Placeholder,
let data = Data(base64Encoded: base64Placeholder, options: .ignoreUnknownCharacters)
else { return nil }
self.init(data: data)
}

func roundedCorner(with radius: CGFloat) -> UIImage {
let format = UIGraphicsImageRendererFormat()
format.scale = scale
let renderer = UIGraphicsImageRenderer(size: size, format: format)
return renderer.image { rendererContext in
let rect = CGRect(origin: .zero, size: size)
let path = UIBezierPath(roundedRect: rect,
byRoundingCorners: .allCorners,
cornerRadii: CGSize(width: radius, height: radius))
path.close()

let cgContext = rendererContext.cgContext
cgContext.saveGState()
path.addClip()
draw(in: rect)
cgContext.restoreGState()
}
}
}
1 change: 1 addition & 0 deletions src/TurboImage.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface TurboImageProps extends AccessibilityProps, ViewProps {
showActivityIndicator?: boolean;
base64Placeholder?: string;
fadeDuration?: number;
rounded?: boolean;
onSuccess?: () => void;
onError?: (error: any) => void;
ref?: React.Ref<any>;
Expand Down

0 comments on commit 486c20f

Please sign in to comment.