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
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.
- Swift 3.0
- iOS 8.0+
- Xcode 8
UIImageView+FaceAware.swift into your project.
There’s one for Swift 3.0 and 2.3 however the example project will only run in Xcode 8.
pod 'FaceAware'to your pod file.
import FaceAwareto the top of your files where you wish to use it.
There are a few ways to get your image views focussing in on faces within images.
This is the easiest method and doesn’t require writing any code. The extension makes use of
@IBInspectable so you can turn on focusOnFaces from within IB. However you won’t actually see the extension working until you run your project.
You can set
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.
More help? Questions?
Reach out to me on Twitter @beaunouvelle
- Add an option to only focus on largest/closest face in photo.
- Debug mode. Draw rectangles around detected faces.
The MIT License (MIT)
Copyright (c) 2016 Beau Nouvelle
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.