mirror of
https://github.com/laosb/CropImage.git
synced 2025-04-30 15:41:08 +00:00
doc: Basic documentation.
This commit is contained in:
parent
45c2b0ac58
commit
574b546c9c
3 changed files with 61 additions and 9 deletions
|
@ -10,15 +10,36 @@ import SwiftUI
|
|||
import UIKit
|
||||
#endif
|
||||
|
||||
/// A view that allows the user to crop an image.
|
||||
public struct CropImageView: View {
|
||||
public enum RenderError: Error {
|
||||
/// Errors that could happen during the cropping process.
|
||||
public enum CropError: Error {
|
||||
/// SwiftUI `ImageRenderer` returned nil when calling `nsImage` or `uiImage`.
|
||||
///
|
||||
/// See [SwiftUI - ImageRenderer](https://developer.apple.com/documentation/swiftui/imagerenderer) for more information.
|
||||
case imageRendererReturnedNil
|
||||
/// `UIGraphicsGetCurrentContext()` call returned `nil`.
|
||||
///
|
||||
/// It shouldn't happen, but if it does it will only be on iOS versions prior to 16.0.
|
||||
case failedToGetCurrentUIGraphicsContext
|
||||
/// `UIGraphicsGetImageFromCurrentImageContext()` call returned `nil`.
|
||||
///
|
||||
/// It shouldn't happen, but if it does it will only be on iOS versions prior to 16.0.
|
||||
case failedToGetImageFromCurrentUIGraphicsImageContext
|
||||
}
|
||||
|
||||
var image: PlatformImage
|
||||
var targetSize: CGSize
|
||||
var targetScale: CGFloat = 1
|
||||
var onCrop: (Result<PlatformImage, Error>) -> Void
|
||||
/// The image to crop.
|
||||
public var image: PlatformImage
|
||||
/// The intended size of the cropped image, in points.
|
||||
public var targetSize: CGSize
|
||||
/// The intended scale of the cropped image.
|
||||
///
|
||||
/// This defines the point to pixel ratio for the output image. Defaults to `1`.
|
||||
public var targetScale: CGFloat = 1
|
||||
/// A closure that will be called when the user finishes cropping.
|
||||
///
|
||||
/// The error should be a ``CropError``.
|
||||
public var onCrop: (Result<PlatformImage, Error>) -> Void
|
||||
|
||||
@State private var offset: CGSize = .zero
|
||||
@State private var scale: CGFloat = 1
|
||||
|
@ -34,13 +55,13 @@ public struct CropImageView: View {
|
|||
if let image = renderer.uiImage {
|
||||
return image
|
||||
} else {
|
||||
throw RenderError.imageRendererReturnedNil
|
||||
throw CropError.imageRendererReturnedNil
|
||||
}
|
||||
#elseif os(macOS)
|
||||
if let image = renderer.nsImage {
|
||||
return image
|
||||
} else {
|
||||
throw RenderError.imageRendererReturnedNil
|
||||
throw CropError.imageRendererReturnedNil
|
||||
}
|
||||
#endif
|
||||
} else {
|
||||
|
@ -53,9 +74,13 @@ public struct CropImageView: View {
|
|||
window.addSubview(hosting.view)
|
||||
window.makeKeyAndVisible()
|
||||
UIGraphicsBeginImageContextWithOptions(hosting.view.bounds.size, false, targetScale)
|
||||
let context = UIGraphicsGetCurrentContext()!
|
||||
guard let context = UIGraphicsGetCurrentContext() else {
|
||||
throw CropError.failedToGetCurrentUIGraphicsContext
|
||||
}
|
||||
hosting.view.layer.render(in: context)
|
||||
let image = UIGraphicsGetImageFromCurrentImageContext()!
|
||||
guard let image = UIGraphicsGetImageFromCurrentImageContext() else {
|
||||
throw CropError.failedToGetImageFromCurrentUIGraphicsImageContext
|
||||
}
|
||||
UIGraphicsEndImageContext()
|
||||
return image
|
||||
#endif
|
||||
|
|
21
Sources/CropImage/Documentation.docc/Documentation.md
Normal file
21
Sources/CropImage/Documentation.docc/Documentation.md
Normal file
|
@ -0,0 +1,21 @@
|
|||
# ``CropImageView``
|
||||
|
||||
A simple SwiftUI view where user can move and resize an image to a pre-defined size.
|
||||
|
||||
Supports iOS 14.0 and above, or macOS Ventura 13.0 and above.
|
||||
|
||||
## Overview
|
||||
|
||||
- Supports both iOS and macOS
|
||||
- Use `ImageRenderer` to render the cropped image, when possible
|
||||
- Very lightweight
|
||||
|
||||
## Topics
|
||||
|
||||
### Views
|
||||
|
||||
- ``CropImageView``
|
||||
|
||||
### Supporting Types
|
||||
|
||||
- ``PlatformImage``
|
|
@ -9,8 +9,14 @@ import Foundation
|
|||
|
||||
#if os(macOS)
|
||||
import AppKit
|
||||
/// The image object type, aliased to each platform.
|
||||
///
|
||||
/// On macOS, it's `NSImage` and on iOS it's `UIImage`.
|
||||
public typealias PlatformImage = NSImage
|
||||
#elseif os(iOS)
|
||||
import UIKit
|
||||
/// The image object type, aliased to each platform.
|
||||
///
|
||||
/// On macOS, it's `NSImage` and on iOS it's `UIImage`.
|
||||
public typealias PlatformImage = UIImage
|
||||
#endif
|
||||
|
|
Loading…
Add table
Reference in a new issue