Skip to content

Commit

Permalink
fix: update nuke
Browse files Browse the repository at this point in the history
  • Loading branch information
gtokman committed May 26, 2024
1 parent 45585d1 commit b40b291
Show file tree
Hide file tree
Showing 34 changed files with 771 additions and 869 deletions.
76 changes: 0 additions & 76 deletions ios/Nuke/AsyncImageTask.swift

This file was deleted.

6 changes: 4 additions & 2 deletions ios/Nuke/Caching/Cache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ final class Cache<Key: Hashable, Value>: @unchecked Sendable {
self.memoryPressure.resume()

#if os(iOS) || os(tvOS) || os(visionOS)
registerForEnterBackground()
Task {
await registerForEnterBackground()
}
#endif
}

Expand All @@ -68,7 +70,7 @@ final class Cache<Key: Hashable, Value>: @unchecked Sendable {
}

#if os(iOS) || os(tvOS) || os(visionOS)
private func registerForEnterBackground() {
@MainActor private func registerForEnterBackground() {
notificationObserver = NotificationCenter.default.addObserver(forName: UIApplication.didEnterBackgroundNotification, object: nil, queue: nil) { [weak self] _ in
self?.clearCacheOnEnterBackground()
}
Expand Down
4 changes: 2 additions & 2 deletions ios/Nuke/Caching/ImageCaching.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ public struct ImageCacheKey: Hashable, Sendable {
// This is faster than using AnyHashable (and it shows in performance tests).
enum Inner: Hashable, Sendable {
case custom(String)
case `default`(CacheKey)
case `default`(MemoryCacheKey)
}

public init(key: String) {
self.key = .custom(key)
}

public init(request: ImageRequest) {
self.key = .default(request.makeImageCacheKey())
self.key = .default(MemoryCacheKey(request))
}
}
2 changes: 1 addition & 1 deletion ios/Nuke/Decoding/ImageDecoderRegistry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public struct ImageDecodingContext: @unchecked Sendable {
public var urlResponse: URLResponse?
public var cacheType: ImageResponse.CacheType?

public init(request: ImageRequest, data: Data, isCompleted: Bool, urlResponse: URLResponse?, cacheType: ImageResponse.CacheType?) {
public init(request: ImageRequest, data: Data, isCompleted: Bool = true, urlResponse: URLResponse? = nil, cacheType: ImageResponse.CacheType? = nil) {
self.request = request
self.data = data
self.isCompleted = isCompleted
Expand Down
20 changes: 11 additions & 9 deletions ios/Nuke/Encoding/ImageEncoders+ImageIO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,38 @@ extension ImageEncoders {
self.compressionRatio = compressionRatio
}

@Atomic private static var availability = [AssetType: Bool]()
private static let availability = Atomic<[AssetType: Bool]>(value: [:])

/// Returns `true` if the encoding is available for the given format on
/// the current hardware. Some of the most recent formats might not be
/// available so its best to check before using them.
public static func isSupported(type: AssetType) -> Bool {
if let isAvailable = availability[type] {
if let isAvailable = availability.value[type] {
return isAvailable
}
let isAvailable = CGImageDestinationCreateWithData(
NSMutableData() as CFMutableData, type.rawValue as CFString, 1, nil
) != nil
availability[type] = isAvailable
availability.withLock { $0[type] = isAvailable }
return isAvailable
}

public func encode(_ image: PlatformImage) -> Data? {
let data = NSMutableData()
guard let source = image.cgImage,
let data = CFDataCreateMutable(nil, 0),
let destination = CGImageDestinationCreateWithData(data, type.rawValue as CFString, 1, nil) else {
return nil
}
var options: [CFString: Any] = [
kCGImageDestinationLossyCompressionQuality: compressionRatio
]
#if canImport(UIKit)
options[kCGImagePropertyOrientation] = CGImagePropertyOrientation(image.imageOrientation).rawValue
#endif
guard let source = image.cgImage,
let destination = CGImageDestinationCreateWithData(data as CFMutableData, type.rawValue as CFString, 1, nil) else {
return nil
}
CGImageDestinationAddImage(destination, source, options as CFDictionary)
CGImageDestinationFinalize(destination)
guard CGImageDestinationFinalize(destination) else {
return nil
}
return data as Data
}
}
Expand Down
2 changes: 1 addition & 1 deletion ios/Nuke/ImageRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public struct ImageRequest: CustomStringConvertible, Sendable, ExpressibleByStri
/// (``ImageProcessors/Resize``).
///
/// - note: You must be using the default image decoder to make it work.
public static let thumbnailKey: ImageRequest.UserInfoKey = "github.com/kean/nuke/thumbmnailKey"
public static let thumbnailKey: ImageRequest.UserInfoKey = "github.com/kean/nuke/thumbnail"
}

/// Thumbnail options.
Expand Down
10 changes: 6 additions & 4 deletions ios/Nuke/ImageResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

import Foundation

#if !os(macOS)
import UIKit.UIImage
#else
import AppKit.NSImage
#if canImport(UIKit)
import UIKit
#endif

#if canImport(AppKit)
import AppKit
#endif

/// An image response that contains a fetched image and some metadata.
Expand Down
Loading

0 comments on commit b40b291

Please sign in to comment.