Skip to content

Commit

Permalink
Use the scale factor of the device's main screen when scaling. (#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentSit authored and LucianoPAlmeida committed Jul 15, 2018
1 parent fdef062 commit 70bd4a6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -13,6 +13,8 @@ The changelog for **SwifterSwift**. Also see the [releases](https://github.com/S
> ### Deprecated
> ### Removed
> ### Fixed
- **UIImage**:
- Fixed `scaled(toWidth:, with orientation:)` and `scaled(toHeight:, with orientation:)` were using image's scale as the scale factor. [#515](https://github.com/SwifterSwift/SwifterSwift/pull/515) by [VincentSit](https://github.com/VincentSit).
> ### Security
---
Expand Down
10 changes: 4 additions & 6 deletions Sources/Extensions/UIKit/UIImageExtensions.swift
Expand Up @@ -69,12 +69,11 @@ public extension UIImage {
/// - Parameters:
/// - toHeight: new height.
/// - opaque: flag indicating whether the bitmap is opaque.
/// - orientation: optional UIImage orientation (default is nil).
/// - Returns: optional scaled UIImage (if applicable).
public func scaled(toHeight: CGFloat, opaque: Bool = false, with orientation: UIImageOrientation? = nil) -> UIImage? {
public func scaled(toHeight: CGFloat, opaque: Bool = false) -> UIImage? {
let scale = toHeight / size.height
let newWidth = size.width * scale
UIGraphicsBeginImageContextWithOptions(CGSize(width: newWidth, height: toHeight), opaque, scale)
UIGraphicsBeginImageContextWithOptions(CGSize(width: newWidth, height: toHeight), opaque, 0)
draw(in: CGRect(x: 0, y: 0, width: newWidth, height: toHeight))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
Expand All @@ -86,12 +85,11 @@ public extension UIImage {
/// - Parameters:
/// - toWidth: new width.
/// - opaque: flag indicating whether the bitmap is opaque.
/// - orientation: optional UIImage orientation (default is nil).
/// - Returns: optional scaled UIImage (if applicable).
public func scaled(toWidth: CGFloat, opaque: Bool = false, with orientation: UIImageOrientation? = nil) -> UIImage? {
public func scaled(toWidth: CGFloat, opaque: Bool = false) -> UIImage? {
let scale = toWidth / size.width
let newHeight = size.height * scale
UIGraphicsBeginImageContextWithOptions(CGSize(width: toWidth, height: newHeight), opaque, scale)
UIGraphicsBeginImageContextWithOptions(CGSize(width: toWidth, height: newHeight), opaque, 0)
draw(in: CGRect(x: 0, y: 0, width: toWidth, height: newHeight))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
Expand Down

0 comments on commit 70bd4a6

Please sign in to comment.