BeauNouvelle / AspectFillFaceAware
- воскресенье, 7 августа 2016 г. в 03:12:43
Swift
An extension that gives UIImageView the ability to focus on faces within an image when using AspectFill.
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 AspectFillFaceAware comes in.
It will analyse an image either through UIImageView
's image
property, or one you set using one of the built in fucntions and focus in on any faces it can find within.
Also works great with avatars!
Based on these two older projects:
Both of which don't seem to be maintained anymore.
Simply drag 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.
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 @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.
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.
Reach out to me on Twitter @beaunouvelle
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.