Skip to content

Commit

Permalink
fix: #179 Same watermark image is behaving differently on ANDROID and…
Browse files Browse the repository at this point in the history
… IOS when use given position enum

fix #179 Same watermark image is behaving differently on ANDROID and IOS when use given position enum
  • Loading branch information
JimmyDaddy authored Dec 28, 2023
1 parent bedea02 commit 39107f0
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions ios/RCTImageMarker/ImageMarker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public final class ImageMarker: NSObject, RCTBridgeModule {

func markImage(with image: UIImage, waterImages: [UIImage], options: MarkImageOptions) -> UIImage? {

var bg = image;
let bg = image;
let w = bg.size.width
let h = bg.size.height
UIGraphicsBeginImageContextWithOptions(bg.size, false, options.backgroundImage.scale)
Expand Down Expand Up @@ -323,39 +323,43 @@ public final class ImageMarker: NSObject, RCTBridgeModule {
case .topLeft:
rect = CGRect(origin: CGPoint(x: 20, y: 20), size: size)
case .topCenter:
rect = CGRect(origin: CGPoint(x: (w - size.width) / 2, y: 20), size: size)
rect = CGRect(origin: CGPoint(x: (w - ww) / 2, y: 20), size: size)
case .topRight:
rect = CGRect(origin: CGPoint(x: w - size.width - 20, y: 20), size: size)
rect = CGRect(origin: CGPoint(x: w - ww - 20, y: 20), size: size)
case .bottomLeft:
rect = CGRect(origin: CGPoint(x: 20, y: h - size.height - 20), size: size)
rect = CGRect(origin: CGPoint(x: 20, y: h - wh - 20), size: size)
case .bottomCenter:
rect = CGRect(origin: CGPoint(x: (w - size.width) / 2, y: h - size.height - 20), size: size)
rect = CGRect(origin: CGPoint(x: (w - ww) / 2, y: h - wh - 20), size: size)
case .bottomRight:
rect = CGRect(origin: CGPoint(x: w - size.width - 20, y: h - size.height - 20), size: size)
rect = CGRect(origin: CGPoint(x: w - ww - 20, y: h - wh - 20), size: size)
case .center:
rect = CGRect(origin: CGPoint(x: (w - size.width) / 2, y: (h - size.height) / 2), size: size)
rect = CGRect(origin: CGPoint(x: (w - ww) / 2, y: (h - wh) / 2), size: size)
default:
rect = CGRect(origin: CGPoint(x: 20, y: 20), size: size)
}
} else {
rect = CGRect(x: Utils.parseSpreadValue(v: watermarkOptions.X, relativeTo: w) ?? 20, y: Utils.parseSpreadValue(v: watermarkOptions.Y, relativeTo: h) ?? 20, width: CGFloat(ww), height: CGFloat(wh))
rect = CGRect(x: Utils.parseSpreadValue(v: watermarkOptions.X, relativeTo: w) ?? 20, y: Utils.parseSpreadValue(v: watermarkOptions.Y, relativeTo: h) ?? 20, width: diagonal, height: diagonal)
}

UIGraphicsBeginImageContextWithOptions(CGSize(width: diagonal, height: diagonal), false, 1)
let markerContext = UIGraphicsGetCurrentContext()
markerContext?.setFillColor(UIColor.red.cgColor)
markerContext?.fill(rect)
markerContext?.saveGState()

if watermarkOptions.imageOption.alpha != 1.0 {
markerContext?.beginTransparencyLayer(auxiliaryInfo: nil)
markerContext?.setAlpha(watermarkOptions.imageOption.alpha)
markerContext?.setBlendMode(.multiply)
let markerImage = markerImg.rotatedImageWithTransform(watermarkOptions.imageOption.rotate)
markerContext?.draw(markerImage.cgImage!, in: CGRect(origin: .zero, size: CGSize(width: diagonal, height: diagonal)))
let originPoint = CGPoint(x: 0, y: rect.height - markerImage.size.height)
markerContext?.draw(markerImage.cgImage!, in: CGRect(origin: originPoint, size: CGSize(width: markerImage.size.width, height: markerImage.size.height)))
markerContext?.endTransparencyLayer()

} else {
let markerImage = markerImg.rotatedImageWithTransform(watermarkOptions.imageOption.rotate)
markerContext?.draw(markerImage.cgImage!, in: CGRect(origin: .zero, size: CGSize(width: diagonal, height: diagonal)))
let originPoint = CGPoint(x: 0, y: rect.height - markerImage.size.height)
markerContext?.draw(markerImage.cgImage!, in: CGRect(origin: originPoint, size: CGSize(width: markerImage.size.width, height: markerImage.size.height)))
}
markerContext?.restoreGState()

Expand Down

0 comments on commit 39107f0

Please sign in to comment.