Skip to content

About

A light-weight Swift package to resize images with ease on iOS and macOS

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Repository files navigation

HPImageUtils

High-performance image manipulation utilities for macOS and iOS built with Core Graphics and Core Image. Efficiently resize, scale, and apply perspective transformations to images with minimal memory overhead.

Features

  • ImageResizer: Fast image resizing and scaling using CGImageSource thumbnail generation
  • ImageSkewer: Perspective transformation (skewing) using Core Image filters
  • Memory-efficient processing without loading full images into memory
  • Support for multiple image formats (JPEG, PNG, TIFF, BMP, GIF, and more)
  • Direct-to-disk operations to minimize memory usage
  • Full Swift Package Manager support

Requirements

  • iOS 11.0+ / macOS 10.15+
  • Swift 5.1+
  • Xcode 11+

Installation

Swift Package Manager

Add HPImageUtils to your project through Xcode:

  1. Go to File > Add Package Dependencies...
  2. Enter the package URL: https://github.com/henrik-dmg/HPImageUtils
  3. Select the version you want to use

Or add it to your Package.swift dependencies:

dependencies: [
    .package(url: "https://github.com/henrik-dmg/HPImageUtils", from: "1.0.0")
]

Then add it to your target:

targets: [
    .target(
        name: "YourTarget",
        dependencies: ["HPImageUtils"]
    )
]

Usage

ImageResizer

The ImageResizer class provides high-performance image resizing and scaling.

Resize to Specific Dimensions

import HPImageUtils

// Initialize with source image URL
let resizer = try ImageResizer(sourceURL: imageURL)

// Resize to specific dimensions
let resizedImage = try resizer.resizeImage(to: CGSize(width: 800, height: 600))

// Or save directly to disk
try resizer.resizeImage(
    to: CGSize(width: 800, height: 600),
    destinationURL: outputURL,
    format: .jpeg
)

Scale by Percentage

let resizer = try ImageResizer(sourceURL: imageURL)

// Scale to 50% of original size
let scaledImage = try resizer.scaleImage(to: 0.5)

// Or save directly to disk
try resizer.scaleImage(
    to: 0.5,
    destinationURL: outputURL,
    format: .png
)

ImageSkewer

The ImageSkewer class applies perspective transformations to images. Available on iOS 13.0+ and macOS 10.15+.

Basic Perspective Transform

import HPImageUtils

let skewer = try ImageSkewer(sourceURL: imageURL)

// Define where the four corners should appear in the output
let corners = ImageSkewer.CornerConfiguration(
    topLeft: Point(x: 100, y: 100),
    topRight: Point(x: 700, y: 120),
    bottomRight: Point(x: 680, y: 900),
    bottomLeft: Point(x: 120, y: 880)
)

// Create configuration with target size
let config = ImageSkewer.Configuration(
    targetImageSize: Size(width: 800, height: 1000),
    cornerConfiguration: corners
)

// Apply transformation and save
try skewer.skewImage(configuration: config, outputURL: outputURL)

In-Memory Transformation

let skewer = try ImageSkewer(sourceURL: imageURL)

let corners = ImageSkewer.CornerConfiguration(
    topLeft: Point(x: 50, y: 50),
    topRight: Point(x: 450, y: 50),
    bottomRight: Point(x: 450, y: 650),
    bottomLeft: Point(x: 50, y: 650)
)

// Get CGImage directly without writing to disk
let skewedImage = try skewer.skewImage(
    targetImageSize: CGSize(width: 500, height: 700),
    cornerConfiguration: corners
)

Supported Image Formats

HPImageUtils supports a wide range of image formats:

// Common formats
.jpeg, .png, .gif, .tiff, .bmp

// Advanced formats
.jpeg2000, .raw, .svg

// Apple formats
.appleICNS, .quickTimeImage, .pict

// Other formats
.ico

Performance Benefits

  • Memory Efficient: Uses CGImageSource to avoid loading full images into memory
  • Fast Thumbnail Generation: Leverages Core Graphics' optimized thumbnail creation
  • Direct File I/O: Write results directly to disk without intermediate memory buffers
  • Hardware Acceleration: Core Image transformations utilize GPU acceleration when available

Error Handling

All operations can throw errors. Common error scenarios:

do {
    let resizer = try ImageResizer(sourceURL: imageURL)
    try resizer.resizeImage(to: targetSize, destinationURL: outputURL, format: .jpeg)
} catch {
    // Handle errors:
    // - File not found at source URL
    // - Invalid image format
    // - Unable to read image dimensions
    // - Disk write failures
    print("Image processing failed: \(error)")
}

License

[Add your license information here]

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Author

Henrik Panhans

About

A light-weight Swift package to resize images with ease on iOS and macOS

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages