@@ -11,21 +11,14 @@ extension UIImageView {
1111 /// Default Settings
1212 ///
1313 struct SiteIconDefaults {
14-
1514 /// Default SiteIcon's Image Size, in points.
1615 ///
17- static let imageSize = 40
18-
19- /// Default SiteIcon's Image Size, in pixels.
20- ///
21- static var imageSizeInPixels : Int {
22- return imageSize * Int( UIScreen . main. scale)
23- }
16+ static let imageSize = CGSize ( width: 40 , height: 40 )
2417 }
2518
2619
2720 /// Downloads the SiteIcon Image, hosted at the specified path. This method will attempt to optimize the URL, so that
28- /// the download Image Size matches `SiteIconDefaults. imageSize`.
21+ /// the download Image Size matches `imageSize`.
2922 ///
3023 /// TODO: This is a convenience method. Nuke me once we're all swifted.
3124 ///
@@ -38,35 +31,42 @@ extension UIImageView {
3831
3932
4033 /// Downloads the SiteIcon Image, hosted at the specified path. This method will attempt to optimize the URL, so that
41- /// the download Image Size matches `SiteIconDefaults. imageSize`.
34+ /// the download Image Size matches `imageSize`.
4235 ///
4336 /// - Parameters:
4437 /// - path: SiteIcon's url (string encoded) to be downloaded.
38+ /// - imageSize: Request site icon in the specified image size.
4539 /// - placeholderImage: Yes. It's the "place holder image", Sherlock.
4640 ///
4741 @objc
48- func downloadSiteIcon( at path: String , placeholderImage: UIImage ? ) {
49- guard let siteIconURL = optimizedURL ( for: path) else {
42+ func downloadSiteIcon(
43+ at path: String ,
44+ imageSize: CGSize = SiteIconDefaults . imageSize,
45+ placeholderImage: UIImage ?
46+ ) {
47+ guard let siteIconURL = optimizedURL ( for: path, imageSize: imageSize) else {
5048 image = placeholderImage
5149 return
5250 }
5351
5452 logURLOptimization ( from: path, to: siteIconURL)
5553
5654 let request = URLRequest ( url: siteIconURL)
57- downloadSiteIcon ( with: request, placeholderImage: placeholderImage)
55+ downloadSiteIcon ( with: request, imageSize : imageSize , placeholderImage: placeholderImage)
5856 }
5957
6058 /// Downloads a SiteIcon image, using a specified request.
6159 ///
6260 /// - Parameters:
63- /// - request: the request for the SiteIcon.
61+ /// - request: The request for the SiteIcon.
62+ /// - imageSize: Request site icon in the specified image size.
6463 /// - placeholderImage: Yes. It's the "place holder image".
6564 ///
6665 private func downloadSiteIcon(
6766 with request: URLRequest ,
68- placeholderImage: UIImage ? ) {
69-
67+ imageSize expectedSize: CGSize = SiteIconDefaults . imageSize,
68+ placeholderImage: UIImage ?
69+ ) {
7070 af_setImage ( withURLRequest: request, placeholderImage: placeholderImage, completion: { [ weak self] dataResponse in
7171 switch dataResponse. result {
7272 case . success( let image) :
@@ -82,8 +82,6 @@ extension UIImageView {
8282 // The following lines of code ensure that we resize the image to the default Site Icon size, to
8383 // ensure there is no UI breakage due to having larger images set here.
8484 //
85- let expectedSize = CGSize ( width: SiteIconDefaults . imageSize, height: SiteIconDefaults . imageSize)
86-
8785 if image. size != expectedSize {
8886 self . image = image. resizedImage ( with: . scaleAspectFill, bounds: expectedSize, interpolationQuality: . default)
8987 } else {
@@ -103,19 +101,21 @@ extension UIImageView {
103101
104102
105103 /// Downloads the SiteIcon Image, associated to a given Blog. This method will attempt to optimize the URL, so that
106- /// the download Image Size matches `SiteIconDefaults. imageSize`.
104+ /// the download Image Size matches `imageSize`.
107105 ///
108106 /// - Parameters:
109107 /// - blog: reference to the source blog
110108 /// - placeholderImage: Yes. It's the "place holder image".
111109 ///
112- @objc
113- func downloadSiteIcon( for blog: Blog , placeholderImage: UIImage ? = . siteIconPlaceholder) {
114- guard let siteIconPath = blog. icon, let siteIconURL = optimizedURL ( for: siteIconPath) else {
110+ @objc func downloadSiteIcon(
111+ for blog: Blog ,
112+ imageSize: CGSize = SiteIconDefaults . imageSize,
113+ placeholderImage: UIImage ? = . siteIconPlaceholder
114+ ) {
115+ guard let siteIconPath = blog. icon, let siteIconURL = optimizedURL ( for: siteIconPath, imageSize: imageSize) else {
115116
116117 if blog. isWPForTeams ( ) && placeholderImage == . siteIconPlaceholder {
117- let standardSize = CGSize ( width: SiteIconDefaults . imageSize, height: SiteIconDefaults . imageSize)
118- image = UIImage . gridicon ( . p2, size: standardSize)
118+ image = UIImage . gridicon ( . p2, size: imageSize)
119119 return
120120 }
121121
@@ -135,7 +135,7 @@ extension UIImageView {
135135 for: siteIconURL,
136136 from: host,
137137 onComplete: { [ weak self] request in
138- self ? . downloadSiteIcon ( with: request, placeholderImage: placeholderImage)
138+ self ? . downloadSiteIcon ( with: request, imageSize : imageSize , placeholderImage: placeholderImage)
139139 } ) { error in
140140 DDLogError ( error. localizedDescription)
141141 }
@@ -145,31 +145,31 @@ extension UIImageView {
145145
146146// MARK: - Private Methods
147147//
148- private extension UIImageView {
148+ extension UIImageView {
149149 /// Returns the Size Optimized URL for a given Path.
150150 ///
151- func optimizedURL( for path: String ) -> URL ? {
151+ func optimizedURL( for path: String , imageSize : CGSize = SiteIconDefaults . imageSize ) -> URL ? {
152152 if isPhotonURL ( path) || isDotcomURL ( path) {
153- return optimizedDotcomURL ( from: path)
153+ return optimizedDotcomURL ( from: path, imageSize : imageSize )
154154 }
155155
156156 if isBlavatarURL ( path) {
157- return optimizedBlavatarURL ( from: path)
157+ return optimizedBlavatarURL ( from: path, imageSize : imageSize )
158158 }
159159
160- return optimizedPhotonURL ( from: path)
160+ return optimizedPhotonURL ( from: path, imageSize : imageSize )
161161 }
162162
163163
164164 // MARK: - Private Helpers
165165
166- /// Returns the download URL for a square icon with a size of `SiteIconDefaults.imageSizeInPixels`
166+ /// Returns the download URL for a square icon with a size of `imageSize` in pixels.
167167 ///
168168 /// - Parameter path: SiteIcon URL (string encoded).
169169 ///
170- private func optimizedDotcomURL( from path: String ) -> URL ? {
171- let size = SiteIconDefaults . imageSizeInPixels
172- let query = String ( format: " w=%d&h=%d " , size, size)
170+ private func optimizedDotcomURL( from path: String , imageSize : CGSize = SiteIconDefaults . imageSize ) -> URL ? {
171+ let size = imageSize . toPixels ( )
172+ let query = String ( format: " w=%d&h=%d " , Int ( size. width ) , Int ( size. height ) )
173173
174174 return parseURL ( path: path, query: query)
175175 }
@@ -179,9 +179,9 @@ private extension UIImageView {
179179 ///
180180 /// - Parameter path: Blavatar URL (string encoded).
181181 ///
182- private func optimizedBlavatarURL( from path: String ) -> URL ? {
183- let size = SiteIconDefaults . imageSizeInPixels
184- let query = String ( format: " d=404&s=%d " , size)
182+ private func optimizedBlavatarURL( from path: String , imageSize : CGSize = SiteIconDefaults . imageSize ) -> URL ? {
183+ let size = imageSize . toPixels ( )
184+ let query = String ( format: " d=404&s=%d " , Int ( max ( size. width , size . height ) ) )
185185
186186 return parseURL ( path: path, query: query)
187187 }
@@ -191,13 +191,12 @@ private extension UIImageView {
191191 ///
192192 /// - Parameter siteIconPath: SiteIcon URL (string encoded).
193193 ///
194- private func optimizedPhotonURL( from path: String ) -> URL ? {
194+ private func optimizedPhotonURL( from path: String , imageSize : CGSize = SiteIconDefaults . imageSize ) -> URL ? {
195195 guard let url = URL ( string: path) else {
196196 return nil
197197 }
198198
199- let size = CGSize ( width: SiteIconDefaults . imageSize, height: SiteIconDefaults . imageSize)
200- return PhotonImageURLHelper . photonURL ( with: size, forImageURL: url)
199+ return PhotonImageURLHelper . photonURL ( with: imageSize, forImageURL: url)
201200 }
202201
203202
@@ -267,3 +266,19 @@ private extension UIImageView {
267266 DDLogInfo ( " URL optimized from \( original) to \( optimized. absoluteString) for blog \( blogInfo) " )
268267 }
269268}
269+
270+ // MARK: - CGFloat Extension
271+
272+ private extension CGSize {
273+
274+ func toPixels( ) -> CGSize {
275+ return CGSize ( width: width. toPixels ( ) , height: height. toPixels ( ) )
276+ }
277+ }
278+
279+ private extension CGFloat {
280+
281+ func toPixels( ) -> CGFloat {
282+ return self * UIScreen. main. scale
283+ }
284+ }
0 commit comments