Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace objc UIImage Gif extension with swift uiimage gif version #424

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions SKPhotoBrowser.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
7643F4B41FE8CE7A00475F6E /* SKActionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7643F4B31FE8CE7A00475F6E /* SKActionView.swift */; };
7643F4B61FE9F42F00475F6E /* SKPaginationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7643F4B51FE9F42F00475F6E /* SKPaginationView.swift */; };
7643F4B91FEA4FCF00475F6E /* SKToolbar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7643F4B81FEA4FCF00475F6E /* SKToolbar.swift */; };
8909B5341BC791280060A053 /* SKPhotoBrowser.h in Headers */ = {isa = PBXBuildFile; fileRef = 8909B5331BC791280060A053 /* SKPhotoBrowser.h */; settings = {ATTRIBUTES = (Public, ); }; };
8909B5431BC791510060A053 /* SKCaptionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8909B53B1BC791510060A053 /* SKCaptionView.swift */; };
8909B5441BC791510060A053 /* SKDetectingImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8909B53C1BC791510060A053 /* SKDetectingImageView.swift */; };
8909B5451BC791510060A053 /* SKDetectingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8909B53D1BC791510060A053 /* SKDetectingView.swift */; };
Expand All @@ -32,10 +31,9 @@
9521CC63232F677A00446D11 /* UIApplication+UIWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9521CC60232F677900446D11 /* UIApplication+UIWindow.swift */; };
9521CC64232F677A00446D11 /* UIImage+Rotation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9521CC61232F677900446D11 /* UIImage+Rotation.swift */; };
9521CC65232F677A00446D11 /* UIView+Radius.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9521CC62232F677900446D11 /* UIView+Radius.swift */; };
9521CC6B232F693800446D11 /* UIImage+animatedGIF.h in Headers */ = {isa = PBXBuildFile; fileRef = 9521CC69232F693800446D11 /* UIImage+animatedGIF.h */; settings = {ATTRIBUTES = (Public, ); }; };
9521CC6C232F693800446D11 /* UIImage+animatedGIF.m in Sources */ = {isa = PBXBuildFile; fileRef = 9521CC6A232F693800446D11 /* UIImage+animatedGIF.m */; };
A64B89361CB04222000071B9 /* SKPhotoBrowserTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A64B89351CB04222000071B9 /* SKPhotoBrowserTests.swift */; };
A64B89381CB04222000071B9 /* SKPhotoBrowser.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8909B5301BC791280060A053 /* SKPhotoBrowser.framework */; };
C1088AE327F024B800873DD4 /* UIImage+Gif.swift in Sources */ = {isa = PBXBuildFile; fileRef = C1088AE227F024B800873DD4 /* UIImage+Gif.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -56,7 +54,6 @@
7643F4B51FE9F42F00475F6E /* SKPaginationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SKPaginationView.swift; sourceTree = "<group>"; };
7643F4B81FEA4FCF00475F6E /* SKToolbar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SKToolbar.swift; sourceTree = "<group>"; };
8909B5301BC791280060A053 /* SKPhotoBrowser.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SKPhotoBrowser.framework; sourceTree = BUILT_PRODUCTS_DIR; };
8909B5331BC791280060A053 /* SKPhotoBrowser.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SKPhotoBrowser.h; sourceTree = "<group>"; };
8909B5351BC791280060A053 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
8909B53B1BC791510060A053 /* SKCaptionView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SKCaptionView.swift; sourceTree = "<group>"; };
8909B53C1BC791510060A053 /* SKDetectingImageView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SKDetectingImageView.swift; sourceTree = "<group>"; };
Expand All @@ -76,11 +73,10 @@
9521CC60232F677900446D11 /* UIApplication+UIWindow.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIApplication+UIWindow.swift"; sourceTree = "<group>"; };
9521CC61232F677900446D11 /* UIImage+Rotation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIImage+Rotation.swift"; sourceTree = "<group>"; };
9521CC62232F677900446D11 /* UIView+Radius.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIView+Radius.swift"; sourceTree = "<group>"; };
9521CC69232F693800446D11 /* UIImage+animatedGIF.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImage+animatedGIF.h"; sourceTree = "<group>"; };
9521CC6A232F693800446D11 /* UIImage+animatedGIF.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+animatedGIF.m"; sourceTree = "<group>"; };
A64B89331CB04222000071B9 /* SKPhotoBrowserTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SKPhotoBrowserTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
A64B89351CB04222000071B9 /* SKPhotoBrowserTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SKPhotoBrowserTests.swift; sourceTree = "<group>"; };
A64B89371CB04222000071B9 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
C1088AE227F024B800873DD4 /* UIImage+Gif.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImage+Gif.swift"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -139,7 +135,6 @@
8909B53F1BC791510060A053 /* SKPhoto.swift */,
8909B5411BC791510060A053 /* SKPhotoBrowser.swift */,
89C24A811D657AD1005F09A9 /* SKPhotoBrowserOptions.swift */,
8909B5331BC791280060A053 /* SKPhotoBrowser.h */,
8917B1AF1D5A13DE000CE1C4 /* SKPhotoBrowserDelegate.swift */,
7643F4B81FEA4FCF00475F6E /* SKToolbar.swift */,
8909B5421BC791510060A053 /* SKZoomingScrollView.swift */,
Expand All @@ -153,11 +148,10 @@
9521CC5F232F677900446D11 /* extensions */ = {
isa = PBXGroup;
children = (
9521CC69232F693800446D11 /* UIImage+animatedGIF.h */,
9521CC6A232F693800446D11 /* UIImage+animatedGIF.m */,
9521CC60232F677900446D11 /* UIApplication+UIWindow.swift */,
9521CC61232F677900446D11 /* UIImage+Rotation.swift */,
9521CC62232F677900446D11 /* UIView+Radius.swift */,
C1088AE227F024B800873DD4 /* UIImage+Gif.swift */,
);
path = extensions;
sourceTree = "<group>";
Expand All @@ -179,8 +173,6 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
9521CC6B232F693800446D11 /* UIImage+animatedGIF.h in Headers */,
8909B5341BC791280060A053 /* SKPhotoBrowser.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -307,6 +299,7 @@
7643F4B91FEA4FCF00475F6E /* SKToolbar.swift in Sources */,
9521CC65232F677A00446D11 /* UIView+Radius.swift in Sources */,
89C24A841D657AFE005F09A9 /* SKPagingScrollView.swift in Sources */,
C1088AE327F024B800873DD4 /* UIImage+Gif.swift in Sources */,
8909B5441BC791510060A053 /* SKDetectingImageView.swift in Sources */,
8909B54A1BC791510060A053 /* SKZoomingScrollView.swift in Sources */,
9521CC64232F677A00446D11 /* UIImage+Rotation.swift in Sources */,
Expand All @@ -315,7 +308,6 @@
7643F4B61FE9F42F00475F6E /* SKPaginationView.swift in Sources */,
7643F4B41FE8CE7A00475F6E /* SKActionView.swift in Sources */,
8917B1B01D5A13DE000CE1C4 /* SKPhotoBrowserDelegate.swift in Sources */,
9521CC6C232F693800446D11 /* UIImage+animatedGIF.m in Sources */,
89D0BA491D59966B002A811B /* SKMesurement.swift in Sources */,
8909B5471BC791510060A053 /* SKPhoto.swift in Sources */,
8909B5461BC791510060A053 /* SKIndicatorView.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion SKPhotoBrowser/SKPhoto.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ open class SKPhoto: NSObject, SKPhotoProtocol {
return
}

if let data = data, let response = response, let image = UIImage.animatedImage(withAnimatedGIFData: data) {
if let data = data, let response = response, let image = UIImage.animatedImage(data: data) {
if self.shouldCachePhotoURLImage {
if SKCache.sharedCache.imageCache is SKRequestResponseCacheable {
SKCache.sharedCache.setImageData(data, response: response, request: task?.originalRequest)
Expand Down
20 changes: 0 additions & 20 deletions SKPhotoBrowser/SKPhotoBrowser.h

This file was deleted.

62 changes: 62 additions & 0 deletions SKPhotoBrowser/extensions/UIImage+Gif.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// UIImage+Gif.swift
// SKPhotoBrowser
//
// Created by rosua le on 2022/3/27.
// Copyright © 2022 suzuki_keishi. All rights reserved.
// copy from https://github.com/toddheasley/gif
//

import UIKit

extension UIImage {
public enum GIFBehavior {
case unclamped, clamped(TimeInterval), webkit

fileprivate func duration(_ properties: Any?) -> TimeInterval? {

// Look for specified image duration; always prefer unclamped delay time
guard let properties: [String: Any] = (properties as? [String: Any])?["{GIF}"] as? [String: Any],
let duration: TimeInterval = properties["UnclampedDelayTime"] as? TimeInterval ?? properties["DelayTime"] as? TimeInterval else {
return nil
}

// Apply appropriate clamping behavior
switch self {
case .unclamped:
return max(duration, 0.0) // Respect natural delay time
case .clamped(let clamp):
return max(duration, max(clamp, 0.0)) // Clamp to custom delay time
case .webkit:
return max(duration, 0.1) // Mimic WebKit behavior
}
}
}

public static func animatedImage(data: Data, behavior: GIFBehavior = .webkit) -> UIImage? {
guard let source: CGImageSource = CGImageSourceCreateWithData(data as CFData, nil), CGImageSourceGetCount(source) > 1 else {
return Self(data: data) // Delegate ineligible image data to the designated data constructor
}

// Collect key frames and durations
let frames: [(image: CGImage, duration: TimeInterval)] = (0 ..< CGImageSourceGetCount(source)).compactMap { index in
guard let image: CGImage = CGImageSourceCreateImageAtIndex(source, index, nil),
let duration: TimeInterval = behavior.duration(CGImageSourceCopyPropertiesAtIndex(source, index, nil)) else {
return nil // Drop bad frames
}
return (image, duration)
}

// Convert key frames to animated image
var images: [UIImage] = []
var duration: TimeInterval = 0.0
for frame in frames {
let image = UIImage(cgImage: frame.image)
for _ in 0 ..< Int(frame.duration * 100.0) {
images.append(image) // Add fill frames
}
duration += frame.duration
}
return animatedImage(with: images, duration: round(duration * 10.0) / 10.0)
}
}
32 changes: 0 additions & 32 deletions SKPhotoBrowser/extensions/UIImage+animatedGIF.h

This file was deleted.

119 changes: 0 additions & 119 deletions SKPhotoBrowser/extensions/UIImage+animatedGIF.m

This file was deleted.