You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am sure lots of people will want to make a rounded image from a url for social apps etc I thought I would share below to help others.
If this is a good candidate for the library- happy to do a pull request. Maybe we can extend the image method to allow the user to customize the image shape that is cached to support other shapes.
Thanks for the wonderful library. Appreciate your efforts.
extension UIImageView {
// Extending UIImageHelper with new handler for Round Images
/**
Loads an image and makes it into a circle from a URL. If cached, the cached image is returned. Otherwise, a place holder is used until the image from web is returned by the closure.
- Parameter url: The image URL.
- Parameter placeholder: The placeholder image.
- Parameter fadeIn: Weather the mage should fade in.
- Parameter closure: Returns the image from the web the first time is fetched.
- Returns A new image
*/
func roundedimageFromURL(_ url: String, placeholder: UIImage, fadeIn: Bool = true, shouldCacheImage: Bool = true, closure: ((_ image: UIImage?) -> ())?)
{
self.image = UIImage.roundImage(fromURL: url, placeholder: placeholder, shouldCacheImage: shouldCacheImage) {
(image: UIImage?) in
if image == nil {
return
}
self.image = image
if fadeIn {
let transition = CATransition()
transition.duration = 0.5
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.type = kCATransitionFade
self.layer.add(transition, forKey: nil)
}
closure?(self.image)
}
}
}
extension UIImage {
class func roundImage(fromURL url: String, placeholder: UIImage, shouldCacheImage: Bool = true, closure: @escaping (_ image: UIImage?) -> ()) -> UIImage? {
// From Cache
let urlKey = "round_\(url)"
if shouldCacheImage {
if let image = UIImage.shared.object(forKey: urlKey as AnyObject) as? UIImage {
closure(nil)
return image
}
}
// Fetch Image
let session = URLSession(configuration: URLSessionConfiguration.default)
if let nsURL = URL(string: url) {
session.dataTask(with: nsURL, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
DispatchQueue.main.async {
closure(nil)
}
}
if let data = data, let image = UIImage(data: data) {
if let roundImage = image.roundCornersToCircle() {
if shouldCacheImage {
UIImage.shared.setObject(roundImage, forKey: urlKey as AnyObject)
}
DispatchQueue.main.async {
closure(roundImage)
}
}
}
session.finishTasksAndInvalidate()
}).resume()
}
return placeholder
}
}
The text was updated successfully, but these errors were encountered:
grantkemp
changed the title
Making a roundedimageFromURL method
[Feature Suggestion/Help For Others]Making a roundedimageFromURL method
Apr 30, 2017
I am sure lots of people will want to make a rounded image from a url for social apps etc I thought I would share below to help others.
If this is a good candidate for the library- happy to do a pull request. Maybe we can extend the image method to allow the user to customize the image shape that is cached to support other shapes.
Thanks for the wonderful library. Appreciate your efforts.
The text was updated successfully, but these errors were encountered: