Skip to content

Commit

Permalink
Move ImageContainer to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Jun 15, 2022
1 parent f0f5575 commit c8c2472
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 93 deletions.
4 changes: 4 additions & 0 deletions Nuke.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@
0C91B0F42438E38B007F9100 /* CompositionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C91B0F32438E38B007F9100 /* CompositionTests.swift */; };
0C91B0F62438E3CB007F9100 /* GaussianBlurTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C91B0F52438E3CB007F9100 /* GaussianBlurTests.swift */; };
0C91B0F82438E84E007F9100 /* fixture-tiny.jpeg in Resources */ = {isa = PBXBuildFile; fileRef = 0C91B0F72438E84E007F9100 /* fixture-tiny.jpeg */; };
0C933E642859686D00F43606 /* ImageContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C933E632859686D00F43606 /* ImageContainer.swift */; };
0C95FD542571B278008D4FC2 /* baseline.webp in Resources */ = {isa = PBXBuildFile; fileRef = 0C95FD532571B278008D4FC2 /* baseline.webp */; };
0C973E141D9FDB9F00C00AD9 /* Nuke.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 0C9174901BAE99EE004A7905 /* Nuke.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
0C9B6E7620B9F3E2001924B8 /* ImagePipelineCoalescingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C9B6E7520B9F3E2001924B8 /* ImagePipelineCoalescingTests.swift */; };
Expand Down Expand Up @@ -478,6 +479,7 @@
0C91B0F32438E38B007F9100 /* CompositionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CompositionTests.swift; sourceTree = "<group>"; };
0C91B0F52438E3CB007F9100 /* GaussianBlurTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GaussianBlurTests.swift; sourceTree = "<group>"; };
0C91B0F72438E84E007F9100 /* fixture-tiny.jpeg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = "fixture-tiny.jpeg"; sourceTree = "<group>"; };
0C933E632859686D00F43606 /* ImageContainer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageContainer.swift; sourceTree = "<group>"; };
0C94466D1D47EC0E006DB314 /* ImageViewExtensionsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageViewExtensionsTests.swift; sourceTree = "<group>"; };
0C95FD532571B278008D4FC2 /* baseline.webp */ = {isa = PBXFileReference; lastKnownFileType = file; path = baseline.webp; sourceTree = "<group>"; };
0C97DD9E284C00EA00F55FDA /* Nuke 11 Migration Guide.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = "Nuke 11 Migration Guide.md"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1163,6 +1165,7 @@
children = (
0C0FD5D91CA47FE1002A78FB /* ImageRequest.swift */,
0C063F93266524190018F2C2 /* ImageResponse.swift */,
0C933E632859686D00F43606 /* ImageContainer.swift */,
0C86AB69228B3B5100A81BA1 /* ImageTask.swift */,
0CBA07842852DA7A00CE29F4 /* Pipeline */,
0CA4ECCB26E68F9900BAC8E5 /* Loading */,
Expand Down Expand Up @@ -1827,6 +1830,7 @@
0CA4ECCD26E68FA100BAC8E5 /* DataLoading.swift in Sources */,
0CA4ECAF26E683FD00BAC8E5 /* ImageEncoders+Default.swift in Sources */,
0CC36A2525B8BC4900811018 /* Operation.swift in Sources */,
0C933E642859686D00F43606 /* ImageContainer.swift in Sources */,
0C78A2A7263F4E680051E0FF /* ImagePipelineCache.swift in Sources */,
0CA4ECD026E68FC000BAC8E5 /* DataCaching.swift in Sources */,
0CA4ECCA26E6868300BAC8E5 /* ImageProcessingOptions.swift in Sources */,
Expand Down
94 changes: 94 additions & 0 deletions Sources/Nuke/Core/ImageContainer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// The MIT License (MIT)
//
// Copyright (c) 2015-2022 Alexander Grebenyuk (github.com/kean).

#if !os(watchOS)
import AVKit
#endif

import Foundation

#if !os(macOS)
import UIKit.UIImage
/// Alias for `UIImage`.
public typealias PlatformImage = UIImage
#else
import AppKit.NSImage
/// Alias for `NSImage`.
public typealias PlatformImage = NSImage
#endif

/// An image container with an image and associated metadata.
public struct ImageContainer: @unchecked Sendable {
#if os(macOS)
/// A fetched image.
public var image: NSImage
#else
/// A fetched image.
public var image: UIImage
#endif

/// An image type.
public var type: AssetType?

/// Returns `true` if the image in the container is a preview of the image.
public var isPreview: Bool

/// Contains the original image `data`, but only if the decoder decides to
/// attach it to the image.
///
/// The default decoder (`ImageDecoders.Default`) attaches data to GIFs to
/// allow to display them using a rendering engine of your choice.
///
/// - note: The `data`, along with the image container itself gets stored
/// in the memory cache.
public var data: Data?

#if !os(watchOS)
/// Represents in-memory video asset.
public var asset: AVAsset?
#endif

/// An metadata provided by the user.
public var userInfo: [UserInfoKey: Any]

/// Initializes the container with the given image.
public init(image: PlatformImage, type: AssetType? = nil, isPreview: Bool = false, data: Data? = nil, userInfo: [UserInfoKey: Any] = [:]) {
self.image = image
self.type = type
self.isPreview = isPreview
self.data = data
self.userInfo = userInfo

#if !os(watchOS)
if type?.isVideo == true {
self.asset = data.flatMap { AVDataAsset(data: $0, type: type) }
}
#endif
}

func map(_ closure: (PlatformImage) throws -> PlatformImage) rethrows -> ImageContainer {
var copy = self
copy.image = try closure(image)
return copy
}

/// A key use in `userInfo`.
public struct UserInfoKey: Hashable, ExpressibleByStringLiteral, Sendable {
public let rawValue: String

public init(_ rawValue: String) {
self.rawValue = rawValue
}

public init(stringLiteral value: String) {
self.rawValue = value
}

// For internal purposes.
static let isThumbnailKey: UserInfoKey = "com.github/kean/nuke/skip-decompression"

/// A user info key to get the scan number (Int).
public static let scanNumberKey: UserInfoKey = "com.github/kean/nuke/scan-number"
}
}
97 changes: 4 additions & 93 deletions Sources/Nuke/Core/ImageResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,14 @@
//
// Copyright (c) 2015-2022 Alexander Grebenyuk (github.com/kean).

#if !os(watchOS)
import AVKit
#endif

import Foundation

#if !os(macOS)
import UIKit.UIImage
/// Alias for `UIImage`.
public typealias PlatformImage = UIImage
#else
import AppKit.NSImage
/// Alias for `NSImage`.
public typealias PlatformImage = NSImage
#endif

// MARK: - ImageResponse

/// An image response that contains a fetched image and some metadata.
public struct ImageResponse: @unchecked Sendable {
/// An image container with an image and associated metadata.
Expand Down Expand Up @@ -52,96 +42,17 @@ public struct ImageResponse: @unchecked Sendable {
self.cacheType = cacheType
}

func map(_ transform: (ImageContainer) throws -> ImageContainer) rethrows -> ImageResponse {
try autoreleasepool {
var response = self
response.container = try transform(response.container)
return response
}
}

/// A cache type.
public enum CacheType: Sendable {
/// Memory cache (see `ImageCaching`)
case memory
/// Disk cache (see `DataCaching`)
case disk
}
}

// MARK: - ImageContainer

/// An image container with an image and associated metadata.
public struct ImageContainer: @unchecked Sendable {
#if os(macOS)
/// A fetched image.
public var image: NSImage
#else
/// A fetched image.
public var image: UIImage
#endif

/// An image type.
public var type: AssetType?

/// Returns `true` if the image in the container is a preview of the image.
public var isPreview: Bool

/// Contains the original image `data`, but only if the decoder decides to
/// attach it to the image.
///
/// The default decoder (`ImageDecoders.Default`) attaches data to GIFs to
/// allow to display them using a rendering engine of your choice.
///
/// - note: The `data`, along with the image container itself gets stored
/// in the memory cache.
public var data: Data?

#if !os(watchOS)
/// Represents in-memory video asset.
public var asset: AVAsset?
#endif

/// An metadata provided by the user.
public var userInfo: [UserInfoKey: Any]

/// Initializes the container with the given image.
public init(image: PlatformImage, type: AssetType? = nil, isPreview: Bool = false, data: Data? = nil, userInfo: [UserInfoKey: Any] = [:]) {
self.image = image
self.type = type
self.isPreview = isPreview
self.data = data
self.userInfo = userInfo

#if !os(watchOS)
if type?.isVideo == true {
self.asset = data.flatMap { AVDataAsset(data: $0, type: type) }
}
#endif
}

func map(_ closure: (PlatformImage) throws -> PlatformImage) rethrows -> ImageContainer {
var copy = self
copy.image = try closure(image)
return copy
}

/// A key use in `userInfo`.
public struct UserInfoKey: Hashable, ExpressibleByStringLiteral, Sendable {
public let rawValue: String

public init(_ rawValue: String) {
self.rawValue = rawValue
}

public init(stringLiteral value: String) {
self.rawValue = value
}

// For internal purposes.
static let isThumbnailKey: UserInfoKey = "com.github/kean/nuke/skip-decompression"

/// A user info key to get the scan number (Int).
public static let scanNumberKey: UserInfoKey = "com.github/kean/nuke/scan-number"
func map(_ transform: (ImageContainer) throws -> ImageContainer) rethrows -> ImageResponse {
var response = self
response.container = try transform(response.container)
return response
}
}

0 comments on commit c8c2472

Please sign in to comment.