Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Suggestion/Help For Others]Making a roundedimageFromURL method #27

Open
grantkemp opened this issue Apr 30, 2017 · 0 comments
Open

Comments

@grantkemp
Copy link

grantkemp commented 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.

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
 	}
 
 }

@grantkemp grantkemp changed the title Making a roundedimageFromURL method [Feature Suggestion/Help For Others]Making a roundedimageFromURL method Apr 30, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant