Skip to content

Commit

Permalink
feat: Add saveToFile(path)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Jul 19, 2024
1 parent bbaffa2 commit d784f1d
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 9 deletions.
18 changes: 13 additions & 5 deletions packages/react-native-nitro-image/ios/Image.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ import NitroModules

public class Image : ImageSpec {
private let uiImage: UIImage

public init(uiImage: UIImage) {
self.uiImage = uiImage
}

public var width: Int {
return Int(uiImage.size.width)
}

public var height: Int {
return Int(uiImage.size.height)
}

public var pixelFormat: PixelFormat {
return .rgb
}

public func toArrayBuffer(format: ImageFormat) throws -> Data {
switch format {
case .jpg:
Expand All @@ -42,4 +42,12 @@ public class Image : ImageSpec {
return data
}
}

public func saveToFile(path: String) throws {
let data = try toArrayBuffer(format: .jpg)
guard let url = URL(string: path) else {
throw RuntimeError.error(withMessage: "Path \"\(path)\" is not a valid URL!")
}
try data.write(to: url)
}
}
1 change: 1 addition & 0 deletions packages/react-native-nitro-image/ios/ImageSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public protocol ImageSpec {
var pixelFormat: PixelFormat { get }

func toArrayBuffer(format: ImageFormat) throws -> Data
func saveToFile(path: String) throws
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
///
/// HybridImage.cpp
/// Thu Jul 18 2024
/// Fri Jul 19 2024
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
/// https://github.com/mrousavy/react-native-nitro
/// Copyright © 2024 Marc Rousavy @ Margelo
Expand All @@ -17,4 +17,5 @@ void HybridImage::loadHybridMethods() {
registerHybridGetter("height", &HybridImage::getHeight, this);
registerHybridGetter("pixelFormat", &HybridImage::getPixelFormat, this);
registerHybridMethod("toArrayBuffer", &HybridImage::toArrayBuffer, this);
registerHybridMethod("saveToFile", &HybridImage::saveToFile, this);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
///
/// HybridImage.hpp
/// Thu Jul 18 2024
/// Fri Jul 19 2024
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
/// https://github.com/mrousavy/react-native-nitro
/// Copyright © 2024 Marc Rousavy @ Margelo
Expand Down Expand Up @@ -44,6 +44,7 @@ class HybridImage: public HybridObject {
public:
// Methods
virtual std::shared_ptr<ArrayBuffer> toArrayBuffer(ImageFormat format) = 0;
virtual std::future<void> saveToFile(const std::string& path) = 0;

protected:
// Tag for logging
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
///
/// ImageFormat.hpp
/// Thu Jul 18 2024
/// Fri Jul 19 2024
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
/// https://github.com/mrousavy/react-native-nitro
/// Copyright © 2024 Marc Rousavy @ Margelo
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
///
/// PixelFormat.hpp
/// Thu Jul 18 2024
/// Fri Jul 19 2024
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
/// https://github.com/mrousavy/react-native-nitro
/// Copyright © 2024 Marc Rousavy @ Margelo
Expand Down

0 comments on commit d784f1d

Please sign in to comment.