parent
6940f74acd
commit
c34afb7187
@ -1,31 +1,34 @@
|
|||||||
// swift-tools-version:5.3
|
// swift-tools-version:5.3
|
||||||
|
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
// The swift-tools-version declares the minimum version of Swift required to build this package.
|
// The swift-tools-version declares the minimum version of Swift required to build this package.
|
||||||
|
|
||||||
import PackageDescription
|
import PackageDescription
|
||||||
|
|
||||||
let package = Package(
|
let package = Package(
|
||||||
name: "tauri-plugin-camera",
|
name: "tauri-plugin-camera",
|
||||||
platforms: [
|
platforms: [
|
||||||
.iOS(.v11),
|
.iOS(.v11)
|
||||||
],
|
],
|
||||||
products: [
|
products: [
|
||||||
// Products define the executables and libraries a package produces, and make them visible to other packages.
|
// Products define the executables and libraries a package produces, and make them visible to other packages.
|
||||||
.library(
|
.library(
|
||||||
name: "tauri-plugin-camera",
|
name: "tauri-plugin-camera",
|
||||||
type: .static,
|
type: .static,
|
||||||
targets: ["tauri-plugin-camera"]),
|
targets: ["tauri-plugin-camera"])
|
||||||
],
|
],
|
||||||
dependencies: [
|
dependencies: [
|
||||||
.package(name: "Tauri", path: "../.tauri/tauri-api")
|
.package(name: "Tauri", path: "../.tauri/tauri-api")
|
||||||
],
|
],
|
||||||
targets: [
|
targets: [
|
||||||
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
|
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
|
||||||
// Targets can depend on other targets in this package, and on products in packages this package depends on.
|
// Targets can depend on other targets in this package, and on products in packages this package depends on.
|
||||||
.target(
|
.target(
|
||||||
name: "tauri-plugin-camera",
|
name: "tauri-plugin-camera",
|
||||||
dependencies: [
|
dependencies: [
|
||||||
.byName(name: "Tauri")
|
.byName(name: "Tauri")
|
||||||
],
|
],
|
||||||
path: "Sources")
|
path: "Sources")
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
@ -1,105 +1,112 @@
|
|||||||
import UIKit
|
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
import Photos
|
import Photos
|
||||||
|
import UIKit
|
||||||
|
|
||||||
internal protocol CameraAuthorizationState {
|
internal protocol CameraAuthorizationState {
|
||||||
var authorizationState: String { get }
|
var authorizationState: String { get }
|
||||||
}
|
}
|
||||||
|
|
||||||
extension AVAuthorizationStatus: CameraAuthorizationState {
|
extension AVAuthorizationStatus: CameraAuthorizationState {
|
||||||
var authorizationState: String {
|
var authorizationState: String {
|
||||||
switch self {
|
switch self {
|
||||||
case .denied, .restricted:
|
case .denied, .restricted:
|
||||||
return "denied"
|
return "denied"
|
||||||
case .authorized:
|
case .authorized:
|
||||||
return "granted"
|
return "granted"
|
||||||
case .notDetermined:
|
case .notDetermined:
|
||||||
fallthrough
|
fallthrough
|
||||||
@unknown default:
|
@unknown default:
|
||||||
return "prompt"
|
return "prompt"
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension PHAuthorizationStatus: CameraAuthorizationState {
|
extension PHAuthorizationStatus: CameraAuthorizationState {
|
||||||
var authorizationState: String {
|
var authorizationState: String {
|
||||||
switch self {
|
switch self {
|
||||||
case .denied, .restricted:
|
case .denied, .restricted:
|
||||||
return "denied"
|
return "denied"
|
||||||
case .authorized:
|
case .authorized:
|
||||||
return "granted"
|
return "granted"
|
||||||
#if swift(>=5.3)
|
#if swift(>=5.3)
|
||||||
// poor proxy for Xcode 12/iOS 14, should be removed once building with Xcode 12 is required
|
// poor proxy for Xcode 12/iOS 14, should be removed once building with Xcode 12 is required
|
||||||
case .limited:
|
case .limited:
|
||||||
return "limited"
|
return "limited"
|
||||||
#endif
|
#endif
|
||||||
case .notDetermined:
|
case .notDetermined:
|
||||||
fallthrough
|
fallthrough
|
||||||
@unknown default:
|
@unknown default:
|
||||||
return "prompt"
|
return "prompt"
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal extension PHAsset {
|
extension PHAsset {
|
||||||
/**
|
/**
|
||||||
Retrieves the image metadata for the asset.
|
Retrieves the image metadata for the asset.
|
||||||
*/
|
*/
|
||||||
var imageData: [String: Any] {
|
var imageData: [String: Any] {
|
||||||
let options = PHImageRequestOptions()
|
let options = PHImageRequestOptions()
|
||||||
options.isSynchronous = true
|
options.isSynchronous = true
|
||||||
options.resizeMode = .none
|
options.resizeMode = .none
|
||||||
options.isNetworkAccessAllowed = false
|
options.isNetworkAccessAllowed = false
|
||||||
options.version = .current
|
options.version = .current
|
||||||
|
|
||||||
var result: [String: Any] = [:]
|
var result: [String: Any] = [:]
|
||||||
_ = PHCachingImageManager().requestImageDataAndOrientation(for: self, options: options) { (data, _, _, _) in
|
_ = PHCachingImageManager().requestImageDataAndOrientation(for: self, options: options) {
|
||||||
if let data = data as NSData? {
|
(data, _, _, _) in
|
||||||
let options = [kCGImageSourceShouldCache as String: kCFBooleanFalse] as CFDictionary
|
if let data = data as NSData? {
|
||||||
if let imgSrc = CGImageSourceCreateWithData(data, options),
|
let options = [kCGImageSourceShouldCache as String: kCFBooleanFalse] as CFDictionary
|
||||||
let metadata = CGImageSourceCopyPropertiesAtIndex(imgSrc, 0, options) as? [String: Any] {
|
if let imgSrc = CGImageSourceCreateWithData(data, options),
|
||||||
result = metadata
|
let metadata = CGImageSourceCopyPropertiesAtIndex(imgSrc, 0, options) as? [String: Any]
|
||||||
}
|
{
|
||||||
}
|
result = metadata
|
||||||
}
|
}
|
||||||
return result
|
}
|
||||||
}
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal extension UIImage {
|
extension UIImage {
|
||||||
/**
|
/**
|
||||||
Generates a new image from the existing one, implicitly resetting any orientation.
|
Generates a new image from the existing one, implicitly resetting any orientation.
|
||||||
Dimensions greater than 0 will resize the image while preserving the aspect ratio.
|
Dimensions greater than 0 will resize the image while preserving the aspect ratio.
|
||||||
*/
|
*/
|
||||||
func reformat(to size: CGSize? = nil) -> UIImage {
|
func reformat(to size: CGSize? = nil) -> UIImage {
|
||||||
let imageHeight = self.size.height
|
let imageHeight = self.size.height
|
||||||
let imageWidth = self.size.width
|
let imageWidth = self.size.width
|
||||||
// determine the max dimensions, 0 is treated as 'no restriction'
|
// determine the max dimensions, 0 is treated as 'no restriction'
|
||||||
var maxWidth: CGFloat
|
var maxWidth: CGFloat
|
||||||
if let size = size, size.width > 0 {
|
if let size = size, size.width > 0 {
|
||||||
maxWidth = size.width
|
maxWidth = size.width
|
||||||
} else {
|
} else {
|
||||||
maxWidth = imageWidth
|
maxWidth = imageWidth
|
||||||
}
|
}
|
||||||
let maxHeight: CGFloat
|
let maxHeight: CGFloat
|
||||||
if let size = size, size.height > 0 {
|
if let size = size, size.height > 0 {
|
||||||
maxHeight = size.height
|
maxHeight = size.height
|
||||||
} else {
|
} else {
|
||||||
maxHeight = imageHeight
|
maxHeight = imageHeight
|
||||||
}
|
}
|
||||||
// adjust to preserve aspect ratio
|
// adjust to preserve aspect ratio
|
||||||
var targetWidth = min(imageWidth, maxWidth)
|
var targetWidth = min(imageWidth, maxWidth)
|
||||||
var targetHeight = (imageHeight * targetWidth) / imageWidth
|
var targetHeight = (imageHeight * targetWidth) / imageWidth
|
||||||
if targetHeight > maxHeight {
|
if targetHeight > maxHeight {
|
||||||
targetWidth = (imageWidth * maxHeight) / imageHeight
|
targetWidth = (imageWidth * maxHeight) / imageHeight
|
||||||
targetHeight = maxHeight
|
targetHeight = maxHeight
|
||||||
}
|
}
|
||||||
// generate the new image and return
|
// generate the new image and return
|
||||||
let format: UIGraphicsImageRendererFormat = UIGraphicsImageRendererFormat.default()
|
let format: UIGraphicsImageRendererFormat = UIGraphicsImageRendererFormat.default()
|
||||||
format.scale = 1.0
|
format.scale = 1.0
|
||||||
format.opaque = false
|
format.opaque = false
|
||||||
let renderer = UIGraphicsImageRenderer(size: CGSize(width: targetWidth, height: targetHeight), format: format)
|
let renderer = UIGraphicsImageRenderer(
|
||||||
return renderer.image { (_) in
|
size: CGSize(width: targetWidth, height: targetHeight), format: format)
|
||||||
self.draw(in: CGRect(origin: .zero, size: CGSize(width: targetWidth, height: targetHeight)))
|
return renderer.image { (_) in
|
||||||
}
|
self.draw(in: CGRect(origin: .zero, size: CGSize(width: targetWidth, height: targetHeight)))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue