An extension that gives UIImageView the ability to focus on faces within an image.

Overview

FaceAware

Sometimes the aspect ratios of images we need to work with don't quite fit within the confines of our UIImageViews.

In most cases we can use AspectFill to fit the image to the bounds of a UIImageView without stretching or leaving whitespace, however when it comes to photos of people, it's quite often to have the faces cropped out if they're not perfectly centered.

This is where FaceAware comes in. It will analyse an image either through UIImageView's image property, or one you set using one of the built in functions and focus in on any faces it can find within.

The most common use for FaceAware is with avatars.

With FaceAware your users will no longer have to crop and adjust their profile pictures.

Based on these two older projects:

Both of which don't seem to be maintained anymore.

Requirements

  • Swift 5.1
  • iOS 8.0+
  • Xcode 11

Installation

Manual

Simply drag UIImageView+FaceAware.swift into your project.

Carthage

  • Add github "BeauNouvelle/FaceAware" to your Cartfile

More information on installing and setting up Carthage can be found here: https://github.com/Carthage/Carthage

Cocoapods

  • Add pod 'FaceAware' to your pod file.
  • Add import FaceAware to the top of your files where you wish to use it.

Usage

There are a few ways to get your image views focussing in on faces within images.

Interface Builder

This is the easiest method and doesn't require writing any code. The extension makes use of @IBDesignable and @IBInspectable so you can turn on focusOnFaces from within IB. However you won't actually see the extension working until you run your project.

Code

You can set focusOnFaces to true.

someImageView.focusOnFaces = true

Be sure to set this after setting your image. If no image is present when this is called, there will be no faces to focus on.


Alternatively you can use:

someImageView.set(image: myImage, focusOnFaces: true)

Which elimates the worry of not having an image previously set.


You can also recieve a callback for when face detection and any image adjustments have been completed by passing in a closure to the didFocusOnFaces property.

someImageView.didFocusOnFaces = {
     print("Did finish focussing")
}

Debugging

FaceAware now features a debug mode which draws red squares around any detected faces within an image. To enable you can set the debug property to true.

someImageView.debug = true

You can also set this flag within interface builder.

More help? Questions?

Reach out to me on Twitter @beaunouvelle Also, if you're using this in your project and you like it, please let me know so I can continue working on it!

Future Plans

  • Add an option to only focus on largest/closest face in photo.
Comments
  • Getting a strange

    Getting a strange "Blinking"

    Hi @BeauNouvelle

    Any idea why would I see the following issue: when focusOnFaces is set On I get a strange issue where the image is being seen for a brief moment, then it disappears and the re-apears. It all happens very fast but the issue is noticeable and happens every time.

    Any thoughts maybe?

    bug 
    opened by oferRounds 12
  • add pass FaceAware Finished image modify

    add pass FaceAware Finished image modify

    SomeTimes , The project should know the image FaceAware Finished, so I modify the code.

    add pass FaceAware Finished image modify.

    This is a Notification to pass FaceAwared image value modify, And I think Use block should be delightful. What do you think ?

    opened by CocoaML 8
  • Memory issue

    Memory issue

    WeakSelf(imageView) [imageView sd_setImageWithURL:[NSURL URLWithString:url] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) { weakimageView.image = image; weakimageView.focusOnFaces = YES; }];

    When I scroll the TableView, This code will be performed.( I used SDWebImage to set ImageView image and Then focusOnFaces )
    And the memory report The memory used so high.

    What can do to reduce memory ?

    opened by CocoaML 6
  • Pods’ Swift 2.3 version

    Pods’ Swift 2.3 version

    Hi,

    First of all — cool library, thanks for sharing it!

    I’d like to use the Swift 2.3 version, what version should I refer to on my cocoa pods?

    Thanks!

    opened by oferRounds 5
  • Xcode 9: UIImageView.image must be used from main thread only

    Xcode 9: UIImageView.image must be used from main thread only

    As the title says Xcode 9 gives me a nice title purple exclamation mark telling me that FaceAware is using a method where UIImageView.image is not run on the main thread.

    opened by timroesner 4
  • Refresh image when the frame changes

    Refresh image when the frame changes

    This is an attempt to fix #7. I've also updated the example app to enable landscape support so that this fix can be tested.

    About the fix:

    First, the app loads in portrait mode:

    simulator screen shot - iphone 8 plus - 2017-10-19 at 16 40 39

    Then, user rotates the device to landscape mode, making the UIImageView's frame change:

    • Without the fix:

    simulator screen shot - iphone 8 plus - 2017-10-19 at 16 41 06

    • With the fix:

    simulator screen shot - iphone 8 plus - 2017-10-19 at 16 40 44

    opened by T-Pham 3
  • Doesn't handle frame changes

    Doesn't handle frame changes

    If the frame changes aspect ratio, the visible image layer isn't recomputed with the new aspect ratio.

    This is easiest to see on with a full-screen view that handles rotation on a long thin device like an iPhone 5, when you rotate from portrait to landscape the preserved pixels from the original image should be different between the two modes.

    bug 
    opened by DavidPhillipOster 2
  • Fix initial value for rect calculation

    Fix initial value for rect calculation

    Fix for #10

    When iterating features rect origin is updated like this:

                rect.origin.x = min(oneRect.origin.x, rect.origin.x)
                rect.origin.y = min(oneRect.origin.y, rect.origin.y)
    

    But initial value for rect.origin was zero - this prevented rect.origin.x and y from being >0. This PR fixes it by using first feature's bounds as initial value.

    opened by algrid 1
  • Fix Xcode 8 / iOS 10 Requirement

    Fix Xcode 8 / iOS 10 Requirement

    I wanted to test your library out to see if it would work for an app I am developing, but it looks like you built the entire example app in Xcode 8 Beta using iOS 10. You really shouldn't release public libraries under Betas. Would love to see this re-built in the current release version of Xcode.

    Cheers!

    opened by jhickmanEB 1
  • Get notifified when focus has finished.

    Get notifified when focus has finished.

    This feature adds a new property where you can pass in a closure and be notified once the focus process has been completed.

    I've also just realized that this process is run TWICE when first setting up. Once for when 'focusOnFaces' Bool is set, and another during layoutSubviews.

    Obviously this will significantly affect performance, especially when placed inside tableview cells. As this has been the behavior for a while, I might create another PR addressing this issue rather than doing it here.

    opened by BeauNouvelle 0
  • Face processing is occurring multiple times.

    Face processing is occurring multiple times.

    1. When focusOnFaces is set.
    2. When layoutSubviews is called.

    Face detection is already quite expensive on resources, and to double up on that just isn't acceptable, especially with the number of apps out there now using this library.

    HACKTOBERFEST 
    opened by BeauNouvelle 1
Owner
Beau Nouvelle
iOS/OSX Swift Developer.
Beau Nouvelle
A simple UIImageView extension for using initials as a profile image, written in swift

InitialsImageView An easy, helpful UIImageView extension that generates letter initials as a placeholder for user profile images, with a randomized ba

Tom Bachant 215 Dec 17, 2022
Async image downloader with Mem&Disk cached as a UIImageView extension

HBWebImage Example To run the example project, clone the repo, and run pod install from the Example directory first. Requirements Installation HBWebIm

haoboxuxu 1 Oct 22, 2022
✂️ Detect and crop faces, barcodes and texts in image with iOS 11 Vision api.

ImageDetect ImageDetect is a library developed on Swift. With ImageDetect you can easily detect and crop faces, texts or barcodes in your image with i

Arthur Sahakyan 299 Dec 17, 2022
Crop faces, inside of your image, with iOS 11 Vision api.

FaceCropper Requirements Xcode 9.0 (beta) or higher. iOS 11.0 (beta) or higher. (It is possible to import this library under the iOS 11. But it won't

Taejun Kim 488 Dec 17, 2022
Image slide-show viewer with multiple predefined transition styles, with ability to create new transitions with ease.

ATGMediaBrowser ATGMediaBrowser is an image slide-show viewer that supports multiple predefined transition styles, and also allows the client to defin

null 200 Dec 19, 2022
Asynchronous image downloader with cache support as a UIImageView category

This library provides an async image downloader with cache support. For convenience, we added categories for UI elements like UIImageView, UIButton, M

null 24.4k Jan 5, 2023
Utilizing Apple's Vision Framework to center faces in CGImage.

FaceCrop CGImage extension that utilizes Apple's Vision Framework to detect and center faces. Usage cgImage.faceCrop { [weak self] result in switc

Ancestry.com 40 Nov 24, 2022
A framework that lists all photos in the album that contain faces.

FaceCollectionViewKit A framework that lists all photos in the album that contain faces. Features It uses the detection features of CIDetectorTypeFace

Serhat Akalın 2 Aug 30, 2022
An image download extension of the image view written in Swift for iOS, tvOS and macOS.

Moa, an image downloader written in Swift for iOS, tvOS and macOS Moa is an image download library written in Swift. It allows to download and show an

Evgenii Neumerzhitckii 330 Sep 9, 2022
UIImageView BetterFace Swift

UIImageView BetterFace Swift

Croath Liu 470 Sep 9, 2022
iOS Framework that makes it easy to preview images on any UIImageView.

AZImagePreview iOS Framework that makes it easy to preview images on any UIImageView. Screenshots Installation: Cocoa Pods: pod 'AZImagePreview' Swift

Antonio Zaitoun 25 Dec 11, 2022
Live rendering of UIImageView properties in Interface Builder

TJProfileImage Live rendering of componet’s properties in Interface Builder Features Dashed border Solid border Round corner Circle image Requirements

Tejas Ardeshna 32 Feb 28, 2022
Media view which subclasses UIImageView, and can display & load images, videos, GIFs, and audio and from the web, and has functionality to minimize from fullscreen, as well as show GIF previews for videos.

I've built out the Swift version of this library! Screenshots Description ABMediaView can display images, videos, as well as now GIFs and Audio! It su

Andrew Boryk 80 Dec 20, 2022
Focus on avatar caching.

Navi Navi is designed for avatar caching, with style. The name of Navi from movie Avatar. Requirements Swift 3.1, iOS 8.0 Swift 2.3, use version 0.5.0

null 114 Jun 30, 2022
Siri Shortcuts extension for calculating NN-based image hash.

NNHash Siri Shortcuts extension for calculating NN-based image hash. Based on nhcalc.

Yi Xie 3 Aug 9, 2021
SwiftColorArt is a demo application that includes Swift files with all classes and extension necessary to create a font color schema matching to an image

SwiftColorArt SwiftColorArt is a demo application that includes Swift files with all classes and extension necessary to create a font color schema mat

Jan Gregor Triebel 264 Jan 4, 2023
An NSFW image detector for Swift built as an extension on UIImage.

Swift_NSFW_Detector An NSFW image detector for Swift built as an extension on UIImage. If you've ever allowed users to share images you are probably w

Chris Brown 5 Nov 27, 2022
📷 A composable image editor using Core Image and Metal.

Brightroom - Composable image editor - building your own UI Classic Image Editor PhotosCrop Face detection Masking component ?? v2.0.0-alpha now open!

Muukii 2.8k Jan 3, 2023
📷 A composable image editor using Core Image and Metal.

Brightroom - Composable image editor - building your own UI Classic Image Editor PhotosCrop Face detection Masking component ?? v2.0.0-alpha now open!

Muukii 2.8k Jan 2, 2023