Skip to content

Commit

Permalink
Update for Swift 4.2 and Xcode 10 support (#562)
Browse files Browse the repository at this point in the history
* Updating Code base and iOS Unit Tests.

* Updating MacOS tests

* Update travis image.

* Updating some extensions.

* Updating pod spec swift version

* Updating swift version

* Fixing indentation on some files.

* Indenting.

* Indenting some files.

* Running swiftlint autocorrect --format

* Workaround cocoapods issue

* Updating spec, readme and changelog for new version.

* Removing toggles since they are stdlib now.

* Deprecating random extensions in favor of the natives.

* Updating arc4_random usages.

* Removing prior SwifterSwift 4.2.0 version deprecated version.
  • Loading branch information
LucianoPAlmeida committed Sep 20, 2018
1 parent b23b559 commit b24e70f
Show file tree
Hide file tree
Showing 150 changed files with 14,279 additions and 14,503 deletions.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.1
4.2
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: objective-c
osx_image: xcode9.4
osx_image: xcode10

env:
global:
Expand All @@ -16,6 +16,7 @@ env:

before_install:
- bundle install
- gem install cocoapods --pre
- brew update
- brew outdated xctool || brew upgrade xctool
script:
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ The changelog for **SwifterSwift**. Also see the [releases](https://github.com/S

# Upcoming release

### Added
### Changed
### Fixed
### Deprecated
### Removed
### Security

---

# [v4.5.0](https://github.com/SwifterSwift/SwifterSwift/releases/tag/4.5.0)

### Added
- **CGVector**
- Added `angle` computed property to get the angle of the vector (in radians). [#527](https://github.com/SwifterSwift/SwifterSwift/pull/527) by [moyerr](https://github.com/moyerr)
Expand Down
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let package = Package(
.library(
name: "SwifterSwift",
targets: ["Extensions"])
],
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
Expand All @@ -21,5 +21,5 @@ let package = Package(
.target(
name: "Extensions",
dependencies: [])
]
]
)
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
SwifterSwift is a collection of **over 500 native Swift extensions**, with handy methods, syntactic sugar, and performance improvements for wide range of primitive data types, UIKit and Cocoa classes –over 500 in 1– for iOS, macOS, tvOS, watchOS and Linux.


### [Whats New in v4.4.0?](https://github.com/SwifterSwift/SwifterSwift/blob/master/CHANGELOG.md#v440)
### [Whats New in v4.5.0?](https://github.com/SwifterSwift/SwifterSwift/blob/master/CHANGELOG.md#v450)

## Requirements:
- **iOS** 8.0+ / **tvOS** 9.0+ / **watchOS** 2.0+ / **macOS** 10.10+ / **Ubuntu** 14.04+
- Swift 4.0+
- Swift 4.2+



Expand Down
88 changes: 44 additions & 44 deletions Sources/Extensions/AppKit/NSImageExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,59 +12,59 @@ import Cocoa
// MARK: - Methods
extension NSImage {

/// SwifterSwift: NSImage scaled to maximum size with respect to aspect ratio
///
/// - Parameter toMaxSize: maximum size
/// - Returns: scaled NSImage
public func scaled(toMaxSize: NSSize) -> NSImage {
var ratio: Float = 0.0
let imageWidth = Float(self.size.width)
let imageHeight = Float(self.size.height)
let maxWidth = Float(toMaxSize.width)
let maxHeight = Float(toMaxSize.height)
/// SwifterSwift: NSImage scaled to maximum size with respect to aspect ratio
///
/// - Parameter toMaxSize: maximum size
/// - Returns: scaled NSImage
public func scaled(toMaxSize: NSSize) -> NSImage {
var ratio: Float = 0.0
let imageWidth = Float(self.size.width)
let imageHeight = Float(self.size.height)
let maxWidth = Float(toMaxSize.width)
let maxHeight = Float(toMaxSize.height)

// Get ratio (landscape or portrait)
if imageWidth > imageHeight {
// Landscape
ratio = maxWidth / imageWidth
} else {
// Portrait
ratio = maxHeight / imageHeight
}
// Get ratio (landscape or portrait)
if imageWidth > imageHeight {
// Landscape
ratio = maxWidth / imageWidth
} else {
// Portrait
ratio = maxHeight / imageHeight
}

// Calculate new size based on the ratio
let newWidth = imageWidth * ratio
let newHeight = imageHeight * ratio
// Calculate new size based on the ratio
let newWidth = imageWidth * ratio
let newHeight = imageHeight * ratio

// Create a new NSSize object with the newly calculated size
let newSize: NSSize = NSSize(width: Int(newWidth), height: Int(newHeight))
// Create a new NSSize object with the newly calculated size
let newSize: NSSize = NSSize(width: Int(newWidth), height: Int(newHeight))

// Cast the NSImage to a CGImage
var imageRect: CGRect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
let imageRef = self.cgImage(forProposedRect: &imageRect, context: nil, hints: nil)
// Cast the NSImage to a CGImage
var imageRect: CGRect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
let imageRef = self.cgImage(forProposedRect: &imageRect, context: nil, hints: nil)

// Create NSImage from the CGImage using the new size
let imageWithNewSize = NSImage(cgImage: imageRef!, size: newSize)
// Create NSImage from the CGImage using the new size
let imageWithNewSize = NSImage(cgImage: imageRef!, size: newSize)

// Return the new image
return imageWithNewSize
}
// Return the new image
return imageWithNewSize
}

/// SwifterSwift: Write NSImage to url.
///
/// - Parameters:
/// - url: Desired file URL.
/// - type: Type of image (default is .jpeg).
/// - compressionFactor: used only for JPEG files. The value is a float between 0.0 and 1.0, with 1.0 resulting in no compression and 0.0 resulting in the maximum compression possible.
public func write(to url: URL, fileType type: NSBitmapImageRep.FileType = .jpeg, compressionFactor: NSNumber = 1.0) {
// https://stackoverflow.com/a/45042611/3882644
/// SwifterSwift: Write NSImage to url.
///
/// - Parameters:
/// - url: Desired file URL.
/// - type: Type of image (default is .jpeg).
/// - compressionFactor: used only for JPEG files. The value is a float between 0.0 and 1.0, with 1.0 resulting in no compression and 0.0 resulting in the maximum compression possible.
public func write(to url: URL, fileType type: NSBitmapImageRep.FileType = .jpeg, compressionFactor: NSNumber = 1.0) {
// https://stackoverflow.com/a/45042611/3882644

guard let data = tiffRepresentation else { return }
guard let imageRep = NSBitmapImageRep(data: data) else { return }
guard let data = tiffRepresentation else { return }
guard let imageRep = NSBitmapImageRep(data: data) else { return }

guard let imageData = imageRep.representation(using: type, properties: [.compressionFactor: compressionFactor]) else { return }
try? imageData.write(to: url)
}
guard let imageData = imageRep.representation(using: type, properties: [.compressionFactor: compressionFactor]) else { return }
try? imageData.write(to: url)
}

}

Expand Down

0 comments on commit b24e70f

Please sign in to comment.