Skip to content

Commit

Permalink
Fix Concurrency errors
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixHerrmann committed Sep 8, 2024
1 parent 64b09b5 commit ed0a978
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ Higher qualities will give better results at the cost of performance.
| ------------------- | ----------- |
| low | 50 pixel |
| medium | 100 pixel |
| high | 250 pixel |
| high | 250 pixel |
| full | no scaling |
| custom (Swift only) | given value |
Expand Down
23 changes: 19 additions & 4 deletions Sources/UIImageColors/NSImage+Colors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import AppKit
extension NSImage {

/// Represents the most common colors inside an image.
public struct Colors {
public struct Colors: Sendable {

/// The most common, non-black/white color.
public let background: NSColor
Expand Down Expand Up @@ -59,11 +59,26 @@ extension NSImage {
/// - Parameters:
/// - quality: The scale quality. Default is `ScaleQuality.high`.
/// - completion: The completion block with the ``Colors``.
public func getColors(quality: ScaleQuality = .high, _ completion: @escaping (Colors?) -> Void) {
public func getColors(quality: ScaleQuality = .high, _ completion: @MainActor @Sendable @escaping (Colors?) -> Void) {
let scaledSize = quality._scaleSize(size)
guard let resizedCGImage = _resizedCGImage(size: scaledSize) else {
DispatchQueue.main.async {
completion(nil)
}
return
}

DispatchQueue.global().async {
let result = self.getColors(quality: quality)
let colors: Colors?
if let counter = _ColorCounter(cgImage: resizedCGImage) {
let analyzer = _ColorAnalyzer(counter: counter)
colors = analyzer?.colors
} else {
colors = nil
}

DispatchQueue.main.async {
completion(result)
completion(colors)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/UIImageColors/ScaleQuality.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import CoreGraphics
// MARK: - ScaleQuality

/// The quality the original image should be scaled to.
public enum ScaleQuality {
public enum ScaleQuality: Sendable {

/// Scales the image to 50 pixel.
case low
Expand Down
4 changes: 2 additions & 2 deletions Sources/UIImageColors/UIImage+Colors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import UIKit
extension UIImage {

/// Represents the most common colors inside an image.
public struct Colors {
public struct Colors: Sendable {

/// The most common, non-black/white color.
public let background: UIColor
Expand Down Expand Up @@ -59,7 +59,7 @@ extension UIImage {
/// - Parameters:
/// - quality: The scale quality. Default is `ScaleQuality.high`.
/// - completion: The completion block with the ``Colors``.
public func getColors(quality: ScaleQuality = .high, _ completion: @escaping (Colors?) -> Void) {
public func getColors(quality: ScaleQuality = .high, _ completion: @MainActor @Sendable @escaping (Colors?) -> Void) {
DispatchQueue.global().async {
let result = self.getColors(quality: quality)
DispatchQueue.main.async {
Expand Down
2 changes: 1 addition & 1 deletion Sources/UIImageColorsObjc/NSImageColorsObjc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ extension NSImage {
/// - quality: The scale quality.
/// - completion: The completion block with the ``NSImageColorsObjc``.
@objc(getColorsWithQuality:completion:)
public func getColorsObjc(quality: UIImageColorsScaleQuality, completion: @escaping (NSImageColorsObjc?) -> Void) {
public func getColorsObjc(quality: UIImageColorsScaleQuality, completion: @Sendable @escaping (NSImageColorsObjc?) -> Void) {
getColors(quality: quality._scaleQuality) { colors in
if let colors = colors {
completion(NSImageColorsObjc(background: colors.background, primary: colors.primary, secondary: colors.secondary, detail: colors.detail))
Expand Down
2 changes: 1 addition & 1 deletion Sources/UIImageColorsObjc/UIImageColorsObjc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ extension UIImage {
/// - quality: The scale quality.
/// - completion: The completion block with the ``UIImageColorsObjc``.
@objc(getColorsWithQuality:completion:)
public func getColorsObjc(quality: UIImageColorsScaleQuality, completion: @escaping (UIImageColorsObjc?) -> Void) {
public func getColorsObjc(quality: UIImageColorsScaleQuality, completion: @Sendable @escaping (UIImageColorsObjc?) -> Void) {
getColors(quality: quality._scaleQuality) { colors in
if let colors = colors {
completion(UIImageColorsObjc(background: colors.background, primary: colors.primary, secondary: colors.secondary, detail: colors.detail))
Expand Down
1 change: 1 addition & 0 deletions Tests/UIImageColorsTests/UIImageColorsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ final class UIImageColorsTests: XCTestCase {
#endif
}

@MainActor
func testInternalScaling() throws {
#if canImport(UIKit)
let cgImage = try XCTUnwrap(image.cgImage)
Expand Down

0 comments on commit ed0a978

Please sign in to comment.