PhotoTrim is available under the MIT license. See the LICENSE file for more info.
PhotoTrim is a powerful and intuitive image cropping library designed for iOS applications. Built with user experience as the top priority, it offers smooth gesture handling and an elegant UI.
PhotoTrim is available under the MIT license. See the LICENSE file for more info.
PhotoTrim is a powerful
PhotoTrim offers the following core functionalities:
- Intuitive pinch gestures for zoom control (supporting 0.5x to 4x magnification)
- Smooth pan gestures for image positioning
- Automatic image size adjustment and alignment
- Elegantly designed circular crop area
- Semi-transparent overlay to highlight the crop region
- Optional guidelines that can be enabled or disabled
- Support for iOS 11.0 and above
- Modern codebase written in Swift 5.0
- Responsive design using Auto Layout
- Memory-efficient image processing
- Thread-safe cropping operations
Here's how to add PhotoTrim to your project in Xcode:
- Select File > Add Packages... from the Xcode menu
- Enter the following URL in the search field:
https://github.com/hooni0918/photoTrim.git
- Select Version Rule:
- Up to Next Major: 1.0.0
- Click Add Package
Alternatively, you can add the dependency directly to your Package.swift file:
dependencies: [
.package(url: "https://github.com/hooni0918/photoTrim.git", from: "1.0.0")
]
First, import PhotoTrim:
import PhotoTrim
Here's the basic setup:
class ViewController: UIViewController {
private var trimView: PhotoTrimView!
override func viewDidLoad() {
super.viewDidLoad()
// Initialize PhotoTrimView
trimView = PhotoTrimView(frame: view.bounds)
view.addSubview(trimView)
// Setup Auto Layout
trimView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
trimView.topAnchor.constraint(equalTo: view.topAnchor),
trimView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
trimView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
trimView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
// Set image
if let image = UIImage(named: "example") {
trimView.setImage(image)
}
}
}
Here's how to obtain the cropped image:
@IBAction func cropButtonTapped(_ sender: Any) {
if let croppedImage = trimView.getCroppedImage() {
// Use the cropped image
// Example: Save to photo library
UIImageWriteToSavedPhotosAlbum(croppedImage, nil, nil, nil)
// Or pass to another view controller
let resultVC = ResultViewController()
resultVC.image = croppedImage
navigationController?.pushViewController(resultVC, animated: true)
}
}
PhotoTrimView provides various customization options:
// Configure zoom levels
trimView.minimumZoomScale = 0.5 // Default: 1.0
trimView.maximumZoomScale = 4.0 // Default: 3.0
// Configure guidelines
trimView.shouldShowGuideLines = true // Default: true
// Customize crop area style
trimView.cropAreaView.backgroundColor = UIColor.black.withAlphaComponent(0.6)
To monitor image cropping status, implement PhotoTrimViewDelegate:
class ViewController: UIViewController, PhotoTrimViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
trimView.delegate = self
}
func photoTrimViewDidBeginEditing(_ trimView: PhotoTrimView) {
// Called when editing begins
}
func photoTrimViewDidEndEditing(_ trimView: PhotoTrimView) {
// Called when editing ends
}
func photoTrimView(_ trimView: PhotoTrimView, didChangeCropRect rect: CGRect) {
// Called when crop area changes
}
}
Consider these points when handling large images:
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
// Release image from memory
trimView.imageView.image = nil
}
-
Image Not Displaying:
- Verify image file is properly included in the project
- Check image name accuracy in Asset Catalog
-
Unexpected Cropped Image Results:
- Check original image orientation
- Consider resizing if image is too large
-
Memory Warnings:
- Implement image resizing for large images
- Release unused images from memory
- Image Size Optimization:
extension UIImage {
func optimizedForCropping() -> UIImage {
let maxDimension: CGFloat = 2048
if size.width > maxDimension || size.height > maxDimension {
let ratio = size.width / size.height
let newSize: CGSize
if ratio > 1 {
newSize = CGSize(width: maxDimension, height: maxDimension / ratio)
} else {
newSize = CGSize(width: maxDimension * ratio, height: maxDimension)
}
let format = UIGraphicsImageRendererFormat()
format.scale = 1
let renderer = UIGraphicsImageRenderer(size: newSize, format: format)
return renderer.image { _ in
draw(in: CGRect(origin: .zero, size: newSize))
}
}
return self
}
}
- Image Loading Optimization:
func loadImage() {
DispatchQueue.global(qos: .userInitiated).async {
if let image = UIImage(named: "example")?.optimizedForCropping() {
DispatchQueue.main.async {
self.trimView.setImage(image)
}
}
}
}
To contribute to PhotoTrim:
-
Issue Submission:
- Bug reports
- Feature requests
- Documentation improvements
-
Pull Requests:
- Follow code style guidelines
- Include test coverage
- Provide detailed change descriptions
PhotoTrim is available under the MIT license. See the LICENSE file for details.
Please submit questions or issues through GitHub Issues.
- 1.0.0
- Initial release
- Basic image cropping functionality
- Circular crop area support
- Gesture-based manipulation
This project was inspired by the following open-source projects:
- YPImagePicker
- TOCropViewController
Thank you for choosing our library. We are committed to continuously improving the iOS development experience.