Code Monkey home page Code Monkey logo

avatarimageview's Introduction

MIT Licence Pod Version Swift Version Twitter

๐Ÿšจ๐Ÿšจ THIS LIBRARY IS NO LONGER BEING MAINTAINED ๐Ÿšจ๐Ÿšจ

I've made a career move away from iOS and decided to stop maintaining this library due to Swift's continued instability.

The amazing folks at Neone have forked the project and continued to develop it. Please use their fork located here: https://github.com/neone/NDAvatarApp.

Build Status

Branch Build Status
Develop Build Status
Master Build Status

Description

AvatarImageView is a customisable subclass of UIImageView that is designed to show users' profile pictures. It falls back to the user's initials with a random background color if no profile picture is supplied.

This library was inspired by and is supposed to be a Swift rewrite of bachonk/UIImageView-Letters.

Usage

To set up AvatarImageView, a dataSource that conforms to AvatarImageViewDataSource needs to be set. Optionally a configuration that conforms to AvatarImageViewConfiguration can also be set. The default configuration will show a square picture; and if no profile picture is supplied, it will draw the initials with the system font on a random background color.

The AvatarImageViewDataSource contains the following members. All have default implementations and are hence optional.

  • var name: String { get } - Default: returns ""
  • var avatar: UIImage? { get } - Default: returns nil
  • var bgColor: UIColor? { get } - Default: returns nil
  • var initials: String { get } - Default: returns initials calculated from the name.
  • var avatarId: Int { get } - Default: returns the hash values of the name and initials combined using XOR.

The AvatarImageViewConfiguration contains the following members. All have default implementations and are hence optional.

  • var shape: Shape { get } - Default: returns .Square
  • var textSizeFactor: CGFloat { get } - Default: returns 0.5
  • var fontName: String? { get } - Default: returns nil
  • var bgColor: UIColor? { get } - Default: returns innil. The bgColor in AvatarImageViewDataSource will take precedence over this one.
  • var textColor: UIColor { get } - Default: returns .white.

Check out the docs for more information.

The random background colour is generated for each unique user from its avatarId, so if you have AvatarImageViews in different parts of your app, the background color for a particular user will be the same in both.

The image view can be drawn as a square or circle out of the box. You can even sepcify a mask image if you want a custom shape. These settings are done in an AvatarImageViewConfiguration. Here are some examples for initials being drawn in different shapes.

Square Initials Circle Initials Mask Initials

Here's an example of when the dataSource supplies a profile picture and the configuration is set to a circle.

Circle Profile Pic

It works great with custom fonts!

Custom Font

...and also with UITableViews

Table View Pics

Example Project

Please refer to the example project in this repository for an implementation of all the above use cases. If you find any bugs, open a GitHub issue!

Gotchas

  1. Always set the configuration before the dataSource. If you don't, you will need to manually call refresh() to render the view correctly.

  2. When implementing the AvatarImageViewDataSource and AvatarImageViewConfiguration protocols, you will need to explicitly define the type for any protocol member that is an optional otherwise Swift goes insane.

For example, AvatarImageViewConfiguration has a type called var fontName: String? that returns nil by default. To implement this in a struct, define it as follows:

struct Config: AvatarImageViewConfiguration {
    var fontName: String? = "Futura-Medium"        
}

Defining it as:

struct Config: AvatarImageViewConfiguration {
    var fontName = "Futura-Medium"
}

... will not work :(

Documentation

Docs are available on CocoaDocs

Requirements

AvatarImageView requires at least iOS 8.

Installation

AvatarImageView is available through CocoaPods. To install it, add the following lines to your Podfile:

use_frameworks!

# Swift 2.2
pod "AvatarImageView", '1.1.1'

# Swift 2.3
pod "AvatarImageView", '~> 1.2.1'

# Swift 3.0
pod "AvatarImageView", '2.0.0'

# Swift 4.0
pod "AvatarImageView", '~> 2.1.0'

# Swift 4.2
pod "AvatarImageView", '~> 2.2.0'

Release Notes

2.2.0

Added Swift 4.2 support

2.1.1

Fixed irrational test suite and test warnings

2.1.0

Migrated to Swift 4

2.0.3

Added a baseline offset config attribute which may be needed for custom fonts

2.0.2

Fixing an issue where a profile picture may not always appear in a circle even if specified in the configuration.

2.0.1

Modified init(frame:) to be public. Seems to have reversed from earlier by a bad merge.

2.0.0

Migrated code to Swift 3.0.

1.2.1

Set deployment target to 8.0. No further features or patches will be issued for Swift 2.3 after this release.

1.2.0

Migrated code to Swift 2.3.

1.1.1

Modified init(frame:) to be public.

1.1.0

Improved the random color generator. It now generates a hash from the initials and name, and then uses that as the seed to generate the random color, so it's always the same for each unique user.

1.0.0

Initial Release

Author

Ayush Newatia, [email protected]

License

AvatarImageView is available under the MIT license. See the LICENSE.md file for more info.

avatarimageview's People

Contributors

basthomas avatar jblorenzo avatar loryhuz avatar sanketfirodiya avatar wow-such-amazing avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

avatarimageview's Issues

Choose smaller of the two dimensions or make a perfect circle

When width of image is bigger than height and it is taken as the radius the image drawn in circle is not perfect circle but a rectangle with two sides circular. Is ther ea way to force smaller of the two edges to be taken as reference. or maybe just fit the image the best way without losing orientation or aspect ratio in between a circle.

EXC_BAD_Access

Hi,
Thanks a lot for pod!
I am trying to implement it into my app but I am running into an issue.
I'm trying to add it in a cell of my table view controller.
I am using swift 2.2 and the pod "AvatarImageView", '1.1.1'

Here is my table_cell file

`import UIKit
import AvatarImageView

struct TableAvatarImageConfig: AvatarImageViewConfiguration {
static let shape: Shape? = .Circle
}

class ContactPageTableViewCell: UITableViewCell {

// - // MARK: - When Cell Loads
override func awakeFromNib() {
    super.awakeFromNib()
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

@IBOutlet weak var ContactPageImageView: AvatarImageView! {
    didSet {
        ContactPageImageView.configuration = TableAvatarImageConfig()
    }
}

@IBOutlet weak var ContactPageNameLabel: UILabel!
@IBOutlet weak var ContactPageNicknameLabel: UILabel!
@IBOutlet weak var ContactPagePhoneNumberLabel: UILabel!

}I have an error or the line ContactPageImageView.configuration = TableAvatarImageConfig()`
EXC_BAD_ACESS (code=2, adress=0x7f9f74d3d1b0)

Any ideas of what I'm doing wrong?

Thanks

Image for UIButton

Can you please provide us one updated version with option for setting image for uibutton

Not working!?

It keeps failing the the following line:
avatarImageView.dataSource = ExampleData()

in the func showInitials() function!?

Border on Avatar view?

Do you have a suggestion of how we could add a border to the Avatar view? I don't see that as a current option. Looks great otherwise and could be very useful. Thanks for the code...

Add white border

I am using the circular shape. What would be the recommended way to add a white border (of arbitrary thickness) arround it?

Swift 4

Plans to migrate the code to Swift 4?

How to create AvatarImageView pragmatically?

(1)

self.avatarImageView = AvatarImageView(frame: CGRectZero)

This gives me error issue "expected a coder: NSCoder" ?

should we write like this in init:

override public init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

Tried to use, but no success:

CGContextFillRects: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
fatal error: unexpectedly found nil while unwrapping an Optional value

Can you write example with pragmatic usage?

(2) Another question, how about with SDWebImage, how we should update asynchronous image in avatar, which downloaded?

Swift 3

Hello Ayush,

I have updated my code to swift 3.0 but when I try to run, it gives me an legacy swift version error.
Later when I convert the dependency code, it gives me so many errors. Could you please check if the it is working or not?

screen shot 2016-09-20 at 4 37 09 pm

Image Isn't Rounded

When providing a contact object's image to the datasource as the avatar, it is not getting rounded. The example does not get rounded when providing an image either.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.