Skip to content

Commit ac6e29d

Browse files
Yingtao GuoYingtao Guo
authored andcommitted
Add getBitmapInfo for CGImgeExtension
Improve the logic of getting bitmapInfo for creating CGContext
1 parent 26d174c commit ac6e29d

File tree

3 files changed

+116
-30
lines changed

3 files changed

+116
-30
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,4 @@ fastlane/Preview.html
6767
fastlane/screenshots/**/*.png
6868
fastlane/test_output
6969
/Example/Podfile.lock
70+
.DS_Store
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "1240"
4+
version = "1.3">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "5FC10B10217A9EDF00582874"
18+
BuildableName = "MantisExample.app"
19+
BlueprintName = "MantisExample"
20+
ReferencedContainer = "container:MantisExample.xcodeproj">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
</BuildActionEntries>
24+
</BuildAction>
25+
<TestAction
26+
buildConfiguration = "Debug"
27+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
28+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29+
shouldUseLaunchSchemeArgsEnv = "YES">
30+
<Testables>
31+
</Testables>
32+
</TestAction>
33+
<LaunchAction
34+
buildConfiguration = "Debug"
35+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
36+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
37+
launchStyle = "0"
38+
useCustomWorkingDirectory = "NO"
39+
ignoresPersistentStateOnLaunch = "NO"
40+
debugDocumentVersioning = "YES"
41+
debugServiceExtension = "internal"
42+
allowLocationSimulation = "YES">
43+
<BuildableProductRunnable
44+
runnableDebuggingMode = "0">
45+
<BuildableReference
46+
BuildableIdentifier = "primary"
47+
BlueprintIdentifier = "5FC10B10217A9EDF00582874"
48+
BuildableName = "MantisExample.app"
49+
BlueprintName = "MantisExample"
50+
ReferencedContainer = "container:MantisExample.xcodeproj">
51+
</BuildableReference>
52+
</BuildableProductRunnable>
53+
</LaunchAction>
54+
<ProfileAction
55+
buildConfiguration = "Release"
56+
shouldUseLaunchSchemeArgsEnv = "YES"
57+
savedToolIdentifier = ""
58+
useCustomWorkingDirectory = "NO"
59+
debugDocumentVersioning = "YES">
60+
<BuildableProductRunnable
61+
runnableDebuggingMode = "0">
62+
<BuildableReference
63+
BuildableIdentifier = "primary"
64+
BlueprintIdentifier = "5FC10B10217A9EDF00582874"
65+
BuildableName = "MantisExample.app"
66+
BlueprintName = "MantisExample"
67+
ReferencedContainer = "container:MantisExample.xcodeproj">
68+
</BuildableReference>
69+
</BuildableProductRunnable>
70+
</ProfileAction>
71+
<AnalyzeAction
72+
buildConfiguration = "Debug">
73+
</AnalyzeAction>
74+
<ArchiveAction
75+
buildConfiguration = "Release"
76+
revealArchiveInOrganizer = "YES">
77+
</ArchiveAction>
78+
</Scheme>

Sources/Mantis/Extensions/CGImageExtensions.swift

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,36 +27,43 @@ extension CGImage {
2727
let outputSize = CGSize(width: expectedWidth, height: expectedHeight)
2828
let bitmapBytesPerRow = 0
2929

30-
var context = CGContext(data: nil,
30+
func getBitmapInfo() -> UInt32 {
31+
if colorSpaceRef.model == .rgb {
32+
switch(bitsPerPixel, bitsPerComponent) {
33+
case (16, 5):
34+
return CGImageAlphaInfo.noneSkipFirst.rawValue
35+
case (32, 8):
36+
return CGImageAlphaInfo.premultipliedLast.rawValue
37+
case (32, 10):
38+
if #available(iOS 12, macOS 10.14, *) {
39+
return CGImageAlphaInfo.alphaOnly.rawValue | CGImagePixelFormatInfo.RGBCIF10.rawValue
40+
} else {
41+
return bitmapInfo.rawValue
42+
}
43+
case (64, 16):
44+
return CGImageAlphaInfo.premultipliedLast.rawValue
45+
case (128, 32):
46+
return CGImageAlphaInfo.premultipliedLast.rawValue | CGBitmapInfo.floatComponents.rawValue
47+
default:
48+
return bitmapInfo.rawValue
49+
}
50+
}
51+
52+
return bitmapInfo.rawValue
53+
}
54+
55+
guard let context = CGContext(data: nil,
3156
width: Int(outputSize.width),
3257
height: Int(outputSize.height),
33-
bitsPerComponent: self.bitsPerComponent,
58+
bitsPerComponent: bitsPerComponent,
3459
bytesPerRow: bitmapBytesPerRow,
3560
space: colorSpaceRef,
36-
bitmapInfo: self.bitmapInfo.rawValue)
37-
38-
if context == nil {
39-
context = CGContext(data: nil,
40-
width: Int(outputSize.width),
41-
height: Int(outputSize.height),
42-
bitsPerComponent: self.bitsPerComponent,
43-
bytesPerRow: bitmapBytesPerRow,
44-
space: colorSpaceRef,
45-
bitmapInfo:CGImageAlphaInfo.premultipliedLast.rawValue)
46-
}
47-
48-
if context == nil {
49-
context = CGContext(data: nil,
50-
width: Int(outputSize.width),
51-
height: Int(outputSize.height),
52-
bitsPerComponent: self.bitsPerComponent,
53-
bytesPerRow: bitmapBytesPerRow,
54-
space: colorSpaceRef,
55-
bitmapInfo:CGImageAlphaInfo.premultipliedFirst.rawValue)
61+
bitmapInfo: getBitmapInfo()) else {
62+
return self
5663
}
57-
58-
context?.setFillColor(UIColor.clear.cgColor)
59-
context?.fill(CGRect(x: 0,
64+
65+
context.setFillColor(UIColor.clear.cgColor)
66+
context.fill(CGRect(x: 0,
6067
y: 0,
6168
width: outputSize.width,
6269
height: outputSize.height))
@@ -66,15 +73,15 @@ extension CGImage {
6673
uiCoords = uiCoords.translatedBy(x: cropSize.width / 2, y: cropSize.height / 2)
6774
uiCoords = uiCoords.scaledBy(x: 1.0, y: -1.0)
6875

69-
context?.concatenate(uiCoords)
70-
context?.concatenate(transform)
71-
context?.scaleBy(x: 1.0, y: -1.0)
72-
context?.draw(self, in: CGRect(x: (-imageViewSize.width / 2),
76+
context.concatenate(uiCoords)
77+
context.concatenate(transform)
78+
context.scaleBy(x: 1.0, y: -1.0)
79+
context.draw(self, in: CGRect(x: (-imageViewSize.width / 2),
7380
y: (-imageViewSize.height / 2),
7481
width: imageViewSize.width,
7582
height: imageViewSize.height))
7683

77-
let result = context?.makeImage()
84+
let result = context.makeImage()
7885

7986
return result
8087
}

0 commit comments

Comments
 (0)