Skip to content

Commit

Permalink
extension
Browse files Browse the repository at this point in the history
  • Loading branch information
HuyDuy14 committed Jun 17, 2019
1 parent f04290d commit 7fb1bcb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
10 changes: 10 additions & 0 deletions SDKImageView/Classes/ExtensionImage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// ExtensionImage.swift
// SDKImageView
//
// Created by Le Duy Huy on 6/17/19.
//

import Foundation
import UIKit

41 changes: 41 additions & 0 deletions SDKImageView/Classes/ExtensionImageView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// ExtensionImageView.swift
// SDKImageView
//
// Created by Le Duy Huy on 6/17/19.
//

import Foundation
import UIKit

extension UIImageView {

public func circleImageView(borderColor: UIColor, borderWidth: CGFloat){
self.layer.borderColor = borderColor.cgColor
self.layer.borderWidth = borderWidth
self.layer.cornerRadius = self.layer.frame.size.width / 2
self.clipsToBounds = true
}

func downloadedFrom(url: URL, contentMode mode: UIView.ContentMode = .scaleAspectFill) {
contentMode = mode
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard
let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
let data = data, error == nil,
let image = UIImage(data: data)
else { return }
DispatchQueue.main.async() { () -> Void in
self.image = image
}
}.resume()
}

public func downloadedFrom(link: String, contentMode mode: UIView.ContentMode = .scaleAspectFill) {

guard let url = URL(string: link) else { return }
downloadedFrom(url: url, contentMode: mode)
}

}

0 comments on commit 7fb1bcb

Please sign in to comment.