react-native-picture-selector 1.0.28 → 1.0.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  import Foundation
2
2
  import UIKit
3
+ import AVFoundation
3
4
  import HXPhotoPicker
4
5
  import NitroModules
5
6
 
@@ -112,11 +113,7 @@ final class HybridPictureSelector: HybridHybridPictureSelectorSpec_base, HybridH
112
113
  }
113
114
  Task {
114
115
  do {
115
- let asset = try await self.mapAsset(
116
- result.photoAsset,
117
- compress: options.compress,
118
- isOriginal: false
119
- )
116
+ let asset = try await self.mapCameraResult(result, compress: options.compress)
120
117
  promise.resolve(withResult: [asset])
121
118
  } catch {
122
119
  promise.reject(withError: error)
@@ -259,6 +256,58 @@ final class HybridPictureSelector: HybridHybridPictureSelectorSpec_base, HybridH
259
256
  )
260
257
  }
261
258
 
259
+ // MARK: - Camera result mapping
260
+
261
+ private func mapCameraResult(
262
+ _ result: CameraController.Result,
263
+ compress: CompressOptions?
264
+ ) async throws -> MediaAsset {
265
+ switch result {
266
+ case .image(let image):
267
+ let quality = compress?.quality ?? 0.8
268
+ guard let data = image.jpegData(compressionQuality: quality) else {
269
+ throw PictureSelectorError.unknown("Failed to encode captured image")
270
+ }
271
+ let fileName = "camera_\(UUID().uuidString).jpg"
272
+ let tempURL = FileManager.default.temporaryDirectory.appendingPathComponent(fileName)
273
+ try data.write(to: tempURL)
274
+ let fileSize = Double(data.count)
275
+ return MediaAsset(
276
+ uri: tempURL.absoluteString,
277
+ type: "image",
278
+ mimeType: "image/jpeg",
279
+ width: Double(image.size.width * image.scale),
280
+ height: Double(image.size.height * image.scale),
281
+ duration: 0,
282
+ fileName: fileName,
283
+ fileSize: fileSize,
284
+ editedUri: nil,
285
+ isOriginal: false,
286
+ bucketName: nil
287
+ )
288
+ case .video(let url):
289
+ let attrs = try? FileManager.default.attributesOfItem(atPath: url.path)
290
+ let fileSize = Double((attrs?[.size] as? UInt64) ?? 0)
291
+ let asset = AVURLAsset(url: url)
292
+ let duration = CMTimeGetSeconds(asset.duration) * 1_000
293
+ let tracks = asset.tracks(withMediaType: .video)
294
+ let size = tracks.first?.naturalSize ?? .zero
295
+ return MediaAsset(
296
+ uri: url.absoluteString,
297
+ type: "video",
298
+ mimeType: mimeType(for: url),
299
+ width: Double(size.width),
300
+ height: Double(size.height),
301
+ duration: duration,
302
+ fileName: url.lastPathComponent,
303
+ fileSize: fileSize,
304
+ editedUri: nil,
305
+ isOriginal: false,
306
+ bucketName: nil
307
+ )
308
+ }
309
+ }
310
+
262
311
  // MARK: - Compression helper
263
312
 
264
313
  private func buildCompression(from options: CompressOptions?) -> PhotoAsset.Compression? {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-picture-selector",
3
- "version": "1.0.28",
3
+ "version": "1.0.29",
4
4
  "description": "High-performance photo/video picker for React Native using Nitro Modules",
5
5
  "main": "lib/commonjs/index.js",
6
6
  "module": "lib/module/index.js",