@@ -86,10 +86,14 @@ public final class ImageMarker: NSObject, RCTBridgeModule {
8686 }
8787
8888 func markerImgWithText( _ image: UIImage , _ opts: MarkTextOptions ) -> UIImage ? {
89- let w = Int ( image. size. width)
90- let h = Int ( image. size. height)
89+ var bg = image;
90+ if ( opts. backgroundImage. scale > 0 ) {
91+ bg = UIImage ( cgImage: image. cgImage!, scale: 1 / opts. backgroundImage. scale, orientation: image. imageOrientation)
92+ }
9193
92- UIGraphicsBeginImageContextWithOptions ( image. size, false , opts. backgroundImage. scale)
94+ let w = Int ( bg. size. width)
95+ let h = Int ( bg. size. height)
96+ UIGraphicsBeginImageContextWithOptions ( bg. size, false , 1 / opts. backgroundImage. scale)
9397
9498 guard let context = UIGraphicsGetCurrentContext ( ) else {
9599 return nil
@@ -105,11 +109,11 @@ public final class ImageMarker: NSObject, RCTBridgeModule {
105109 context. beginTransparencyLayer ( auxiliaryInfo: nil )
106110 context. setAlpha ( opts. backgroundImage. alpha)
107111 context. setBlendMode ( . multiply)
108- context. draw ( image . cgImage!, in: canvasRect)
112+ context. draw ( bg . cgImage!, in: canvasRect)
109113 context. endTransparencyLayer ( )
110114 context. setBlendMode ( . normal)
111115 } else {
112- context. draw ( image . cgImage!, in: canvasRect)
116+ context. draw ( bg . cgImage!, in: canvasRect)
113117 }
114118 context. restoreGState ( )
115119
@@ -228,17 +232,29 @@ public final class ImageMarker: NSObject, RCTBridgeModule {
228232 }
229233
230234 func markeImage( with image: UIImage , waterImage: UIImage , options: MarkImageOptions ) -> UIImage ? {
231- let w = Int ( image. size. width)
232- let h = Int ( image. size. height)
235+
236+ var bg = image;
237+ if ( options. backgroundImage. scale > 0 ) {
238+ bg = UIImage ( cgImage: image. cgImage!, scale: 1 / options. backgroundImage. scale, orientation: image. imageOrientation)
239+ }
240+
241+ let w = Int ( bg. size. width)
242+ let h = Int ( bg. size. height)
243+ UIGraphicsBeginImageContextWithOptions ( bg. size, false , 1 / options. backgroundImage. scale)
244+
245+ var marker = waterImage;
246+ if ( options. watermarkImage. scale > 0 ) {
247+ marker = UIImage ( cgImage: waterImage. cgImage!, scale: 1 / options. watermarkImage. scale, orientation: waterImage. imageOrientation)
248+ }
233249
234- let ww = waterImage . size. width * options . watermarkImage . scale
235- let wh = waterImage . size. height * options . watermarkImage . scale
250+ let ww = marker . size. width
251+ let wh = marker . size. height
236252
237253 let diagonal = sqrt ( pow ( ww, 2 ) + pow( ww, 2 ) ) // 计算对角线长度
238254
239255 let canvasRect = CGRect ( x: 0 , y: 0 , width: CGFloat ( w) , height: CGFloat ( h) )
240256
241- UIGraphicsBeginImageContextWithOptions ( image . size, false , options. backgroundImage. scale)
257+ UIGraphicsBeginImageContextWithOptions ( bg . size, false , options. backgroundImage. scale)
242258
243259 let transform = CGAffineTransform ( translationX: 0 , y: canvasRect. height)
244260 . scaledBy ( x: 1 , y: - 1 )
@@ -253,15 +269,15 @@ public final class ImageMarker: NSObject, RCTBridgeModule {
253269 context? . setAlpha ( options. backgroundImage. alpha)
254270 context? . setBlendMode ( . multiply)
255271
256- context? . draw ( image . cgImage!, in: canvasRect)
272+ context? . draw ( bg . cgImage!, in: canvasRect)
257273 context? . endTransparencyLayer ( )
258274 context? . setBlendMode ( . normal)
259275 context? . restoreGState ( )
260276 } else {
261277 context = UIGraphicsGetCurrentContext ( )
262278 context? . saveGState ( )
263279 context? . concatenate ( transform)
264- context? . draw ( image . cgImage!, in: canvasRect)
280+ context? . draw ( bg . cgImage!, in: canvasRect)
265281 context? . restoreGState ( )
266282 }
267283
0 commit comments