Skip to content

Commit

Permalink
Update gif generation
Browse files Browse the repository at this point in the history
  • Loading branch information
EyreFree committed Sep 20, 2019
1 parent 65d44fa commit 6926207
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 60 deletions.
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "ApolloZhu/swift_qrcodejs" "1.1.1"
github "ApolloZhu/swift_qrcodejs" "1.1.2"
1 change: 1 addition & 0 deletions Examples/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ inhibit_all_warnings!
target 'Dependency' do
pod 'SnapKit', '5.0.1'
pod 'EFColorPicker', '5.1.0'
pod 'EFFoundation', '0.8.2'
end
8 changes: 7 additions & 1 deletion Examples/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
PODS:
- EFColorPicker (5.1.0)
- EFFoundation (0.8.2):
- EFFoundation/Core (= 0.8.2)
- EFFoundation/Core (0.8.2)
- SnapKit (5.0.1)

DEPENDENCIES:
- EFColorPicker (= 5.1.0)
- EFFoundation (= 0.8.2)
- SnapKit (= 5.0.1)

SPEC REPOS:
https://github.com/cocoapods/specs.git:
- EFColorPicker
- EFFoundation
- SnapKit

SPEC CHECKSUMS:
EFColorPicker: 6fa37bd9cde0f8dc58762698f1e9809f554dfe6c
EFFoundation: 9ca4a4f3fc6222a4255ad3a8b052ea2307aed5b2
SnapKit: 97b92857e3df3a0c71833cce143274bf6ef8e5eb

PODFILE CHECKSUM: dcdbcf0619146db8e26be75848200790107778e9
PODFILE CHECKSUM: b27516bc2fad10c028890a6fd13c68658c972545

COCOAPODS: 1.7.5
File renamed without changes.
25 changes: 9 additions & 16 deletions Examples/iOS/GeneratorController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -932,14 +932,9 @@ extension GeneratorController {
case 6:
rightImageView.image = icon
case 8:
rightImageView.stopAnimating()
switch watermark {
case .gif(let dataGIF)?:
guard let source = CGImageSourceCreateWithData(dataGIF as CFData, nil)
else { break }
let images = source.toCGImages().map(UIImage.init(cgImage:))
rightImageView.animationImages = images
rightImageView.startAnimating()
rightImageView.loadGif(data: dataGIF)
case .normal(let image)?:
rightImageView.image = image
case nil:
Expand Down Expand Up @@ -1159,11 +1154,7 @@ class ShowController: UIViewController {
view.addSubview(imageView)
switch image {
case .gif(let dataGIF)?:
guard let source = CGImageSourceCreateWithData(dataGIF as CFData, nil)
else { break }
let images = source.toCGImages().map(UIImage.init(cgImage:))
imageView.animationImages = images
imageView.startAnimating()
imageView.loadGif(data: dataGIF)
case .normal(let uiImage)?:
imageView.image = uiImage
case nil:
Expand Down Expand Up @@ -1323,12 +1314,14 @@ enum CustomPhotoAlbum {
PHPhotoLibrary.shared().performChanges({
let assetChangeRequest: PHAssetChangeRequest?
switch image {
case .gif:
guard let fileURL = EFQRCode.tempResultPath else {
finish(NSLocalizedString("Can't create a temporary gif file for export", comment: "EFQRCode.tempResultPath is nil"))
errored = true
return
case .gif(let data):
guard let documentsDirectoryURL: URL? = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true),
let fileURL: URL = documentsDirectoryURL?.appendingPathComponent("EFQRCode_temp.gif") else {
finish(NSLocalizedString("Can't create a temporary gif file for export", comment: "FileURL is nil"))
errored = true
return
}
try? data.write(to: fileURL)
assetChangeRequest = .creationRequestForAssetFromImage(atFileURL: fileURL)
case .normal(let image):
assetChangeRequest = .creationRequestForAsset(from: image)
Expand Down
File renamed without changes.
53 changes: 38 additions & 15 deletions Examples/iOS/iOS Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
120B78031EA3968700D34440 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 120B78021EA3968700D34440 /* ViewController.swift */; };
120B78051EA3969600D34440 /* RecognizerController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 120B78041EA3969600D34440 /* RecognizerController.swift */; };
120B78071EA396A000D34440 /* GeneratorController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 120B78061EA396A000D34440 /* GeneratorController.swift */; };
120B780A1EA3977300D34440 /* UIImage+.swift in Sources */ = {isa = PBXBuildFile; fileRef = 120B78091EA3977300D34440 /* UIImage+.swift */; };
120B780A1EA3977300D34440 /* UIImage+iOS.swift in Sources */ = {isa = PBXBuildFile; fileRef = 120B78091EA3977300D34440 /* UIImage+iOS.swift */; };
120B78411EA39E1B00D34440 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 120B78401EA39E1B00D34440 /* AppDelegate.swift */; };
120B785D1EA3A9FE00D34440 /* EFQRCode.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 120B784E1EA3A9E100D34440 /* EFQRCode.framework */; };
120B785E1EA3AA1100D34440 /* EFQRCode.framework in Copy Frameworks */ = {isa = PBXBuildFile; fileRef = 120B784E1EA3A9E100D34440 /* EFQRCode.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
12981D8A1F99F75D00156E1E /* CGFloat+.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12981D891F99F75D00156E1E /* CGFloat+.swift */; };
12410FE22334DE47002A574B /* UIImage+.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12410FC82334DE46002A574B /* UIImage+.swift */; };
12410FE52334DE47002A574B /* UIImageView+.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12410FCB2334DE46002A574B /* UIImageView+.swift */; };
12410FF22334E7C5002A574B /* EFFoundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12410FF12334E7C5002A574B /* EFFoundation.swift */; };
12981D8A1F99F75D00156E1E /* CGFloat+iOS.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12981D891F99F75D00156E1E /* CGFloat+iOS.swift */; };
12A22CEB2210656D0063EE1F /* StorageUserDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12A22CEA2210656D0063EE1F /* StorageUserDefaults.swift */; };
12A22D0322106FFF0063EE1F /* StorageUserDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12A22CEA2210656D0063EE1F /* StorageUserDefaults.swift */; };
12BC1A602328C0B500B6EB5A /* ConstraintConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12BC1A3C2328C0B200B6EB5A /* ConstraintConfig.swift */; };
Expand Down Expand Up @@ -221,10 +224,13 @@
120B78021EA3968700D34440 /* ViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = SOURCE_ROOT; };
120B78041EA3969600D34440 /* RecognizerController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RecognizerController.swift; sourceTree = SOURCE_ROOT; };
120B78061EA396A000D34440 /* GeneratorController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GeneratorController.swift; sourceTree = SOURCE_ROOT; };
120B78091EA3977300D34440 /* UIImage+.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIImage+.swift"; sourceTree = SOURCE_ROOT; };
120B78091EA3977300D34440 /* UIImage+iOS.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIImage+iOS.swift"; sourceTree = SOURCE_ROOT; };
120B78401EA39E1B00D34440 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = SOURCE_ROOT; };
120B78421EA3A9E100D34440 /* EFQRCode.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = EFQRCode.xcodeproj; path = ../../EFQRCode.xcodeproj; sourceTree = "<group>"; };
12981D891F99F75D00156E1E /* CGFloat+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CGFloat+.swift"; sourceTree = SOURCE_ROOT; };
12410FC82334DE46002A574B /* UIImage+.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "UIImage+.swift"; path = "../Pods/EFFoundation/EFFoundation/Core/UIImage+.swift"; sourceTree = "<group>"; };
12410FCB2334DE46002A574B /* UIImageView+.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "UIImageView+.swift"; path = "../Pods/EFFoundation/EFFoundation/Core/UIImageView+.swift"; sourceTree = "<group>"; };
12410FF12334E7C5002A574B /* EFFoundation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = EFFoundation.swift; path = ../Pods/EFFoundation/EFFoundation/Core/EFFoundation.swift; sourceTree = "<group>"; };
12981D891F99F75D00156E1E /* CGFloat+iOS.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CGFloat+iOS.swift"; sourceTree = SOURCE_ROOT; };
12A22CEA2210656D0063EE1F /* StorageUserDefaults.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StorageUserDefaults.swift; sourceTree = "<group>"; };
12BC1A3C2328C0B200B6EB5A /* ConstraintConfig.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ConstraintConfig.swift; path = ../Pods/SnapKit/Source/ConstraintConfig.swift; sourceTree = "<group>"; };
12BC1A3D2328C0B200B6EB5A /* ConstraintViewDSL.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ConstraintViewDSL.swift; path = ../Pods/SnapKit/Source/ConstraintViewDSL.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -345,8 +351,8 @@
120B77F91EA3951100D34440 /* Extension */ = {
isa = PBXGroup;
children = (
120B78091EA3977300D34440 /* UIImage+.swift */,
12981D891F99F75D00156E1E /* CGFloat+.swift */,
120B78091EA3977300D34440 /* UIImage+iOS.swift */,
12981D891F99F75D00156E1E /* CGFloat+iOS.swift */,
);
name = Extension;
sourceTree = "<group>";
Expand Down Expand Up @@ -375,6 +381,16 @@
name = Products;
sourceTree = "<group>";
};
12410FB72334DE35002A574B /* EFFoundation */ = {
isa = PBXGroup;
children = (
12410FF12334E7C5002A574B /* EFFoundation.swift */,
12410FC82334DE46002A574B /* UIImage+.swift */,
12410FCB2334DE46002A574B /* UIImageView+.swift */,
);
name = EFFoundation;
sourceTree = "<group>";
};
127B82272190846C00B737E8 /* EFColorPicker */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -448,8 +464,9 @@
12C322351F80F192003E9BF4 /* 3rd */ = {
isa = PBXGroup;
children = (
127B82282190848100B737E8 /* SnapKit */,
12410FB72334DE35002A574B /* EFFoundation */,
127B82272190846C00B737E8 /* EFColorPicker */,
127B82282190848100B737E8 /* SnapKit */,
);
name = 3rd;
sourceTree = "<group>";
Expand Down Expand Up @@ -633,15 +650,18 @@
TargetAttributes = {
5282BCE21FF3AD9000DFB36B = {
CreatedOnToolsVersion = 9.2;
DevelopmentTeam = P3X2725LYY;
ProvisioningStyle = Automatic;
};
5282BCEE1FF3AD9100DFB36B = {
CreatedOnToolsVersion = 9.2;
DevelopmentTeam = P3X2725LYY;
LastSwiftMigration = 1020;
ProvisioningStyle = Automatic;
};
F8111E0419A951050040E7D1 = {
CreatedOnToolsVersion = 6.0;
DevelopmentTeam = P3X2725LYY;
LastSwiftMigration = 1020;
};
};
Expand Down Expand Up @@ -792,10 +812,11 @@
12BC1A682328C0B500B6EB5A /* ConstraintMakerEditable.swift in Sources */,
12BC1A972328C0D900B6EB5A /* EFColorUtils.swift in Sources */,
120B78051EA3969600D34440 /* RecognizerController.swift in Sources */,
120B780A1EA3977300D34440 /* UIImage+.swift in Sources */,
120B780A1EA3977300D34440 /* UIImage+iOS.swift in Sources */,
12BC1A702328C0B500B6EB5A /* ConstraintInsetTarget.swift in Sources */,
12BC1A752328C0B500B6EB5A /* ConstraintRelatableTarget.swift in Sources */,
12BC1A6F2328C0B500B6EB5A /* ConstraintItem.swift in Sources */,
12410FF22334E7C5002A574B /* EFFoundation.swift in Sources */,
12BC1A7B2328C0B500B6EB5A /* ConstraintDirectionalInsetTarget.swift in Sources */,
12BC1A7D2328C0B500B6EB5A /* ConstraintMakerPriortizable.swift in Sources */,
12BC1A952328C0D900B6EB5A /* EFSliderView.swift in Sources */,
Expand All @@ -812,9 +833,10 @@
12BC1A792328C0B500B6EB5A /* ConstraintPriority.swift in Sources */,
12BC1A6E2328C0B500B6EB5A /* UILayoutSupport+Extensions.swift in Sources */,
120B78411EA39E1B00D34440 /* AppDelegate.swift in Sources */,
12981D8A1F99F75D00156E1E /* CGFloat+.swift in Sources */,
12981D8A1F99F75D00156E1E /* CGFloat+iOS.swift in Sources */,
12BC1A722328C0B500B6EB5A /* ConstraintMaker.swift in Sources */,
12BC1A7C2328C0B500B6EB5A /* ConstraintMakerFinalizable.swift in Sources */,
12410FE52334DE47002A574B /* UIImageView+.swift in Sources */,
12BC1A602328C0B500B6EB5A /* ConstraintConfig.swift in Sources */,
12BC1A672328C0B500B6EB5A /* Constraint.swift in Sources */,
12BC1A902328C0D900B6EB5A /* EFColorWheelView.swift in Sources */,
Expand All @@ -829,6 +851,7 @@
12BC1A7F2328C0B500B6EB5A /* ConstraintPriorityTarget.swift in Sources */,
12BC1A652328C0B500B6EB5A /* ConstraintMakerRelatable.swift in Sources */,
12BC1A632328C0B500B6EB5A /* ConstraintAttributes.swift in Sources */,
12410FE22334DE47002A574B /* UIImage+.swift in Sources */,
12BC1A6A2328C0B500B6EB5A /* ConstraintLayoutSupport.swift in Sources */,
12BC1A642328C0B500B6EB5A /* ConstraintDescription.swift in Sources */,
12BC1A992328C0D900B6EB5A /* EFColorSelectionView.swift in Sources */,
Expand Down Expand Up @@ -894,7 +917,7 @@
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_STYLE = Automatic;
DEBUG_INFORMATION_FORMAT = dwarf;
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = P3X2725LYY;
GCC_C_LANGUAGE_STANDARD = gnu11;
IBSC_MODULE = watchOS_Example_Extension;
INFOPLIST_FILE = "watchOS Example/Info.plist";
Expand All @@ -921,7 +944,7 @@
CODE_SIGN_STYLE = Automatic;
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = P3X2725LYY;
GCC_C_LANGUAGE_STANDARD = gnu11;
IBSC_MODULE = watchOS_Example_Extension;
INFOPLIST_FILE = "watchOS Example/Info.plist";
Expand All @@ -946,7 +969,7 @@
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_STYLE = Automatic;
DEBUG_INFORMATION_FORMAT = dwarf;
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = P3X2725LYY;
GCC_C_LANGUAGE_STANDARD = gnu11;
INFOPLIST_FILE = "watchOS Example Extension/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @executable_path/../../Frameworks";
Expand All @@ -972,7 +995,7 @@
CODE_SIGN_STYLE = Automatic;
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = P3X2725LYY;
GCC_C_LANGUAGE_STANDARD = gnu11;
INFOPLIST_FILE = "watchOS Example Extension/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @executable_path/../../Frameworks";
Expand Down Expand Up @@ -1105,7 +1128,7 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = P3X2725LYY;
INFOPLIST_FILE = Resources/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand All @@ -1122,7 +1145,7 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = P3X2725LYY;
INFOPLIST_FILE = Resources/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand Down
34 changes: 7 additions & 27 deletions Source/EFQRCode+GIF.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import MobileCoreServices
public extension EFQRCode {

private static let framesPerSecond = 24
static var tempResultPath: URL?

private static func batchWatermark(frames: inout [CGImage], generator: EFQRCodeGenerator, start: Int, end: Int) {
for index in start ... end {
Expand Down Expand Up @@ -112,10 +111,8 @@ public extension EFQRCode {
}
}

if let fileProperties = fileProperties, framePropertiesArray.count == frames.count,
let url = frames.saveToGIFFile(framePropertiesArray: framePropertiesArray,
fileProperties: fileProperties, url: pathToSave) {
return try? Data(contentsOf: url)
if let fileProperties = fileProperties, framePropertiesArray.count == frames.count {
return frames.toGifData(framePropertiesArray: framePropertiesArray, fileProperties: fileProperties)
}
}
return nil
Expand All @@ -136,31 +133,14 @@ public extension CGImageSource {

extension Array where Element: CGImage {

public func saveToGIFFile(framePropertiesArray: [CFDictionary], fileProperties: CFDictionary, url: URL? = nil) -> URL? {
var fileURL = url
if nil == fileURL {
let documentsDirectoryURL: URL? = try? FileManager.default.url(
for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true
)
// The URL to write to. If the URL already exists, the data at this location is overwritten.
fileURL = documentsDirectoryURL?.appendingPathComponent("EFQRCode_temp.gif")
}
EFQRCode.tempResultPath = fileURL

guard let url = fileURL as CFURL?,
let destination = CGImageDestinationCreateWithURL(url, kUTTypeGIF, count, nil)
else {
return nil // Can't create path
}

func toGifData(framePropertiesArray: [CFDictionary], fileProperties: CFDictionary) -> Data? {
guard let mutableData = CFDataCreateMutable(nil, 0) else { return nil }
guard let destination = CGImageDestinationCreateWithData(mutableData, kUTTypeGIF, count, nil) else { return nil }
CGImageDestinationSetProperties(destination, fileProperties)
for (index, image) in enumerated() {
CGImageDestinationAddImage(destination, image, framePropertiesArray[index])
}
guard CGImageDestinationFinalize(destination) else {
// Failed to finalize the image destination
return nil
}
return fileURL
guard CGImageDestinationFinalize(destination) else { return nil }
return mutableData as Data
}
}

0 comments on commit 6926207

Please sign in to comment.