This package integrates a UIImagePickerController into a SwiftUI app.

Overview

Photo Select and Crop

This Swift Package integrates a UIImagePickerController into a SwiftUI app and allows a user to select, scale and position an image to be cropped and saved as a conatct's photo, similar to the stock iOS Contacts app. To accomplish this, the project uses a processImage() function to send four variables via bindings to the parent view:

  1. a copy of the original UIImage,
  2. a scaled and / or cropped UIImage version of it,
  3. a CGFloat, and
  4. a CGPoint.

The CGFloat and CGPoint represent the scale and position of the original image used to make the processed version.

☝️ By saving the original image and scale and position markers and passing them to the ImageMoveAndScaleSheet view, if the user taps the "Change photo" button, the original photo is scaled and positioned to allow subsequent adjustments.

Implementation

Add an image display view to your parent view like this:

ImageDisplay(image: (contact.image != nil) ? teslaProfile : placeholderAperture, isEditMode: $isEditMode)

The above line of code looks to see if a contact already has an image saved. If true, then an ImageAttributes struct is created from the saved data and if false, then an ImageAttributes placeholder is used. $isEditMode is a @State variable passed as a binding to ImageDisplay which toggles showing a button to present the picker.

Using CoreData, one might do the following. Define two entities: Contact and ProfileImage, where Contact has a To One relationship to ProfileImage. The ProfileImage entity should have properties that can be mapped to the variables provided to the ImageDisplay struct after the user has selected an image from their library.

Then, an ImageAttributes struct must be made, initialized with the properties from the Core Data entity. This ImageAttributes struct is then passed to the ImageDisplay as in the above code. For the nil case, a second ImageAttributes struct is initialzed, this time with a default image named avatar which was saved to the project's Image Assets folder.

An ImageAttributes struct can easily be defined as a placeholder.

With an SF Symbol: let placeholderAperture = ImageAttributes(withSFSymbol: "camera.aperture")

With an image from the Assets folder: let placeholderAvatar = ImageAttributes(withImage: "avatar")

You might also like...
Swift Package Manager command plugin for Swift-DocC

Swift-DocC Plugin The Swift-DocC plugin is a Swift Package Manager command plugin that supports building documentation for SwiftPM libraries and execu

A Swift package to convert a colour to a name using Wikipedia's colour list

ColorName Usage import ColorName SwiftUI let myColorName = getName(for: Color.red) print(myColorName) UIKit let myColorName = getName(for: UIColor.red

Swift Package Manager plug-in to compile Metal files that can be debugged in Xcode Metal Debugger.

MetalCompilerPlugin Swift Package Manager plug-in to compile Metal files that can be debugged in Xcode Metal Debugger. Description Swift Package Manag

 SwiftUI App Icon Generator App for iOS & macOS Catalyst
SwiftUI App Icon Generator App for iOS & macOS Catalyst

SwiftUI App Icon Generator App for iOS & macOS Catalyst Generate Asset Icons easily to your iPhone, iPad, Mac, and Apple Watch Features The app has se

A Swift/SwiftUI utility for caching and displaying images in SwiftUI Views

A Swift/SwiftUI utility for caching and displaying images asynchronously. Built with Swift 5.5 and works with async/await.

🖼 Gallery App for Actomaton (async/await + Elm Architecture) + SwiftUI.
🖼 Gallery App for Actomaton (async/await + Elm Architecture) + SwiftUI.

🖼 Actomaton-Gallery Gallery App for Actomaton (async/await + Elm Architecture) + SwiftUI. NOTE: Most of the code are reused from Harvest-SwiftUI-Gall

GIFImage component for your SwiftUI app!
GIFImage component for your SwiftUI app!

SwiftUI GIF Lightweight SwiftUI component for rendering GIFs from data or assets, with no external dependencies. As a bonus, there's an extension that

A demo of face recognition SwiftUI app on iOS. Based on Vision, OpenCV, Dlib and ResNet.

iOS-FaceRecognizer A demo of face recognition SwiftUI app on iOS, build for iPad. Based on Vision, OpenCV, Dlib and ResNet. Features Add face image an

Image gallery similar to Adidias' app, built in SwiftUI
Image gallery similar to Adidias' app, built in SwiftUI

TripleStackGallery TripleStackGallery is a image gallery component, which displays a set of images as a stack of three images, always displaying the i

Comments
  • HoleShapeMask doesn't fit the image

    HoleShapeMask doesn't fit the image

    When the image is to long horizontally or vertically the HoleShapeMask doesn't fit the photo, therefore, when cropping the result image is not a circle.

    To fix the issue on vertically long photos change the if to call resetImageOriginScale() to OR

    if displayW < diameter - inset || displayH < diameter - inset {
        resetImageOriginAndScale()
    }
    

    But this doesn't solve the horizontally long photos Example

    opened by JHeisecke 3
  • Move files around to eliminate a warning

    Move files around to eliminate a warning

    The error that was displayed is:

    Showing Recent Messages
    found 2 file(s) which are unhandled; explicitly declare them as resources or exclude from the target
    

    This change will eliminate it. An undesirable effect of this change is that the images are referenced relatively from the local directory so you can't

    opened by damiancarrillo 0
  • I added support for camera and optional square crop

    I added support for camera and optional square crop

    I modifed your package to support a second button for camera picking and optional square cropping. ImagePane just has an additonal parameter "squareShape: Bool" to toggle between circle and square.

    If you're interested, I can upload the patched code here ... or tell me how else to contact you.

    Screenshot 2022-11-16 at 10 43 04
    opened by vigilanz 1
Releases(2.0.4)
Owner
Rillieux
Rillieux
Combine SnapshotTesting images into a single asset

An extension to SnapshotTesting which allows you to create images combining the output of multiple snapshot strategies, assuming they all output to UIImage.

James Sherlock 41 Nov 28, 2022
Simple CLI utility to save off an image from every webcam hooked into a mac

macOSCameraCapture Simple CLI utility to save off an image from every webcam connected to the macOS machine. This utility is meant for research and te

Cody Thomas 12 Jan 26, 2022
Turn a photo of your food into a face

Megabite Megabite is a mobile app that automatically turns a photo of your food into a face. Read more about it here. Installation This project uses C

Aaron Randall 361 Oct 26, 2022
A low-maintenance, simple framework for a snapshot testing, which takes into account a snapshot difference factor (must have if you're using CI).

CornSnapshotTesting A low-maintenance, simple framework for a snapshot testing, which takes into account a snapshot difference factor (must have if yo

Rita 2 Nov 29, 2021
Roll marbles into set positions on screen

Marble-Tilt Roll Marbles into set positions on screen Paul Hudson Hacking with Swift - project 26 - Took most of the code and created my own spin on i

Scott Mayhew 0 Dec 11, 2021
Generate a list of licenses for the Swift Package libraries that your app depends on.

LicenseList Generate a list of licenses for the Swift Package libraries that your app depends on. Example Requirements Written in Swift 5 Compatible w

Cybozu 40 Dec 19, 2022
URLImage is a package that holds an easy way of showing images from an URL.

URLImage Overview URLImage is a package that holds an easy way of showing images from an URL. Usually this processes should take the following process

Ramón Dias 1 Nov 1, 2021
🎨 Mondrian is lightweight Swift package that provides a universal basis for your design system's color palette.

??‍?? Mondrian Mondrian provides a universal basis on which to build your design system's color palette right from your codebase. With support for all

Klaas Schoenmaker 1 Nov 24, 2021
DittoSwift - Swift package for the DittoSwift framework

DittoSwift Swift package for the DittoSwift framework. See the Ditto iOS Install

Ditto 6 Sep 23, 2022
PerFolderResourcesPublishPlugin - Per-folder resources plugin for the Publish package

Per-folder resources for Publish A Publish plugin that copies per-folder resourc

Tom Harrington 1 Feb 18, 2022