This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct User { | |
var name: String | |
var email: String | |
var photo: UIImage | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct User { | |
var name: String | |
var email: String | |
var photoImage: UIImage? | |
var photoUrl: URL? | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func showUserPhoto(for user: User, on imageView: UIImageView, placeholderImage: UIImage? = nil) { | |
if let photoImage = user.photoImage else { | |
imageView.image = photoImage | |
} else if let photoUrl = user.photoUrl else { | |
imageView.sd_setImageWithURL(photoUrl, placeholderImage:placeholderImage) | |
} else { | |
imageView.image = placeholderImage | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum Image { | |
case local(UIImage) | |
case remote(URL) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct User { | |
var name: String | |
var email: String | |
var photo: Image | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func show(photo: Image, on imageView: UIImageView, placeholderImage: UIImage? = nil) { | |
switch photo { | |
case .local(let image): | |
imageView.image = image | |
case .remote(let url): | |
imageView.sd_setImageWithURL(photoUrl, placeholderImage:placeholderImage) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum Image { | |
case local(UIImage) | |
case remote(URL) | |
init(localImage: UIImage) { | |
self = .local(localImage) | |
} | |
init(remoteUrl: URL) { | |
self = .remote(remoteUrl) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIImageView { | |
func set(image: Image, placeholderImage: UIImage? = nil) { | |
switch image { | |
case .local(let image): | |
self.image = image | |
case .remote(let url): | |
self.sd_setImageWithURL(photoUrl, placeholderImage:placeholderImage) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let localImage = Image.local(localImage: UIImage(named:"image")) | |
imageview.set(image: localImage) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let remoteImage = Image.remote(url: URL(string: "http://www.simpsoncrazy.com/content/pictures/homer/homer-pythagoras.png")) | |
imageview.set(image: remoteImage) |
OlderNewer