├── JPGiftManager.xcodeproj
├── project.pbxproj
├── project.xcworkspace
│ ├── contents.xcworkspacedata
│ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ └── xcuserdata
│ │ └── peng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
└── xcuserdata
│ ├── dong.xcuserdatad
│ └── xcdebugger
│ │ └── Breakpoints_v2.xcbkptlist
│ └── peng.xcuserdatad
│ └── xcschemes
│ └── xcschememanagement.plist
├── JPGiftManager
├── AFNetworking
│ ├── AFNetworking
│ │ ├── AFHTTPSessionManager.h
│ │ ├── AFHTTPSessionManager.m
│ │ ├── AFNetworkReachabilityManager.h
│ │ ├── AFNetworkReachabilityManager.m
│ │ ├── AFNetworking.h
│ │ ├── AFSecurityPolicy.h
│ │ ├── AFSecurityPolicy.m
│ │ ├── AFURLRequestSerialization.h
│ │ ├── AFURLRequestSerialization.m
│ │ ├── AFURLResponseSerialization.h
│ │ ├── AFURLResponseSerialization.m
│ │ ├── AFURLSessionManager.h
│ │ └── AFURLSessionManager.m
│ ├── LICENSE
│ ├── README.md
│ └── UIKit+AFNetworking
│ │ ├── AFAutoPurgingImageCache.h
│ │ ├── AFAutoPurgingImageCache.m
│ │ ├── AFImageDownloader.h
│ │ ├── AFImageDownloader.m
│ │ ├── AFNetworkActivityIndicatorManager.h
│ │ ├── AFNetworkActivityIndicatorManager.m
│ │ ├── UIActivityIndicatorView+AFNetworking.h
│ │ ├── UIActivityIndicatorView+AFNetworking.m
│ │ ├── UIButton+AFNetworking.h
│ │ ├── UIButton+AFNetworking.m
│ │ ├── UIImage+AFNetworking.h
│ │ ├── UIImageView+AFNetworking.h
│ │ ├── UIImageView+AFNetworking.m
│ │ ├── UIKit+AFNetworking.h
│ │ ├── UIProgressView+AFNetworking.h
│ │ ├── UIProgressView+AFNetworking.m
│ │ ├── UIRefreshControl+AFNetworking.h
│ │ ├── UIRefreshControl+AFNetworking.m
│ │ ├── UIWebView+AFNetworking.h
│ │ └── UIWebView+AFNetworking.m
├── AppDelegate.h
├── AppDelegate.m
├── Assets.xcassets
│ ├── AppIcon.appiconset
│ │ └── Contents.json
│ ├── Cece_live_star_small.imageset
│ │ ├── Cece_live_star_small@2x.png
│ │ ├── Cece_live_star_small@3x.png
│ │ └── Contents.json
│ ├── Contents.json
│ └── Live_Red_ccb.imageset
│ │ ├── Contents.json
│ │ ├── Live_Red_ccb@2x.png
│ │ └── Live_Red_ccb@3x.png
├── Base.lproj
│ ├── LaunchScreen.storyboard
│ └── Main.storyboard
├── Info.plist
├── JPGiftManager
│ ├── JPGiftCellModel.h
│ ├── JPGiftCellModel.m
│ ├── JPGiftCollectionViewCell.h
│ ├── JPGiftCollectionViewCell.m
│ ├── JPGiftCountLabel.h
│ ├── JPGiftCountLabel.m
│ ├── JPGiftModel.h
│ ├── JPGiftModel.m
│ ├── JPGiftOperation.h
│ ├── JPGiftOperation.m
│ ├── JPGiftShowManager.h
│ ├── JPGiftShowManager.m
│ ├── JPGiftShowView.h
│ ├── JPGiftShowView.m
│ ├── JPGiftView.h
│ ├── JPGiftView.m
│ ├── JPHorizontalLayout.h
│ └── JPHorizontalLayout.m
├── SDWebImage
│ ├── MKAnnotationView+WebCache.h
│ ├── MKAnnotationView+WebCache.m
│ ├── NSData+ImageContentType.h
│ ├── NSData+ImageContentType.m
│ ├── SDImageCache.h
│ ├── SDImageCache.m
│ ├── SDWebImageCompat.h
│ ├── SDWebImageCompat.m
│ ├── SDWebImageDecoder.h
│ ├── SDWebImageDecoder.m
│ ├── SDWebImageDownloader.h
│ ├── SDWebImageDownloader.m
│ ├── SDWebImageDownloaderOperation.h
│ ├── SDWebImageDownloaderOperation.m
│ ├── SDWebImageManager.h
│ ├── SDWebImageManager.m
│ ├── SDWebImageOperation.h
│ ├── SDWebImagePrefetcher.h
│ ├── SDWebImagePrefetcher.m
│ ├── UIButton+WebCache.h
│ ├── UIButton+WebCache.m
│ ├── UIImage+GIF.h
│ ├── UIImage+GIF.m
│ ├── UIImage+MultiFormat.h
│ ├── UIImage+MultiFormat.m
│ ├── UIImage+WebP.h
│ ├── UIImage+WebP.m
│ ├── UIImageView+HighlightedWebCache.h
│ ├── UIImageView+HighlightedWebCache.m
│ ├── UIImageView+WebCache.h
│ ├── UIImageView+WebCache.m
│ ├── UIView+WebCacheOperation.h
│ └── UIView+WebCacheOperation.m
├── ViewController.h
├── ViewController.m
├── YYModel
│ ├── NSObject+YYModel.h
│ ├── NSObject+YYModel.m
│ ├── YYClassInfo.h
│ ├── YYClassInfo.m
│ └── YYModel.h
├── data.json
├── gif
│ └── giftimage.gif
└── main.m
└── README.md
/JPGiftManager.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/JPGiftManager.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/JPGiftManager.xcodeproj/project.xcworkspace/xcuserdata/peng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/baiyidjp/JPGiftManager/768830504fd7e51bba8104b15509236dfd2523f3/JPGiftManager.xcodeproj/project.xcworkspace/xcuserdata/peng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/JPGiftManager.xcodeproj/xcuserdata/peng.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | JPGiftManager.xcscheme_^#shared#^_
8 |
9 | orderHint
10 | 0
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/JPGiftManager/AFNetworking/AFNetworking/AFNetworking.h:
--------------------------------------------------------------------------------
1 | // AFNetworking.h
2 | //
3 | // Copyright (c) 2013 AFNetworking (http://afnetworking.com/)
4 | //
5 | // Permission is hereby granted, free of charge, to any person obtaining a copy
6 | // of this software and associated documentation files (the "Software"), to deal
7 | // in the Software without restriction, including without limitation the rights
8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | // copies of the Software, and to permit persons to whom the Software is
10 | // furnished to do so, subject to the following conditions:
11 | //
12 | // The above copyright notice and this permission notice shall be included in
13 | // all copies or substantial portions of the Software.
14 | //
15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 | // THE SOFTWARE.
22 |
23 | #import
24 | #import
25 | #import
26 |
27 | #ifndef _AFNETWORKING_
28 | #define _AFNETWORKING_
29 |
30 | #import "AFURLRequestSerialization.h"
31 | #import "AFURLResponseSerialization.h"
32 | #import "AFSecurityPolicy.h"
33 |
34 | #if !TARGET_OS_WATCH
35 | #import "AFNetworkReachabilityManager.h"
36 | #endif
37 |
38 | #import "AFURLSessionManager.h"
39 | #import "AFHTTPSessionManager.h"
40 |
41 | #endif /* _AFNETWORKING_ */
42 |
--------------------------------------------------------------------------------
/JPGiftManager/AFNetworking/AFNetworking/AFSecurityPolicy.h:
--------------------------------------------------------------------------------
1 | // AFSecurityPolicy.h
2 | // Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/)
3 | //
4 | // Permission is hereby granted, free of charge, to any person obtaining a copy
5 | // of this software and associated documentation files (the "Software"), to deal
6 | // in the Software without restriction, including without limitation the rights
7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | // copies of the Software, and to permit persons to whom the Software is
9 | // furnished to do so, subject to the following conditions:
10 | //
11 | // The above copyright notice and this permission notice shall be included in
12 | // all copies or substantial portions of the Software.
13 | //
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 | // THE SOFTWARE.
21 |
22 | #import
23 | #import
24 |
25 | typedef NS_ENUM(NSUInteger, AFSSLPinningMode) {
26 | AFSSLPinningModeNone,
27 | AFSSLPinningModePublicKey,
28 | AFSSLPinningModeCertificate,
29 | };
30 |
31 | /**
32 | `AFSecurityPolicy` evaluates server trust against pinned X.509 certificates and public keys over secure connections.
33 |
34 | Adding pinned SSL certificates to your app helps prevent man-in-the-middle attacks and other vulnerabilities. Applications dealing with sensitive customer data or financial information are strongly encouraged to route all communication over an HTTPS connection with SSL pinning configured and enabled.
35 | */
36 |
37 | NS_ASSUME_NONNULL_BEGIN
38 |
39 | @interface AFSecurityPolicy : NSObject
40 |
41 | /**
42 | The criteria by which server trust should be evaluated against the pinned SSL certificates. Defaults to `AFSSLPinningModeNone`.
43 | */
44 | @property (readonly, nonatomic, assign) AFSSLPinningMode SSLPinningMode;
45 |
46 | /**
47 | The certificates used to evaluate server trust according to the SSL pinning mode.
48 |
49 | By default, this property is set to any (`.cer`) certificates included in the target compiling AFNetworking. Note that if you are using AFNetworking as embedded framework, no certificates will be pinned by default. Use `certificatesInBundle` to load certificates from your target, and then create a new policy by calling `policyWithPinningMode:withPinnedCertificates`.
50 |
51 | Note that if pinning is enabled, `evaluateServerTrust:forDomain:` will return true if any pinned certificate matches.
52 | */
53 | @property (nonatomic, strong, nullable) NSSet *pinnedCertificates;
54 |
55 | /**
56 | Whether or not to trust servers with an invalid or expired SSL certificates. Defaults to `NO`.
57 | */
58 | @property (nonatomic, assign) BOOL allowInvalidCertificates;
59 |
60 | /**
61 | Whether or not to validate the domain name in the certificate's CN field. Defaults to `YES`.
62 | */
63 | @property (nonatomic, assign) BOOL validatesDomainName;
64 |
65 | ///-----------------------------------------
66 | /// @name Getting Certificates from the Bundle
67 | ///-----------------------------------------
68 |
69 | /**
70 | Returns any certificates included in the bundle. If you are using AFNetworking as an embedded framework, you must use this method to find the certificates you have included in your app bundle, and use them when creating your security policy by calling `policyWithPinningMode:withPinnedCertificates`.
71 |
72 | @return The certificates included in the given bundle.
73 | */
74 | + (NSSet *)certificatesInBundle:(NSBundle *)bundle;
75 |
76 | ///-----------------------------------------
77 | /// @name Getting Specific Security Policies
78 | ///-----------------------------------------
79 |
80 | /**
81 | Returns the shared default security policy, which does not allow invalid certificates, validates domain name, and does not validate against pinned certificates or public keys.
82 |
83 | @return The default security policy.
84 | */
85 | + (instancetype)defaultPolicy;
86 |
87 | ///---------------------
88 | /// @name Initialization
89 | ///---------------------
90 |
91 | /**
92 | Creates and returns a security policy with the specified pinning mode.
93 |
94 | @param pinningMode The SSL pinning mode.
95 |
96 | @return A new security policy.
97 | */
98 | + (instancetype)policyWithPinningMode:(AFSSLPinningMode)pinningMode;
99 |
100 | /**
101 | Creates and returns a security policy with the specified pinning mode.
102 |
103 | @param pinningMode The SSL pinning mode.
104 | @param pinnedCertificates The certificates to pin against.
105 |
106 | @return A new security policy.
107 | */
108 | + (instancetype)policyWithPinningMode:(AFSSLPinningMode)pinningMode withPinnedCertificates:(NSSet *)pinnedCertificates;
109 |
110 | ///------------------------------
111 | /// @name Evaluating Server Trust
112 | ///------------------------------
113 |
114 | /**
115 | Whether or not the specified server trust should be accepted, based on the security policy.
116 |
117 | This method should be used when responding to an authentication challenge from a server.
118 |
119 | @param serverTrust The X.509 certificate trust of the server.
120 | @param domain The domain of serverTrust. If `nil`, the domain will not be validated.
121 |
122 | @return Whether or not to trust the server.
123 | */
124 | - (BOOL)evaluateServerTrust:(SecTrustRef)serverTrust
125 | forDomain:(nullable NSString *)domain;
126 |
127 | @end
128 |
129 | NS_ASSUME_NONNULL_END
130 |
131 | ///----------------
132 | /// @name Constants
133 | ///----------------
134 |
135 | /**
136 | ## SSL Pinning Modes
137 |
138 | The following constants are provided by `AFSSLPinningMode` as possible SSL pinning modes.
139 |
140 | enum {
141 | AFSSLPinningModeNone,
142 | AFSSLPinningModePublicKey,
143 | AFSSLPinningModeCertificate,
144 | }
145 |
146 | `AFSSLPinningModeNone`
147 | Do not used pinned certificates to validate servers.
148 |
149 | `AFSSLPinningModePublicKey`
150 | Validate host certificates against public keys of pinned certificates.
151 |
152 | `AFSSLPinningModeCertificate`
153 | Validate host certificates against pinned certificates.
154 | */
155 |
--------------------------------------------------------------------------------
/JPGiftManager/AFNetworking/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/)
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy
4 | of this software and associated documentation files (the "Software"), to deal
5 | in the Software without restriction, including without limitation the rights
6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | copies of the Software, and to permit persons to whom the Software is
8 | furnished to do so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in
11 | all copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 | THE SOFTWARE.
20 |
--------------------------------------------------------------------------------
/JPGiftManager/AFNetworking/UIKit+AFNetworking/AFAutoPurgingImageCache.h:
--------------------------------------------------------------------------------
1 | // AFAutoPurgingImageCache.h
2 | // Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/)
3 | //
4 | // Permission is hereby granted, free of charge, to any person obtaining a copy
5 | // of this software and associated documentation files (the "Software"), to deal
6 | // in the Software without restriction, including without limitation the rights
7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | // copies of the Software, and to permit persons to whom the Software is
9 | // furnished to do so, subject to the following conditions:
10 | //
11 | // The above copyright notice and this permission notice shall be included in
12 | // all copies or substantial portions of the Software.
13 | //
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 | // THE SOFTWARE.
21 |
22 | #import
23 | #import
24 |
25 | #if TARGET_OS_IOS || TARGET_OS_TV
26 | #import
27 |
28 | NS_ASSUME_NONNULL_BEGIN
29 |
30 | /**
31 | The `AFImageCache` protocol defines a set of APIs for adding, removing and fetching images from a cache synchronously.
32 | */
33 | @protocol AFImageCache
34 |
35 | /**
36 | Adds the image to the cache with the given identifier.
37 |
38 | @param image The image to cache.
39 | @param identifier The unique identifier for the image in the cache.
40 | */
41 | - (void)addImage:(UIImage *)image withIdentifier:(NSString *)identifier;
42 |
43 | /**
44 | Removes the image from the cache matching the given identifier.
45 |
46 | @param identifier The unique identifier for the image in the cache.
47 |
48 | @return A BOOL indicating whether or not the image was removed from the cache.
49 | */
50 | - (BOOL)removeImageWithIdentifier:(NSString *)identifier;
51 |
52 | /**
53 | Removes all images from the cache.
54 |
55 | @return A BOOL indicating whether or not all images were removed from the cache.
56 | */
57 | - (BOOL)removeAllImages;
58 |
59 | /**
60 | Returns the image in the cache associated with the given identifier.
61 |
62 | @param identifier The unique identifier for the image in the cache.
63 |
64 | @return An image for the matching identifier, or nil.
65 | */
66 | - (nullable UIImage *)imageWithIdentifier:(NSString *)identifier;
67 | @end
68 |
69 |
70 | /**
71 | The `ImageRequestCache` protocol extends the `ImageCache` protocol by adding methods for adding, removing and fetching images from a cache given an `NSURLRequest` and additional identifier.
72 | */
73 | @protocol AFImageRequestCache
74 |
75 | /**
76 | Adds the image to the cache using an identifier created from the request and additional identifier.
77 |
78 | @param image The image to cache.
79 | @param request The unique URL request identifing the image asset.
80 | @param identifier The additional identifier to apply to the URL request to identify the image.
81 | */
82 | - (void)addImage:(UIImage *)image forRequest:(NSURLRequest *)request withAdditionalIdentifier:(nullable NSString *)identifier;
83 |
84 | /**
85 | Removes the image from the cache using an identifier created from the request and additional identifier.
86 |
87 | @param request The unique URL request identifing the image asset.
88 | @param identifier The additional identifier to apply to the URL request to identify the image.
89 |
90 | @return A BOOL indicating whether or not all images were removed from the cache.
91 | */
92 | - (BOOL)removeImageforRequest:(NSURLRequest *)request withAdditionalIdentifier:(nullable NSString *)identifier;
93 |
94 | /**
95 | Returns the image from the cache associated with an identifier created from the request and additional identifier.
96 |
97 | @param request The unique URL request identifing the image asset.
98 | @param identifier The additional identifier to apply to the URL request to identify the image.
99 |
100 | @return An image for the matching request and identifier, or nil.
101 | */
102 | - (nullable UIImage *)imageforRequest:(NSURLRequest *)request withAdditionalIdentifier:(nullable NSString *)identifier;
103 |
104 | @end
105 |
106 | /**
107 | The `AutoPurgingImageCache` in an in-memory image cache used to store images up to a given memory capacity. When the memory capacity is reached, the image cache is sorted by last access date, then the oldest image is continuously purged until the preferred memory usage after purge is met. Each time an image is accessed through the cache, the internal access date of the image is updated.
108 | */
109 | @interface AFAutoPurgingImageCache : NSObject
110 |
111 | /**
112 | The total memory capacity of the cache in bytes.
113 | */
114 | @property (nonatomic, assign) UInt64 memoryCapacity;
115 |
116 | /**
117 | The preferred memory usage after purge in bytes. During a purge, images will be purged until the memory capacity drops below this limit.
118 | */
119 | @property (nonatomic, assign) UInt64 preferredMemoryUsageAfterPurge;
120 |
121 | /**
122 | The current total memory usage in bytes of all images stored within the cache.
123 | */
124 | @property (nonatomic, assign, readonly) UInt64 memoryUsage;
125 |
126 | /**
127 | Initialies the `AutoPurgingImageCache` instance with default values for memory capacity and preferred memory usage after purge limit. `memoryCapcity` defaults to `100 MB`. `preferredMemoryUsageAfterPurge` defaults to `60 MB`.
128 |
129 | @return The new `AutoPurgingImageCache` instance.
130 | */
131 | - (instancetype)init;
132 |
133 | /**
134 | Initialies the `AutoPurgingImageCache` instance with the given memory capacity and preferred memory usage
135 | after purge limit.
136 |
137 | @param memoryCapacity The total memory capacity of the cache in bytes.
138 | @param preferredMemoryUsageAfterPurge The preferred memory usage after purge in bytes.
139 |
140 | @return The new `AutoPurgingImageCache` instance.
141 | */
142 | - (instancetype)initWithMemoryCapacity:(UInt64)memoryCapacity preferredMemoryCapacity:(UInt64)preferredMemoryCapacity;
143 |
144 | @end
145 |
146 | NS_ASSUME_NONNULL_END
147 |
148 | #endif
149 |
150 |
--------------------------------------------------------------------------------
/JPGiftManager/AFNetworking/UIKit+AFNetworking/AFAutoPurgingImageCache.m:
--------------------------------------------------------------------------------
1 | // AFAutoPurgingImageCache.m
2 | // Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/)
3 | //
4 | // Permission is hereby granted, free of charge, to any person obtaining a copy
5 | // of this software and associated documentation files (the "Software"), to deal
6 | // in the Software without restriction, including without limitation the rights
7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | // copies of the Software, and to permit persons to whom the Software is
9 | // furnished to do so, subject to the following conditions:
10 | //
11 | // The above copyright notice and this permission notice shall be included in
12 | // all copies or substantial portions of the Software.
13 | //
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 | // THE SOFTWARE.
21 |
22 | #import
23 |
24 | #if TARGET_OS_IOS || TARGET_OS_TV
25 |
26 | #import "AFAutoPurgingImageCache.h"
27 |
28 | @interface AFCachedImage : NSObject
29 |
30 | @property (nonatomic, strong) UIImage *image;
31 | @property (nonatomic, strong) NSString *identifier;
32 | @property (nonatomic, assign) UInt64 totalBytes;
33 | @property (nonatomic, strong) NSDate *lastAccessDate;
34 | @property (nonatomic, assign) UInt64 currentMemoryUsage;
35 |
36 | @end
37 |
38 | @implementation AFCachedImage
39 |
40 | -(instancetype)initWithImage:(UIImage *)image identifier:(NSString *)identifier {
41 | if (self = [self init]) {
42 | self.image = image;
43 | self.identifier = identifier;
44 |
45 | CGSize imageSize = CGSizeMake(image.size.width * image.scale, image.size.height * image.scale);
46 | CGFloat bytesPerPixel = 4.0;
47 | CGFloat bytesPerRow = imageSize.width * bytesPerPixel;
48 | self.totalBytes = (UInt64)bytesPerPixel * (UInt64)bytesPerRow;
49 | self.lastAccessDate = [NSDate date];
50 | }
51 | return self;
52 | }
53 |
54 | - (UIImage*)accessImage {
55 | self.lastAccessDate = [NSDate date];
56 | return self.image;
57 | }
58 |
59 | - (NSString *)description {
60 | NSString *descriptionString = [NSString stringWithFormat:@"Idenfitier: %@ lastAccessDate: %@ ", self.identifier, self.lastAccessDate];
61 | return descriptionString;
62 |
63 | }
64 |
65 | @end
66 |
67 | @interface AFAutoPurgingImageCache ()
68 | @property (nonatomic, strong) NSMutableDictionary *cachedImages;
69 | @property (nonatomic, assign) UInt64 currentMemoryUsage;
70 | @property (nonatomic, strong) dispatch_queue_t synchronizationQueue;
71 | @end
72 |
73 | @implementation AFAutoPurgingImageCache
74 |
75 | - (instancetype)init {
76 | return [self initWithMemoryCapacity:100 * 1024 * 1024 preferredMemoryCapacity:60 * 1024 * 1024];
77 | }
78 |
79 | - (instancetype)initWithMemoryCapacity:(UInt64)memoryCapacity preferredMemoryCapacity:(UInt64)preferredMemoryCapacity {
80 | if (self = [super init]) {
81 | self.memoryCapacity = memoryCapacity;
82 | self.preferredMemoryUsageAfterPurge = preferredMemoryCapacity;
83 | self.cachedImages = [[NSMutableDictionary alloc] init];
84 |
85 | NSString *queueName = [NSString stringWithFormat:@"com.alamofire.autopurgingimagecache-%@", [[NSUUID UUID] UUIDString]];
86 | self.synchronizationQueue = dispatch_queue_create([queueName cStringUsingEncoding:NSASCIIStringEncoding], DISPATCH_QUEUE_CONCURRENT);
87 |
88 | [[NSNotificationCenter defaultCenter]
89 | addObserver:self
90 | selector:@selector(removeAllImages)
91 | name:UIApplicationDidReceiveMemoryWarningNotification
92 | object:nil];
93 |
94 | }
95 | return self;
96 | }
97 |
98 | - (void)dealloc {
99 | [[NSNotificationCenter defaultCenter] removeObserver:self];
100 | }
101 |
102 | - (UInt64)memoryUsage {
103 | __block UInt64 result = 0;
104 | dispatch_sync(self.synchronizationQueue, ^{
105 | result = self.currentMemoryUsage;
106 | });
107 | return result;
108 | }
109 |
110 | - (void)addImage:(UIImage *)image withIdentifier:(NSString *)identifier {
111 | dispatch_barrier_async(self.synchronizationQueue, ^{
112 | AFCachedImage *cacheImage = [[AFCachedImage alloc] initWithImage:image identifier:identifier];
113 |
114 | AFCachedImage *previousCachedImage = self.cachedImages[identifier];
115 | if (previousCachedImage != nil) {
116 | self.currentMemoryUsage -= previousCachedImage.totalBytes;
117 | }
118 |
119 | self.cachedImages[identifier] = cacheImage;
120 | self.currentMemoryUsage += cacheImage.totalBytes;
121 | });
122 |
123 | dispatch_barrier_async(self.synchronizationQueue, ^{
124 | if (self.currentMemoryUsage > self.memoryCapacity) {
125 | UInt64 bytesToPurge = self.currentMemoryUsage - self.preferredMemoryUsageAfterPurge;
126 | NSMutableArray *sortedImages = [NSMutableArray arrayWithArray:self.cachedImages.allValues];
127 | NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastAccessDate"
128 | ascending:YES];
129 | [sortedImages sortUsingDescriptors:@[sortDescriptor]];
130 |
131 | UInt64 bytesPurged = 0;
132 |
133 | for (AFCachedImage *cachedImage in sortedImages) {
134 | [self.cachedImages removeObjectForKey:cachedImage.identifier];
135 | bytesPurged += cachedImage.totalBytes;
136 | if (bytesPurged >= bytesToPurge) {
137 | break ;
138 | }
139 | }
140 | self.currentMemoryUsage -= bytesPurged;
141 | }
142 | });
143 | }
144 |
145 | - (BOOL)removeImageWithIdentifier:(NSString *)identifier {
146 | __block BOOL removed = NO;
147 | dispatch_barrier_sync(self.synchronizationQueue, ^{
148 | AFCachedImage *cachedImage = self.cachedImages[identifier];
149 | if (cachedImage != nil) {
150 | [self.cachedImages removeObjectForKey:identifier];
151 | self.currentMemoryUsage -= cachedImage.totalBytes;
152 | removed = YES;
153 | }
154 | });
155 | return removed;
156 | }
157 |
158 | - (BOOL)removeAllImages {
159 | __block BOOL removed = NO;
160 | dispatch_barrier_sync(self.synchronizationQueue, ^{
161 | if (self.cachedImages.count > 0) {
162 | [self.cachedImages removeAllObjects];
163 | self.currentMemoryUsage = 0;
164 | removed = YES;
165 | }
166 | });
167 | return removed;
168 | }
169 |
170 | - (nullable UIImage *)imageWithIdentifier:(NSString *)identifier {
171 | __block UIImage *image = nil;
172 | dispatch_sync(self.synchronizationQueue, ^{
173 | AFCachedImage *cachedImage = self.cachedImages[identifier];
174 | image = [cachedImage accessImage];
175 | });
176 | return image;
177 | }
178 |
179 | - (void)addImage:(UIImage *)image forRequest:(NSURLRequest *)request withAdditionalIdentifier:(NSString *)identifier {
180 | [self addImage:image withIdentifier:[self imageCacheKeyFromURLRequest:request withAdditionalIdentifier:identifier]];
181 | }
182 |
183 | - (BOOL)removeImageforRequest:(NSURLRequest *)request withAdditionalIdentifier:(NSString *)identifier {
184 | return [self removeImageWithIdentifier:[self imageCacheKeyFromURLRequest:request withAdditionalIdentifier:identifier]];
185 | }
186 |
187 | - (nullable UIImage *)imageforRequest:(NSURLRequest *)request withAdditionalIdentifier:(NSString *)identifier {
188 | return [self imageWithIdentifier:[self imageCacheKeyFromURLRequest:request withAdditionalIdentifier:identifier]];
189 | }
190 |
191 | - (NSString *)imageCacheKeyFromURLRequest:(NSURLRequest *)request withAdditionalIdentifier:(NSString *)additionalIdentifier {
192 | NSString *key = request.URL.absoluteString;
193 | if (additionalIdentifier != nil) {
194 | key = [key stringByAppendingString:additionalIdentifier];
195 | }
196 | return key;
197 | }
198 |
199 | @end
200 |
201 | #endif
202 |
--------------------------------------------------------------------------------
/JPGiftManager/AFNetworking/UIKit+AFNetworking/AFImageDownloader.h:
--------------------------------------------------------------------------------
1 | // AFImageDownloader.h
2 | // Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/)
3 | //
4 | // Permission is hereby granted, free of charge, to any person obtaining a copy
5 | // of this software and associated documentation files (the "Software"), to deal
6 | // in the Software without restriction, including without limitation the rights
7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | // copies of the Software, and to permit persons to whom the Software is
9 | // furnished to do so, subject to the following conditions:
10 | //
11 | // The above copyright notice and this permission notice shall be included in
12 | // all copies or substantial portions of the Software.
13 | //
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 | // THE SOFTWARE.
21 |
22 | #import
23 |
24 | #if TARGET_OS_IOS || TARGET_OS_TV
25 |
26 | #import
27 | #import "AFAutoPurgingImageCache.h"
28 | #import "AFHTTPSessionManager.h"
29 |
30 | NS_ASSUME_NONNULL_BEGIN
31 |
32 | typedef NS_ENUM(NSInteger, AFImageDownloadPrioritization) {
33 | AFImageDownloadPrioritizationFIFO,
34 | AFImageDownloadPrioritizationLIFO
35 | };
36 |
37 | /**
38 | The `AFImageDownloadReceipt` is an object vended by the `AFImageDownloader` when starting a data task. It can be used to cancel active tasks running on the `AFImageDownloader` session. As a general rule, image data tasks should be cancelled using the `AFImageDownloadReceipt` instead of calling `cancel` directly on the `task` itself. The `AFImageDownloader` is optimized to handle duplicate task scenarios as well as pending versus active downloads.
39 | */
40 | @interface AFImageDownloadReceipt : NSObject
41 |
42 | /**
43 | The data task created by the `AFImageDownloader`.
44 | */
45 | @property (nonatomic, strong) NSURLSessionDataTask *task;
46 |
47 | /**
48 | The unique identifier for the success and failure blocks when duplicate requests are made.
49 | */
50 | @property (nonatomic, strong) NSUUID *receiptID;
51 | @end
52 |
53 | /** The `AFImageDownloader` class is responsible for downloading images in parallel on a prioritized queue. Incoming downloads are added to the front or back of the queue depending on the download prioritization. Each downloaded image is cached in the underlying `NSURLCache` as well as the in-memory image cache. By default, any download request with a cached image equivalent in the image cache will automatically be served the cached image representation.
54 | */
55 | @interface AFImageDownloader : NSObject
56 |
57 | /**
58 | The image cache used to store all downloaded images in. `AFAutoPurgingImageCache` by default.
59 | */
60 | @property (nonatomic, strong, nullable) id imageCache;
61 |
62 | /**
63 | The `AFHTTPSessionManager` used to download images. By default, this is configured with an `AFImageResponseSerializer`, and a shared `NSURLCache` for all image downloads.
64 | */
65 | @property (nonatomic, strong) AFHTTPSessionManager *sessionManager;
66 |
67 | /**
68 | Defines the order prioritization of incoming download requests being inserted into the queue. `AFImageDownloadPrioritizationFIFO` by default.
69 | */
70 | @property (nonatomic, assign) AFImageDownloadPrioritization downloadPrioritizaton;
71 |
72 | /**
73 | The shared default instance of `AFImageDownloader` initialized with default values.
74 | */
75 | + (instancetype)defaultInstance;
76 |
77 | /**
78 | Creates a default `NSURLCache` with common usage parameter values.
79 |
80 | @returns The default `NSURLCache` instance.
81 | */
82 | + (NSURLCache *)defaultURLCache;
83 |
84 | /**
85 | Default initializer
86 |
87 | @return An instance of `AFImageDownloader` initialized with default values.
88 | */
89 | - (instancetype)init;
90 |
91 | /**
92 | Initializes the `AFImageDownloader` instance with the given session manager, download prioritization, maximum active download count and image cache.
93 |
94 | @param sessionManager The session manager to use to download images
95 | @param downloadPrioritization The download prioritization of the download queue.
96 | @param maximumActiveDownloads The maximum number of active downloads allowed at any given time. Recommend `4`.
97 | @param imageCache The image cache used to store all downloaded images in.
98 |
99 | @return The new `AFImageDownloader` instance.
100 | */
101 | - (instancetype)initWithSessionManager:(AFHTTPSessionManager *)sessionManager
102 | downloadPrioritization:(AFImageDownloadPrioritization)downloadPrioritization
103 | maximumActiveDownloads:(NSInteger)maximumActiveDownloads
104 | imageCache:(nullable id )imageCache;
105 |
106 | /**
107 | Creates a data task using the `sessionManager` instance for the specified URL request.
108 |
109 | If the same data task is already in the queue or currently being downloaded, the success and failure blocks are
110 | appended to the already existing task. Once the task completes, all success or failure blocks attached to the
111 | task are executed in the order they were added.
112 |
113 | @param request The URL request.
114 | @param success A block to be executed when the image data task finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the image created from the response data of request. If the image was returned from cache, the response parameter will be `nil`.
115 | @param failure A block object to be executed when the image data task finishes unsuccessfully, or that finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the error object describing the network or parsing error that occurred.
116 |
117 | @return The image download receipt for the data task if available. `nil` if the image is stored in the image
118 | cache and the URL request cache policy allows the cache to be used.
119 | */
120 | - (nullable AFImageDownloadReceipt *)downloadImageForURLRequest:(NSURLRequest *)request
121 | success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *responseObject))success
122 | failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure;
123 |
124 | /**
125 | Cancels the data task in the receipt by removing the corresponding success and failure blocks and cancelling the data task if necessary.
126 |
127 | If the data task is pending in the queue, it will be cancelled if no other success and failure blocks are registered with the data task. If the data task is currently executing or is already completed, the success and failure blocks are removed and will not be called when the task finishes.
128 |
129 | @param imageDownloadReceipt The image download receipt to cancel.
130 | */
131 | - (void)cancelTaskForImageDownloadReceipt:(AFImageDownloadReceipt *)imageDownloadReceipt;
132 |
133 | @end
134 |
135 | #endif
136 |
137 | NS_ASSUME_NONNULL_END
138 |
--------------------------------------------------------------------------------
/JPGiftManager/AFNetworking/UIKit+AFNetworking/AFNetworkActivityIndicatorManager.h:
--------------------------------------------------------------------------------
1 | // AFNetworkActivityIndicatorManager.h
2 | // Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/)
3 | //
4 | // Permission is hereby granted, free of charge, to any person obtaining a copy
5 | // of this software and associated documentation files (the "Software"), to deal
6 | // in the Software without restriction, including without limitation the rights
7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | // copies of the Software, and to permit persons to whom the Software is
9 | // furnished to do so, subject to the following conditions:
10 | //
11 | // The above copyright notice and this permission notice shall be included in
12 | // all copies or substantial portions of the Software.
13 | //
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 | // THE SOFTWARE.
21 |
22 | #import
23 |
24 | #import
25 |
26 | #if TARGET_OS_IOS
27 |
28 | #import
29 |
30 | NS_ASSUME_NONNULL_BEGIN
31 |
32 | /**
33 | `AFNetworkActivityIndicatorManager` manages the state of the network activity indicator in the status bar. When enabled, it will listen for notifications indicating that a session task has started or finished, and start or stop animating the indicator accordingly. The number of active requests is incremented and decremented much like a stack or a semaphore, and the activity indicator will animate so long as that number is greater than zero.
34 |
35 | You should enable the shared instance of `AFNetworkActivityIndicatorManager` when your application finishes launching. In `AppDelegate application:didFinishLaunchingWithOptions:` you can do so with the following code:
36 |
37 | [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
38 |
39 | By setting `enabled` to `YES` for `sharedManager`, the network activity indicator will show and hide automatically as requests start and finish. You should not ever need to call `incrementActivityCount` or `decrementActivityCount` yourself.
40 |
41 | See the Apple Human Interface Guidelines section about the Network Activity Indicator for more information:
42 | http://developer.apple.com/library/iOS/#documentation/UserExperience/Conceptual/MobileHIG/UIElementGuidelines/UIElementGuidelines.html#//apple_ref/doc/uid/TP40006556-CH13-SW44
43 | */
44 | NS_EXTENSION_UNAVAILABLE_IOS("Use view controller based solutions where appropriate instead.")
45 | @interface AFNetworkActivityIndicatorManager : NSObject
46 |
47 | /**
48 | A Boolean value indicating whether the manager is enabled.
49 |
50 | If YES, the manager will change status bar network activity indicator according to network operation notifications it receives. The default value is NO.
51 | */
52 | @property (nonatomic, assign, getter = isEnabled) BOOL enabled;
53 |
54 | /**
55 | A Boolean value indicating whether the network activity indicator manager is currently active.
56 | */
57 | @property (readonly, nonatomic, assign, getter=isNetworkActivityIndicatorVisible) BOOL networkActivityIndicatorVisible;
58 |
59 | /**
60 | A time interval indicating the minimum duration of networking activity that should occur before the activity indicator is displayed. The default value 1 second. If the network activity indicator should be displayed immediately when network activity occurs, this value should be set to 0 seconds.
61 |
62 | Apple's HIG describes the following:
63 |
64 | > Display the network activity indicator to provide feedback when your app accesses the network for more than a couple of seconds. If the operation finishes sooner than that, you don’t have to show the network activity indicator, because the indicator is likely to disappear before users notice its presence.
65 |
66 | */
67 | @property (nonatomic, assign) NSTimeInterval activationDelay;
68 |
69 | /**
70 | A time interval indicating the duration of time of no networking activity required before the activity indicator is disabled. This allows for continuous display of the network activity indicator across multiple requests. The default value is 0.17 seconds.
71 | */
72 |
73 | @property (nonatomic, assign) NSTimeInterval completionDelay;
74 |
75 | /**
76 | Returns the shared network activity indicator manager object for the system.
77 |
78 | @return The systemwide network activity indicator manager.
79 | */
80 | + (instancetype)sharedManager;
81 |
82 | /**
83 | Increments the number of active network requests. If this number was zero before incrementing, this will start animating the status bar network activity indicator.
84 | */
85 | - (void)incrementActivityCount;
86 |
87 | /**
88 | Decrements the number of active network requests. If this number becomes zero after decrementing, this will stop animating the status bar network activity indicator.
89 | */
90 | - (void)decrementActivityCount;
91 |
92 | /**
93 | Set the a custom method to be executed when the network activity indicator manager should be hidden/shown. By default, this is null, and the UIApplication Network Activity Indicator will be managed automatically. If this block is set, it is the responsiblity of the caller to manager the network activity indicator going forward.
94 |
95 | @param block A block to be executed when the network activity indicator status changes.
96 | */
97 | - (void)setNetworkingActivityActionWithBlock:(nullable void (^)(BOOL networkActivityIndicatorVisible))block;
98 |
99 | @end
100 |
101 | NS_ASSUME_NONNULL_END
102 |
103 | #endif
104 |
--------------------------------------------------------------------------------
/JPGiftManager/AFNetworking/UIKit+AFNetworking/UIActivityIndicatorView+AFNetworking.h:
--------------------------------------------------------------------------------
1 | // UIActivityIndicatorView+AFNetworking.h
2 | // Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/)
3 | //
4 | // Permission is hereby granted, free of charge, to any person obtaining a copy
5 | // of this software and associated documentation files (the "Software"), to deal
6 | // in the Software without restriction, including without limitation the rights
7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | // copies of the Software, and to permit persons to whom the Software is
9 | // furnished to do so, subject to the following conditions:
10 | //
11 | // The above copyright notice and this permission notice shall be included in
12 | // all copies or substantial portions of the Software.
13 | //
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 | // THE SOFTWARE.
21 |
22 | #import
23 |
24 | #import
25 |
26 | #if TARGET_OS_IOS || TARGET_OS_TV
27 |
28 | #import
29 |
30 | /**
31 | This category adds methods to the UIKit framework's `UIActivityIndicatorView` class. The methods in this category provide support for automatically starting and stopping animation depending on the loading state of a session task.
32 | */
33 | @interface UIActivityIndicatorView (AFNetworking)
34 |
35 | ///----------------------------------
36 | /// @name Animating for Session Tasks
37 | ///----------------------------------
38 |
39 | /**
40 | Binds the animating state to the state of the specified task.
41 |
42 | @param task The task. If `nil`, automatic updating from any previously specified operation will be disabled.
43 | */
44 | - (void)setAnimatingWithStateOfTask:(nullable NSURLSessionTask *)task;
45 |
46 | @end
47 |
48 | #endif
49 |
--------------------------------------------------------------------------------
/JPGiftManager/AFNetworking/UIKit+AFNetworking/UIActivityIndicatorView+AFNetworking.m:
--------------------------------------------------------------------------------
1 | // UIActivityIndicatorView+AFNetworking.m
2 | // Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/)
3 | //
4 | // Permission is hereby granted, free of charge, to any person obtaining a copy
5 | // of this software and associated documentation files (the "Software"), to deal
6 | // in the Software without restriction, including without limitation the rights
7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | // copies of the Software, and to permit persons to whom the Software is
9 | // furnished to do so, subject to the following conditions:
10 | //
11 | // The above copyright notice and this permission notice shall be included in
12 | // all copies or substantial portions of the Software.
13 | //
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 | // THE SOFTWARE.
21 |
22 | #import "UIActivityIndicatorView+AFNetworking.h"
23 | #import
24 |
25 | #if TARGET_OS_IOS || TARGET_OS_TV
26 |
27 | #import "AFURLSessionManager.h"
28 |
29 | @interface AFActivityIndicatorViewNotificationObserver : NSObject
30 | @property (readonly, nonatomic, weak) UIActivityIndicatorView *activityIndicatorView;
31 | - (instancetype)initWithActivityIndicatorView:(UIActivityIndicatorView *)activityIndicatorView;
32 |
33 | - (void)setAnimatingWithStateOfTask:(NSURLSessionTask *)task;
34 |
35 | @end
36 |
37 | @implementation UIActivityIndicatorView (AFNetworking)
38 |
39 | - (AFActivityIndicatorViewNotificationObserver *)af_notificationObserver {
40 | AFActivityIndicatorViewNotificationObserver *notificationObserver = objc_getAssociatedObject(self, @selector(af_notificationObserver));
41 | if (notificationObserver == nil) {
42 | notificationObserver = [[AFActivityIndicatorViewNotificationObserver alloc] initWithActivityIndicatorView:self];
43 | objc_setAssociatedObject(self, @selector(af_notificationObserver), notificationObserver, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
44 | }
45 | return notificationObserver;
46 | }
47 |
48 | - (void)setAnimatingWithStateOfTask:(NSURLSessionTask *)task {
49 | [[self af_notificationObserver] setAnimatingWithStateOfTask:task];
50 | }
51 |
52 | @end
53 |
54 | @implementation AFActivityIndicatorViewNotificationObserver
55 |
56 | - (instancetype)initWithActivityIndicatorView:(UIActivityIndicatorView *)activityIndicatorView
57 | {
58 | self = [super init];
59 | if (self) {
60 | _activityIndicatorView = activityIndicatorView;
61 | }
62 | return self;
63 | }
64 |
65 | - (void)setAnimatingWithStateOfTask:(NSURLSessionTask *)task {
66 | NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
67 |
68 | [notificationCenter removeObserver:self name:AFNetworkingTaskDidResumeNotification object:nil];
69 | [notificationCenter removeObserver:self name:AFNetworkingTaskDidSuspendNotification object:nil];
70 | [notificationCenter removeObserver:self name:AFNetworkingTaskDidCompleteNotification object:nil];
71 |
72 | if (task) {
73 | if (task.state != NSURLSessionTaskStateCompleted) {
74 |
75 | #pragma clang diagnostic push
76 | #pragma clang diagnostic ignored "-Wreceiver-is-weak"
77 | #pragma clang diagnostic ignored "-Warc-repeated-use-of-weak"
78 | if (task.state == NSURLSessionTaskStateRunning) {
79 | [self.activityIndicatorView startAnimating];
80 | } else {
81 | [self.activityIndicatorView stopAnimating];
82 | }
83 | #pragma clang diagnostic pop
84 |
85 | [notificationCenter addObserver:self selector:@selector(af_startAnimating) name:AFNetworkingTaskDidResumeNotification object:task];
86 | [notificationCenter addObserver:self selector:@selector(af_stopAnimating) name:AFNetworkingTaskDidCompleteNotification object:task];
87 | [notificationCenter addObserver:self selector:@selector(af_stopAnimating) name:AFNetworkingTaskDidSuspendNotification object:task];
88 | }
89 | }
90 | }
91 |
92 | #pragma mark -
93 |
94 | - (void)af_startAnimating {
95 | dispatch_async(dispatch_get_main_queue(), ^{
96 | #pragma clang diagnostic push
97 | #pragma clang diagnostic ignored "-Wreceiver-is-weak"
98 | [self.activityIndicatorView startAnimating];
99 | #pragma clang diagnostic pop
100 | });
101 | }
102 |
103 | - (void)af_stopAnimating {
104 | dispatch_async(dispatch_get_main_queue(), ^{
105 | #pragma clang diagnostic push
106 | #pragma clang diagnostic ignored "-Wreceiver-is-weak"
107 | [self.activityIndicatorView stopAnimating];
108 | #pragma clang diagnostic pop
109 | });
110 | }
111 |
112 | #pragma mark -
113 |
114 | - (void)dealloc {
115 | NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
116 |
117 | [notificationCenter removeObserver:self name:AFNetworkingTaskDidCompleteNotification object:nil];
118 | [notificationCenter removeObserver:self name:AFNetworkingTaskDidResumeNotification object:nil];
119 | [notificationCenter removeObserver:self name:AFNetworkingTaskDidSuspendNotification object:nil];
120 | }
121 |
122 | @end
123 |
124 | #endif
125 |
--------------------------------------------------------------------------------
/JPGiftManager/AFNetworking/UIKit+AFNetworking/UIImage+AFNetworking.h:
--------------------------------------------------------------------------------
1 | //
2 | // UIImage+AFNetworking.h
3 | //
4 | //
5 | // Created by Paulo Ferreira on 08/07/15.
6 | //
7 | // Permission is hereby granted, free of charge, to any person obtaining a copy
8 | // of this software and associated documentation files (the "Software"), to deal
9 | // in the Software without restriction, including without limitation the rights
10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 | // copies of the Software, and to permit persons to whom the Software is
12 | // furnished to do so, subject to the following conditions:
13 | //
14 | // The above copyright notice and this permission notice shall be included in
15 | // all copies or substantial portions of the Software.
16 | //
17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 | // THE SOFTWARE.
24 |
25 | #if TARGET_OS_IOS || TARGET_OS_TV
26 |
27 | #import
28 |
29 | @interface UIImage (AFNetworking)
30 |
31 | + (UIImage*) safeImageWithData:(NSData*)data;
32 |
33 | @end
34 |
35 | #endif
36 |
--------------------------------------------------------------------------------
/JPGiftManager/AFNetworking/UIKit+AFNetworking/UIImageView+AFNetworking.h:
--------------------------------------------------------------------------------
1 | // UIImageView+AFNetworking.h
2 | // Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/)
3 | //
4 | // Permission is hereby granted, free of charge, to any person obtaining a copy
5 | // of this software and associated documentation files (the "Software"), to deal
6 | // in the Software without restriction, including without limitation the rights
7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | // copies of the Software, and to permit persons to whom the Software is
9 | // furnished to do so, subject to the following conditions:
10 | //
11 | // The above copyright notice and this permission notice shall be included in
12 | // all copies or substantial portions of the Software.
13 | //
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 | // THE SOFTWARE.
21 |
22 | #import
23 |
24 | #import
25 |
26 | #if TARGET_OS_IOS || TARGET_OS_TV
27 |
28 | #import
29 |
30 | NS_ASSUME_NONNULL_BEGIN
31 |
32 | @class AFImageDownloader;
33 |
34 | /**
35 | This category adds methods to the UIKit framework's `UIImageView` class. The methods in this category provide support for loading remote images asynchronously from a URL.
36 | */
37 | @interface UIImageView (AFNetworking)
38 |
39 | ///------------------------------------
40 | /// @name Accessing the Image Downloader
41 | ///------------------------------------
42 |
43 | /**
44 | Set the shared image downloader used to download images.
45 |
46 | @param imageDownloader The shared image downloader used to download images.
47 | */
48 | + (void)setSharedImageDownloader:(AFImageDownloader *)imageDownloader;
49 |
50 | /**
51 | The shared image downloader used to download images.
52 | */
53 | + (AFImageDownloader *)sharedImageDownloader;
54 |
55 | ///--------------------
56 | /// @name Setting Image
57 | ///--------------------
58 |
59 | /**
60 | Asynchronously downloads an image from the specified URL, and sets it once the request is finished. Any previous image request for the receiver will be cancelled.
61 |
62 | If the image is cached locally, the image is set immediately, otherwise the specified placeholder image will be set immediately, and then the remote image will be set once the request is finished.
63 |
64 | By default, URL requests have a `Accept` header field value of "image / *", a cache policy of `NSURLCacheStorageAllowed` and a timeout interval of 30 seconds, and are set not handle cookies. To configure URL requests differently, use `setImageWithURLRequest:placeholderImage:success:failure:`
65 |
66 | @param url The URL used for the image request.
67 | */
68 | - (void)setImageWithURL:(NSURL *)url;
69 |
70 | /**
71 | Asynchronously downloads an image from the specified URL, and sets it once the request is finished. Any previous image request for the receiver will be cancelled.
72 |
73 | If the image is cached locally, the image is set immediately, otherwise the specified placeholder image will be set immediately, and then the remote image will be set once the request is finished.
74 |
75 | By default, URL requests have a `Accept` header field value of "image / *", a cache policy of `NSURLCacheStorageAllowed` and a timeout interval of 30 seconds, and are set not handle cookies. To configure URL requests differently, use `setImageWithURLRequest:placeholderImage:success:failure:`
76 |
77 | @param url The URL used for the image request.
78 | @param placeholderImage The image to be set initially, until the image request finishes. If `nil`, the image view will not change its image until the image request finishes.
79 | */
80 | - (void)setImageWithURL:(NSURL *)url
81 | placeholderImage:(nullable UIImage *)placeholderImage;
82 |
83 | /**
84 | Asynchronously downloads an image from the specified URL request, and sets it once the request is finished. Any previous image request for the receiver will be cancelled.
85 |
86 | If the image is cached locally, the image is set immediately, otherwise the specified placeholder image will be set immediately, and then the remote image will be set once the request is finished.
87 |
88 | If a success block is specified, it is the responsibility of the block to set the image of the image view before returning. If no success block is specified, the default behavior of setting the image with `self.image = image` is applied.
89 |
90 | @param urlRequest The URL request used for the image request.
91 | @param placeholderImage The image to be set initially, until the image request finishes. If `nil`, the image view will not change its image until the image request finishes.
92 | @param success A block to be executed when the image data task finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the image created from the response data of request. If the image was returned from cache, the response parameter will be `nil`.
93 | @param failure A block object to be executed when the image data task finishes unsuccessfully, or that finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the error object describing the network or parsing error that occurred.
94 | */
95 | - (void)setImageWithURLRequest:(NSURLRequest *)urlRequest
96 | placeholderImage:(nullable UIImage *)placeholderImage
97 | success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *image))success
98 | failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure;
99 |
100 | /**
101 | Cancels any executing image operation for the receiver, if one exists.
102 | */
103 | - (void)cancelImageDownloadTask;
104 |
105 | @end
106 |
107 | NS_ASSUME_NONNULL_END
108 |
109 | #endif
110 |
--------------------------------------------------------------------------------
/JPGiftManager/AFNetworking/UIKit+AFNetworking/UIImageView+AFNetworking.m:
--------------------------------------------------------------------------------
1 | // UIImageView+AFNetworking.m
2 | // Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/)
3 | //
4 | // Permission is hereby granted, free of charge, to any person obtaining a copy
5 | // of this software and associated documentation files (the "Software"), to deal
6 | // in the Software without restriction, including without limitation the rights
7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | // copies of the Software, and to permit persons to whom the Software is
9 | // furnished to do so, subject to the following conditions:
10 | //
11 | // The above copyright notice and this permission notice shall be included in
12 | // all copies or substantial portions of the Software.
13 | //
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 | // THE SOFTWARE.
21 |
22 | #import "UIImageView+AFNetworking.h"
23 |
24 | #import
25 |
26 | #if TARGET_OS_IOS || TARGET_OS_TV
27 |
28 | #import "AFImageDownloader.h"
29 |
30 | @interface UIImageView (_AFNetworking)
31 | @property (readwrite, nonatomic, strong, setter = af_setActiveImageDownloadReceipt:) AFImageDownloadReceipt *af_activeImageDownloadReceipt;
32 | @end
33 |
34 | @implementation UIImageView (_AFNetworking)
35 |
36 | - (AFImageDownloadReceipt *)af_activeImageDownloadReceipt {
37 | return (AFImageDownloadReceipt *)objc_getAssociatedObject(self, @selector(af_activeImageDownloadReceipt));
38 | }
39 |
40 | - (void)af_setActiveImageDownloadReceipt:(AFImageDownloadReceipt *)imageDownloadReceipt {
41 | objc_setAssociatedObject(self, @selector(af_activeImageDownloadReceipt), imageDownloadReceipt, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
42 | }
43 |
44 | @end
45 |
46 | #pragma mark -
47 |
48 | @implementation UIImageView (AFNetworking)
49 |
50 | + (AFImageDownloader *)sharedImageDownloader {
51 |
52 | #pragma clang diagnostic push
53 | #pragma clang diagnostic ignored "-Wgnu"
54 | return objc_getAssociatedObject(self, @selector(sharedImageDownloader)) ?: [AFImageDownloader defaultInstance];
55 | #pragma clang diagnostic pop
56 | }
57 |
58 | + (void)setSharedImageDownloader:(AFImageDownloader *)imageDownloader {
59 | objc_setAssociatedObject(self, @selector(sharedImageDownloader), imageDownloader, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
60 | }
61 |
62 | #pragma mark -
63 |
64 | - (void)setImageWithURL:(NSURL *)url {
65 | [self setImageWithURL:url placeholderImage:nil];
66 | }
67 |
68 | - (void)setImageWithURL:(NSURL *)url
69 | placeholderImage:(UIImage *)placeholderImage
70 | {
71 | NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
72 | [request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
73 |
74 | [self setImageWithURLRequest:request placeholderImage:placeholderImage success:nil failure:nil];
75 | }
76 |
77 | - (void)setImageWithURLRequest:(NSURLRequest *)urlRequest
78 | placeholderImage:(UIImage *)placeholderImage
79 | success:(void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *image))success
80 | failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure
81 | {
82 |
83 | if ([urlRequest URL] == nil) {
84 | [self cancelImageDownloadTask];
85 | self.image = placeholderImage;
86 | return;
87 | }
88 |
89 | if ([self isActiveTaskURLEqualToURLRequest:urlRequest]){
90 | return;
91 | }
92 |
93 | [self cancelImageDownloadTask];
94 |
95 | AFImageDownloader *downloader = [[self class] sharedImageDownloader];
96 | id imageCache = downloader.imageCache;
97 |
98 | //Use the image from the image cache if it exists
99 | UIImage *cachedImage = [imageCache imageforRequest:urlRequest withAdditionalIdentifier:nil];
100 | if (cachedImage) {
101 | if (success) {
102 | success(urlRequest, nil, cachedImage);
103 | } else {
104 | self.image = cachedImage;
105 | }
106 | self.af_activeImageDownloadReceipt = nil;
107 | } else {
108 | if (placeholderImage) {
109 | self.image = placeholderImage;
110 | }
111 |
112 | __weak __typeof(self)weakSelf = self;
113 | AFImageDownloadReceipt *receipt;
114 | receipt = [downloader
115 | downloadImageForURLRequest:urlRequest
116 | success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull responseObject) {
117 | __strong __typeof(weakSelf)strongSelf = weakSelf;
118 | if ([strongSelf isActiveTaskURLEqualToURLRequest:request]) {
119 | if (success) {
120 | success(request, response, responseObject);
121 | } else if(responseObject) {
122 | strongSelf.image = responseObject;
123 | }
124 | strongSelf.af_activeImageDownloadReceipt = nil;
125 | }
126 |
127 | }
128 | failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
129 | __strong __typeof(weakSelf)strongSelf = weakSelf;
130 | if ([strongSelf isActiveTaskURLEqualToURLRequest:request]) {
131 | if (failure) {
132 | failure(request, response, error);
133 | }
134 | strongSelf.af_activeImageDownloadReceipt = nil;
135 | }
136 | }];
137 |
138 | self.af_activeImageDownloadReceipt = receipt;
139 | }
140 | }
141 |
142 | - (void)cancelImageDownloadTask {
143 | if (self.af_activeImageDownloadReceipt != nil) {
144 | [[self.class sharedImageDownloader] cancelTaskForImageDownloadReceipt:self.af_activeImageDownloadReceipt];
145 | self.af_activeImageDownloadReceipt = nil;
146 | }
147 | }
148 |
149 | - (BOOL)isActiveTaskURLEqualToURLRequest:(NSURLRequest *)urlRequest {
150 | return [self.af_activeImageDownloadReceipt.task.originalRequest.URL.absoluteString isEqualToString:urlRequest.URL.absoluteString];
151 | }
152 |
153 | @end
154 |
155 | #endif
156 |
--------------------------------------------------------------------------------
/JPGiftManager/AFNetworking/UIKit+AFNetworking/UIKit+AFNetworking.h:
--------------------------------------------------------------------------------
1 | // UIKit+AFNetworking.h
2 | //
3 | // Copyright (c) 2013 AFNetworking (http://afnetworking.com/)
4 | //
5 | // Permission is hereby granted, free of charge, to any person obtaining a copy
6 | // of this software and associated documentation files (the "Software"), to deal
7 | // in the Software without restriction, including without limitation the rights
8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | // copies of the Software, and to permit persons to whom the Software is
10 | // furnished to do so, subject to the following conditions:
11 | //
12 | // The above copyright notice and this permission notice shall be included in
13 | // all copies or substantial portions of the Software.
14 | //
15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 | // THE SOFTWARE.
22 |
23 | #if TARGET_OS_IOS || TARGET_OS_TV
24 | #import
25 |
26 | #ifndef _UIKIT_AFNETWORKING_
27 | #define _UIKIT_AFNETWORKING_
28 |
29 | #if TARGET_OS_IOS
30 | #import "AFAutoPurgingImageCache.h"
31 | #import "AFImageDownloader.h"
32 | #import "AFNetworkActivityIndicatorManager.h"
33 | #import "UIRefreshControl+AFNetworking.h"
34 | #import "UIWebView+AFNetworking.h"
35 | #endif
36 |
37 | #import "UIActivityIndicatorView+AFNetworking.h"
38 | #import "UIButton+AFNetworking.h"
39 | #import "UIImageView+AFNetworking.h"
40 | #import "UIProgressView+AFNetworking.h"
41 | #endif /* _UIKIT_AFNETWORKING_ */
42 | #endif
43 |
--------------------------------------------------------------------------------
/JPGiftManager/AFNetworking/UIKit+AFNetworking/UIProgressView+AFNetworking.h:
--------------------------------------------------------------------------------
1 | // UIProgressView+AFNetworking.h
2 | // Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/)
3 | //
4 | // Permission is hereby granted, free of charge, to any person obtaining a copy
5 | // of this software and associated documentation files (the "Software"), to deal
6 | // in the Software without restriction, including without limitation the rights
7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | // copies of the Software, and to permit persons to whom the Software is
9 | // furnished to do so, subject to the following conditions:
10 | //
11 | // The above copyright notice and this permission notice shall be included in
12 | // all copies or substantial portions of the Software.
13 | //
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 | // THE SOFTWARE.
21 |
22 | #import
23 |
24 | #import
25 |
26 | #if TARGET_OS_IOS || TARGET_OS_TV
27 |
28 | #import
29 |
30 | NS_ASSUME_NONNULL_BEGIN
31 |
32 |
33 | /**
34 | This category adds methods to the UIKit framework's `UIProgressView` class. The methods in this category provide support for binding the progress to the upload and download progress of a session task.
35 | */
36 | @interface UIProgressView (AFNetworking)
37 |
38 | ///------------------------------------
39 | /// @name Setting Session Task Progress
40 | ///------------------------------------
41 |
42 | /**
43 | Binds the progress to the upload progress of the specified session task.
44 |
45 | @param task The session task.
46 | @param animated `YES` if the change should be animated, `NO` if the change should happen immediately.
47 | */
48 | - (void)setProgressWithUploadProgressOfTask:(NSURLSessionUploadTask *)task
49 | animated:(BOOL)animated;
50 |
51 | /**
52 | Binds the progress to the download progress of the specified session task.
53 |
54 | @param task The session task.
55 | @param animated `YES` if the change should be animated, `NO` if the change should happen immediately.
56 | */
57 | - (void)setProgressWithDownloadProgressOfTask:(NSURLSessionDownloadTask *)task
58 | animated:(BOOL)animated;
59 |
60 | @end
61 |
62 | NS_ASSUME_NONNULL_END
63 |
64 | #endif
65 |
--------------------------------------------------------------------------------
/JPGiftManager/AFNetworking/UIKit+AFNetworking/UIProgressView+AFNetworking.m:
--------------------------------------------------------------------------------
1 | // UIProgressView+AFNetworking.m
2 | // Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/)
3 | //
4 | // Permission is hereby granted, free of charge, to any person obtaining a copy
5 | // of this software and associated documentation files (the "Software"), to deal
6 | // in the Software without restriction, including without limitation the rights
7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | // copies of the Software, and to permit persons to whom the Software is
9 | // furnished to do so, subject to the following conditions:
10 | //
11 | // The above copyright notice and this permission notice shall be included in
12 | // all copies or substantial portions of the Software.
13 | //
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 | // THE SOFTWARE.
21 |
22 | #import "UIProgressView+AFNetworking.h"
23 |
24 | #import
25 |
26 | #if TARGET_OS_IOS || TARGET_OS_TV
27 |
28 | #import "AFURLSessionManager.h"
29 |
30 | static void * AFTaskCountOfBytesSentContext = &AFTaskCountOfBytesSentContext;
31 | static void * AFTaskCountOfBytesReceivedContext = &AFTaskCountOfBytesReceivedContext;
32 |
33 | #pragma mark -
34 |
35 | @implementation UIProgressView (AFNetworking)
36 |
37 | - (BOOL)af_uploadProgressAnimated {
38 | return [(NSNumber *)objc_getAssociatedObject(self, @selector(af_uploadProgressAnimated)) boolValue];
39 | }
40 |
41 | - (void)af_setUploadProgressAnimated:(BOOL)animated {
42 | objc_setAssociatedObject(self, @selector(af_uploadProgressAnimated), @(animated), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
43 | }
44 |
45 | - (BOOL)af_downloadProgressAnimated {
46 | return [(NSNumber *)objc_getAssociatedObject(self, @selector(af_downloadProgressAnimated)) boolValue];
47 | }
48 |
49 | - (void)af_setDownloadProgressAnimated:(BOOL)animated {
50 | objc_setAssociatedObject(self, @selector(af_downloadProgressAnimated), @(animated), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
51 | }
52 |
53 | #pragma mark -
54 |
55 | - (void)setProgressWithUploadProgressOfTask:(NSURLSessionUploadTask *)task
56 | animated:(BOOL)animated
57 | {
58 | [task addObserver:self forKeyPath:@"state" options:(NSKeyValueObservingOptions)0 context:AFTaskCountOfBytesSentContext];
59 | [task addObserver:self forKeyPath:@"countOfBytesSent" options:(NSKeyValueObservingOptions)0 context:AFTaskCountOfBytesSentContext];
60 |
61 | [self af_setUploadProgressAnimated:animated];
62 | }
63 |
64 | - (void)setProgressWithDownloadProgressOfTask:(NSURLSessionDownloadTask *)task
65 | animated:(BOOL)animated
66 | {
67 | [task addObserver:self forKeyPath:@"state" options:(NSKeyValueObservingOptions)0 context:AFTaskCountOfBytesReceivedContext];
68 | [task addObserver:self forKeyPath:@"countOfBytesReceived" options:(NSKeyValueObservingOptions)0 context:AFTaskCountOfBytesReceivedContext];
69 |
70 | [self af_setDownloadProgressAnimated:animated];
71 | }
72 |
73 | #pragma mark - NSKeyValueObserving
74 |
75 | - (void)observeValueForKeyPath:(NSString *)keyPath
76 | ofObject:(id)object
77 | change:(__unused NSDictionary *)change
78 | context:(void *)context
79 | {
80 | if (context == AFTaskCountOfBytesSentContext || context == AFTaskCountOfBytesReceivedContext) {
81 | if ([keyPath isEqualToString:NSStringFromSelector(@selector(countOfBytesSent))]) {
82 | if ([object countOfBytesExpectedToSend] > 0) {
83 | dispatch_async(dispatch_get_main_queue(), ^{
84 | [self setProgress:[object countOfBytesSent] / ([object countOfBytesExpectedToSend] * 1.0f) animated:self.af_uploadProgressAnimated];
85 | });
86 | }
87 | }
88 |
89 | if ([keyPath isEqualToString:NSStringFromSelector(@selector(countOfBytesReceived))]) {
90 | if ([object countOfBytesExpectedToReceive] > 0) {
91 | dispatch_async(dispatch_get_main_queue(), ^{
92 | [self setProgress:[object countOfBytesReceived] / ([object countOfBytesExpectedToReceive] * 1.0f) animated:self.af_downloadProgressAnimated];
93 | });
94 | }
95 | }
96 |
97 | if ([keyPath isEqualToString:NSStringFromSelector(@selector(state))]) {
98 | if ([(NSURLSessionTask *)object state] == NSURLSessionTaskStateCompleted) {
99 | @try {
100 | [object removeObserver:self forKeyPath:NSStringFromSelector(@selector(state))];
101 |
102 | if (context == AFTaskCountOfBytesSentContext) {
103 | [object removeObserver:self forKeyPath:NSStringFromSelector(@selector(countOfBytesSent))];
104 | }
105 |
106 | if (context == AFTaskCountOfBytesReceivedContext) {
107 | [object removeObserver:self forKeyPath:NSStringFromSelector(@selector(countOfBytesReceived))];
108 | }
109 | }
110 | @catch (NSException * __unused exception) {}
111 | }
112 | }
113 | }
114 | }
115 |
116 | @end
117 |
118 | #endif
119 |
--------------------------------------------------------------------------------
/JPGiftManager/AFNetworking/UIKit+AFNetworking/UIRefreshControl+AFNetworking.h:
--------------------------------------------------------------------------------
1 | // UIRefreshControl+AFNetworking.m
2 | //
3 | // Copyright (c) 2014 AFNetworking (http://afnetworking.com)
4 | //
5 | // Permission is hereby granted, free of charge, to any person obtaining a copy
6 | // of this software and associated documentation files (the "Software"), to deal
7 | // in the Software without restriction, including without limitation the rights
8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | // copies of the Software, and to permit persons to whom the Software is
10 | // furnished to do so, subject to the following conditions:
11 | //
12 | // The above copyright notice and this permission notice shall be included in
13 | // all copies or substantial portions of the Software.
14 | //
15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 | // THE SOFTWARE.
22 |
23 | #import
24 |
25 | #import
26 |
27 | #if TARGET_OS_IOS
28 |
29 | #import
30 |
31 | NS_ASSUME_NONNULL_BEGIN
32 |
33 | /**
34 | This category adds methods to the UIKit framework's `UIRefreshControl` class. The methods in this category provide support for automatically beginning and ending refreshing depending on the loading state of a session task.
35 | */
36 | @interface UIRefreshControl (AFNetworking)
37 |
38 | ///-----------------------------------
39 | /// @name Refreshing for Session Tasks
40 | ///-----------------------------------
41 |
42 | /**
43 | Binds the refreshing state to the state of the specified task.
44 |
45 | @param task The task. If `nil`, automatic updating from any previously specified operation will be disabled.
46 | */
47 | - (void)setRefreshingWithStateOfTask:(NSURLSessionTask *)task;
48 |
49 | @end
50 |
51 | NS_ASSUME_NONNULL_END
52 |
53 | #endif
54 |
--------------------------------------------------------------------------------
/JPGiftManager/AFNetworking/UIKit+AFNetworking/UIRefreshControl+AFNetworking.m:
--------------------------------------------------------------------------------
1 | // UIRefreshControl+AFNetworking.m
2 | //
3 | // Copyright (c) 2014 AFNetworking (http://afnetworking.com)
4 | //
5 | // Permission is hereby granted, free of charge, to any person obtaining a copy
6 | // of this software and associated documentation files (the "Software"), to deal
7 | // in the Software without restriction, including without limitation the rights
8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | // copies of the Software, and to permit persons to whom the Software is
10 | // furnished to do so, subject to the following conditions:
11 | //
12 | // The above copyright notice and this permission notice shall be included in
13 | // all copies or substantial portions of the Software.
14 | //
15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 | // THE SOFTWARE.
22 |
23 | #import "UIRefreshControl+AFNetworking.h"
24 | #import
25 |
26 | #if TARGET_OS_IOS
27 |
28 | #import "AFURLSessionManager.h"
29 |
30 | @interface AFRefreshControlNotificationObserver : NSObject
31 | @property (readonly, nonatomic, weak) UIRefreshControl *refreshControl;
32 | - (instancetype)initWithActivityRefreshControl:(UIRefreshControl *)refreshControl;
33 |
34 | - (void)setRefreshingWithStateOfTask:(NSURLSessionTask *)task;
35 |
36 | @end
37 |
38 | @implementation UIRefreshControl (AFNetworking)
39 |
40 | - (AFRefreshControlNotificationObserver *)af_notificationObserver {
41 | AFRefreshControlNotificationObserver *notificationObserver = objc_getAssociatedObject(self, @selector(af_notificationObserver));
42 | if (notificationObserver == nil) {
43 | notificationObserver = [[AFRefreshControlNotificationObserver alloc] initWithActivityRefreshControl:self];
44 | objc_setAssociatedObject(self, @selector(af_notificationObserver), notificationObserver, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
45 | }
46 | return notificationObserver;
47 | }
48 |
49 | - (void)setRefreshingWithStateOfTask:(NSURLSessionTask *)task {
50 | [[self af_notificationObserver] setRefreshingWithStateOfTask:task];
51 | }
52 |
53 | @end
54 |
55 | @implementation AFRefreshControlNotificationObserver
56 |
57 | - (instancetype)initWithActivityRefreshControl:(UIRefreshControl *)refreshControl
58 | {
59 | self = [super init];
60 | if (self) {
61 | _refreshControl = refreshControl;
62 | }
63 | return self;
64 | }
65 |
66 | - (void)setRefreshingWithStateOfTask:(NSURLSessionTask *)task {
67 | NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
68 |
69 | [notificationCenter removeObserver:self name:AFNetworkingTaskDidResumeNotification object:nil];
70 | [notificationCenter removeObserver:self name:AFNetworkingTaskDidSuspendNotification object:nil];
71 | [notificationCenter removeObserver:self name:AFNetworkingTaskDidCompleteNotification object:nil];
72 |
73 | if (task) {
74 | #pragma clang diagnostic push
75 | #pragma clang diagnostic ignored "-Wreceiver-is-weak"
76 | #pragma clang diagnostic ignored "-Warc-repeated-use-of-weak"
77 | if (task.state == NSURLSessionTaskStateRunning) {
78 | [self.refreshControl beginRefreshing];
79 |
80 | [notificationCenter addObserver:self selector:@selector(af_beginRefreshing) name:AFNetworkingTaskDidResumeNotification object:task];
81 | [notificationCenter addObserver:self selector:@selector(af_endRefreshing) name:AFNetworkingTaskDidCompleteNotification object:task];
82 | [notificationCenter addObserver:self selector:@selector(af_endRefreshing) name:AFNetworkingTaskDidSuspendNotification object:task];
83 | } else {
84 | [self.refreshControl endRefreshing];
85 | }
86 | #pragma clang diagnostic pop
87 | }
88 | }
89 |
90 | #pragma mark -
91 |
92 | - (void)af_beginRefreshing {
93 | dispatch_async(dispatch_get_main_queue(), ^{
94 | #pragma clang diagnostic push
95 | #pragma clang diagnostic ignored "-Wreceiver-is-weak"
96 | [self.refreshControl beginRefreshing];
97 | #pragma clang diagnostic pop
98 | });
99 | }
100 |
101 | - (void)af_endRefreshing {
102 | dispatch_async(dispatch_get_main_queue(), ^{
103 | #pragma clang diagnostic push
104 | #pragma clang diagnostic ignored "-Wreceiver-is-weak"
105 | [self.refreshControl endRefreshing];
106 | #pragma clang diagnostic pop
107 | });
108 | }
109 |
110 | #pragma mark -
111 |
112 | - (void)dealloc {
113 | NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
114 |
115 | [notificationCenter removeObserver:self name:AFNetworkingTaskDidCompleteNotification object:nil];
116 | [notificationCenter removeObserver:self name:AFNetworkingTaskDidResumeNotification object:nil];
117 | [notificationCenter removeObserver:self name:AFNetworkingTaskDidSuspendNotification object:nil];
118 | }
119 |
120 | @end
121 |
122 | #endif
123 |
--------------------------------------------------------------------------------
/JPGiftManager/AFNetworking/UIKit+AFNetworking/UIWebView+AFNetworking.h:
--------------------------------------------------------------------------------
1 | // UIWebView+AFNetworking.h
2 | // Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/)
3 | //
4 | // Permission is hereby granted, free of charge, to any person obtaining a copy
5 | // of this software and associated documentation files (the "Software"), to deal
6 | // in the Software without restriction, including without limitation the rights
7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | // copies of the Software, and to permit persons to whom the Software is
9 | // furnished to do so, subject to the following conditions:
10 | //
11 | // The above copyright notice and this permission notice shall be included in
12 | // all copies or substantial portions of the Software.
13 | //
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 | // THE SOFTWARE.
21 |
22 | #import
23 |
24 | #import
25 |
26 | #if TARGET_OS_IOS
27 |
28 | #import
29 |
30 | NS_ASSUME_NONNULL_BEGIN
31 |
32 | @class AFHTTPSessionManager;
33 |
34 | /**
35 | This category adds methods to the UIKit framework's `UIWebView` class. The methods in this category provide increased control over the request cycle, including progress monitoring and success / failure handling.
36 |
37 | @discussion When using these category methods, make sure to assign `delegate` for the web view, which implements `–webView:shouldStartLoadWithRequest:navigationType:` appropriately. This allows for tapped links to be loaded through AFNetworking, and can ensure that `canGoBack` & `canGoForward` update their values correctly.
38 | */
39 | @interface UIWebView (AFNetworking)
40 |
41 | /**
42 | The session manager used to download all requests.
43 | */
44 | @property (nonatomic, strong) AFHTTPSessionManager *sessionManager;
45 |
46 | /**
47 | Asynchronously loads the specified request.
48 |
49 | @param request A URL request identifying the location of the content to load. This must not be `nil`.
50 | @param progress A progress object monitoring the current download progress.
51 | @param success A block object to be executed when the request finishes loading successfully. This block returns the HTML string to be loaded by the web view, and takes two arguments: the response, and the response string.
52 | @param failure A block object to be executed when the data task finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a single argument: the error that occurred.
53 | */
54 | - (void)loadRequest:(NSURLRequest *)request
55 | progress:(NSProgress * _Nullable __autoreleasing * _Nullable)progress
56 | success:(nullable NSString * (^)(NSHTTPURLResponse *response, NSString *HTML))success
57 | failure:(nullable void (^)(NSError *error))failure;
58 |
59 | /**
60 | Asynchronously loads the data associated with a particular request with a specified MIME type and text encoding.
61 |
62 | @param request A URL request identifying the location of the content to load. This must not be `nil`.
63 | @param MIMEType The MIME type of the content. Defaults to the content type of the response if not specified.
64 | @param textEncodingName The IANA encoding name, as in `utf-8` or `utf-16`. Defaults to the response text encoding if not specified.
65 | @param progress A progress object monitoring the current download progress.
66 | @param success A block object to be executed when the request finishes loading successfully. This block returns the data to be loaded by the web view and takes two arguments: the response, and the downloaded data.
67 | @param failure A block object to be executed when the data task finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a single argument: the error that occurred.
68 | */
69 | - (void)loadRequest:(NSURLRequest *)request
70 | MIMEType:(nullable NSString *)MIMEType
71 | textEncodingName:(nullable NSString *)textEncodingName
72 | progress:(NSProgress * _Nullable __autoreleasing * _Nullable)progress
73 | success:(nullable NSData * (^)(NSHTTPURLResponse *response, NSData *data))success
74 | failure:(nullable void (^)(NSError *error))failure;
75 |
76 | @end
77 |
78 | NS_ASSUME_NONNULL_END
79 |
80 | #endif
--------------------------------------------------------------------------------
/JPGiftManager/AFNetworking/UIKit+AFNetworking/UIWebView+AFNetworking.m:
--------------------------------------------------------------------------------
1 | // UIWebView+AFNetworking.m
2 | // Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/)
3 | //
4 | // Permission is hereby granted, free of charge, to any person obtaining a copy
5 | // of this software and associated documentation files (the "Software"), to deal
6 | // in the Software without restriction, including without limitation the rights
7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | // copies of the Software, and to permit persons to whom the Software is
9 | // furnished to do so, subject to the following conditions:
10 | //
11 | // The above copyright notice and this permission notice shall be included in
12 | // all copies or substantial portions of the Software.
13 | //
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 | // THE SOFTWARE.
21 |
22 | #import "UIWebView+AFNetworking.h"
23 |
24 | #import
25 |
26 | #if TARGET_OS_IOS
27 |
28 | #import "AFHTTPSessionManager.h"
29 | #import "AFURLResponseSerialization.h"
30 | #import "AFURLRequestSerialization.h"
31 |
32 | @interface UIWebView (_AFNetworking)
33 | @property (readwrite, nonatomic, strong, setter = af_setURLSessionTask:) NSURLSessionDataTask *af_URLSessionTask;
34 | @end
35 |
36 | @implementation UIWebView (_AFNetworking)
37 |
38 | - (NSURLSessionDataTask *)af_URLSessionTask {
39 | return (NSURLSessionDataTask *)objc_getAssociatedObject(self, @selector(af_URLSessionTask));
40 | }
41 |
42 | - (void)af_setURLSessionTask:(NSURLSessionDataTask *)af_URLSessionTask {
43 | objc_setAssociatedObject(self, @selector(af_URLSessionTask), af_URLSessionTask, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
44 | }
45 |
46 | @end
47 |
48 | #pragma mark -
49 |
50 | @implementation UIWebView (AFNetworking)
51 |
52 | - (AFHTTPSessionManager *)sessionManager {
53 | static AFHTTPSessionManager *_af_defaultHTTPSessionManager = nil;
54 | static dispatch_once_t onceToken;
55 | dispatch_once(&onceToken, ^{
56 | _af_defaultHTTPSessionManager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
57 | _af_defaultHTTPSessionManager.requestSerializer = [AFHTTPRequestSerializer serializer];
58 | _af_defaultHTTPSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
59 | });
60 |
61 | #pragma clang diagnostic push
62 | #pragma clang diagnostic ignored "-Wgnu"
63 | return objc_getAssociatedObject(self, @selector(sessionManager)) ?: _af_defaultHTTPSessionManager;
64 | #pragma clang diagnostic pop
65 | }
66 |
67 | - (void)setSessionManager:(AFHTTPSessionManager *)sessionManager {
68 | objc_setAssociatedObject(self, @selector(sessionManager), sessionManager, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
69 | }
70 |
71 | - (AFHTTPResponseSerializer *)responseSerializer {
72 | static AFHTTPResponseSerializer *_af_defaultResponseSerializer = nil;
73 | static dispatch_once_t onceToken;
74 | dispatch_once(&onceToken, ^{
75 | _af_defaultResponseSerializer = [AFHTTPResponseSerializer serializer];
76 | });
77 |
78 | #pragma clang diagnostic push
79 | #pragma clang diagnostic ignored "-Wgnu"
80 | return objc_getAssociatedObject(self, @selector(responseSerializer)) ?: _af_defaultResponseSerializer;
81 | #pragma clang diagnostic pop
82 | }
83 |
84 | - (void)setResponseSerializer:(AFHTTPResponseSerializer *)responseSerializer {
85 | objc_setAssociatedObject(self, @selector(responseSerializer), responseSerializer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
86 | }
87 |
88 | #pragma mark -
89 |
90 | - (void)loadRequest:(NSURLRequest *)request
91 | progress:(NSProgress * _Nullable __autoreleasing * _Nullable)progress
92 | success:(NSString * (^)(NSHTTPURLResponse *response, NSString *HTML))success
93 | failure:(void (^)(NSError *error))failure
94 | {
95 | [self loadRequest:request MIMEType:nil textEncodingName:nil progress:progress success:^NSData *(NSHTTPURLResponse *response, NSData *data) {
96 | NSStringEncoding stringEncoding = NSUTF8StringEncoding;
97 | if (response.textEncodingName) {
98 | CFStringEncoding encoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)response.textEncodingName);
99 | if (encoding != kCFStringEncodingInvalidId) {
100 | stringEncoding = CFStringConvertEncodingToNSStringEncoding(encoding);
101 | }
102 | }
103 |
104 | NSString *string = [[NSString alloc] initWithData:data encoding:stringEncoding];
105 | if (success) {
106 | string = success(response, string);
107 | }
108 |
109 | return [string dataUsingEncoding:stringEncoding];
110 | } failure:failure];
111 | }
112 |
113 | - (void)loadRequest:(NSURLRequest *)request
114 | MIMEType:(NSString *)MIMEType
115 | textEncodingName:(NSString *)textEncodingName
116 | progress:(NSProgress * _Nullable __autoreleasing * _Nullable)progress
117 | success:(NSData * (^)(NSHTTPURLResponse *response, NSData *data))success
118 | failure:(void (^)(NSError *error))failure
119 | {
120 | NSParameterAssert(request);
121 |
122 | if (self.af_URLSessionTask.state == NSURLSessionTaskStateRunning || self.af_URLSessionTask.state == NSURLSessionTaskStateSuspended) {
123 | [self.af_URLSessionTask cancel];
124 | }
125 | self.af_URLSessionTask = nil;
126 |
127 | __weak __typeof(self)weakSelf = self;
128 | NSURLSessionDataTask *dataTask;
129 | dataTask = [self.sessionManager
130 | GET:request.URL.absoluteString
131 | parameters:nil
132 | progress:nil
133 | success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
134 | __strong __typeof(weakSelf) strongSelf = weakSelf;
135 | if (success) {
136 | success((NSHTTPURLResponse *)task.response, responseObject);
137 | }
138 | [strongSelf loadData:responseObject MIMEType:MIMEType textEncodingName:textEncodingName baseURL:[task.currentRequest URL]];
139 |
140 | if ([strongSelf.delegate respondsToSelector:@selector(webViewDidStartLoad:)]) {
141 | [strongSelf.delegate webViewDidFinishLoad:strongSelf];
142 | }
143 | }
144 | failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
145 | if (failure) {
146 | failure(error);
147 | }
148 | }];
149 | self.af_URLSessionTask = dataTask;
150 | *progress = [self.sessionManager downloadProgressForTask:dataTask];
151 | [self.af_URLSessionTask resume];
152 |
153 | if ([self.delegate respondsToSelector:@selector(webViewDidStartLoad:)]) {
154 | [self.delegate webViewDidStartLoad:self];
155 | }
156 | }
157 |
158 | @end
159 |
160 | #endif
--------------------------------------------------------------------------------
/JPGiftManager/AppDelegate.h:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.h
3 | // JPGiftManager
4 | //
5 | // Created by Keep丶Dream on 2018/3/13.
6 | // Copyright © 2018年 dong. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface AppDelegate : UIResponder
12 |
13 | @property (strong, nonatomic) UIWindow *window;
14 |
15 |
16 | @end
17 |
18 |
--------------------------------------------------------------------------------
/JPGiftManager/AppDelegate.m:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.m
3 | // JPGiftManager
4 | //
5 | // Created by Keep丶Dream on 2018/3/13.
6 | // Copyright © 2018年 dong. All rights reserved.
7 | //
8 |
9 | #import "AppDelegate.h"
10 |
11 | @interface AppDelegate ()
12 |
13 | @end
14 |
15 | @implementation AppDelegate
16 |
17 |
18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
19 | // Override point for customization after application launch.
20 | return YES;
21 | }
22 |
23 |
24 | - (void)applicationWillResignActive:(UIApplication *)application {
25 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
27 | }
28 |
29 |
30 | - (void)applicationDidEnterBackground:(UIApplication *)application {
31 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
33 | }
34 |
35 |
36 | - (void)applicationWillEnterForeground:(UIApplication *)application {
37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
38 | }
39 |
40 |
41 | - (void)applicationDidBecomeActive:(UIApplication *)application {
42 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
43 | }
44 |
45 |
46 | - (void)applicationWillTerminate:(UIApplication *)application {
47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
48 | }
49 |
50 |
51 | @end
52 |
--------------------------------------------------------------------------------
/JPGiftManager/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "iphone",
5 | "size" : "20x20",
6 | "scale" : "2x"
7 | },
8 | {
9 | "idiom" : "iphone",
10 | "size" : "20x20",
11 | "scale" : "3x"
12 | },
13 | {
14 | "idiom" : "iphone",
15 | "size" : "29x29",
16 | "scale" : "2x"
17 | },
18 | {
19 | "idiom" : "iphone",
20 | "size" : "29x29",
21 | "scale" : "3x"
22 | },
23 | {
24 | "idiom" : "iphone",
25 | "size" : "40x40",
26 | "scale" : "2x"
27 | },
28 | {
29 | "idiom" : "iphone",
30 | "size" : "40x40",
31 | "scale" : "3x"
32 | },
33 | {
34 | "idiom" : "iphone",
35 | "size" : "60x60",
36 | "scale" : "2x"
37 | },
38 | {
39 | "idiom" : "iphone",
40 | "size" : "60x60",
41 | "scale" : "3x"
42 | },
43 | {
44 | "idiom" : "ipad",
45 | "size" : "20x20",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "ipad",
50 | "size" : "20x20",
51 | "scale" : "2x"
52 | },
53 | {
54 | "idiom" : "ipad",
55 | "size" : "29x29",
56 | "scale" : "1x"
57 | },
58 | {
59 | "idiom" : "ipad",
60 | "size" : "29x29",
61 | "scale" : "2x"
62 | },
63 | {
64 | "idiom" : "ipad",
65 | "size" : "40x40",
66 | "scale" : "1x"
67 | },
68 | {
69 | "idiom" : "ipad",
70 | "size" : "40x40",
71 | "scale" : "2x"
72 | },
73 | {
74 | "idiom" : "ipad",
75 | "size" : "76x76",
76 | "scale" : "1x"
77 | },
78 | {
79 | "idiom" : "ipad",
80 | "size" : "76x76",
81 | "scale" : "2x"
82 | },
83 | {
84 | "idiom" : "ipad",
85 | "size" : "83.5x83.5",
86 | "scale" : "2x"
87 | },
88 | {
89 | "idiom" : "ios-marketing",
90 | "size" : "1024x1024",
91 | "scale" : "1x"
92 | }
93 | ],
94 | "info" : {
95 | "version" : 1,
96 | "author" : "xcode"
97 | }
98 | }
--------------------------------------------------------------------------------
/JPGiftManager/Assets.xcassets/Cece_live_star_small.imageset/Cece_live_star_small@2x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/baiyidjp/JPGiftManager/768830504fd7e51bba8104b15509236dfd2523f3/JPGiftManager/Assets.xcassets/Cece_live_star_small.imageset/Cece_live_star_small@2x.png
--------------------------------------------------------------------------------
/JPGiftManager/Assets.xcassets/Cece_live_star_small.imageset/Cece_live_star_small@3x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/baiyidjp/JPGiftManager/768830504fd7e51bba8104b15509236dfd2523f3/JPGiftManager/Assets.xcassets/Cece_live_star_small.imageset/Cece_live_star_small@3x.png
--------------------------------------------------------------------------------
/JPGiftManager/Assets.xcassets/Cece_live_star_small.imageset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "universal",
5 | "scale" : "1x"
6 | },
7 | {
8 | "idiom" : "universal",
9 | "filename" : "Cece_live_star_small@2x.png",
10 | "scale" : "2x"
11 | },
12 | {
13 | "idiom" : "universal",
14 | "filename" : "Cece_live_star_small@3x.png",
15 | "scale" : "3x"
16 | }
17 | ],
18 | "info" : {
19 | "version" : 1,
20 | "author" : "xcode"
21 | }
22 | }
--------------------------------------------------------------------------------
/JPGiftManager/Assets.xcassets/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "info" : {
3 | "version" : 1,
4 | "author" : "xcode"
5 | }
6 | }
--------------------------------------------------------------------------------
/JPGiftManager/Assets.xcassets/Live_Red_ccb.imageset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "universal",
5 | "scale" : "1x"
6 | },
7 | {
8 | "idiom" : "universal",
9 | "filename" : "Live_Red_ccb@2x.png",
10 | "scale" : "2x"
11 | },
12 | {
13 | "idiom" : "universal",
14 | "filename" : "Live_Red_ccb@3x.png",
15 | "scale" : "3x"
16 | }
17 | ],
18 | "info" : {
19 | "version" : 1,
20 | "author" : "xcode"
21 | }
22 | }
--------------------------------------------------------------------------------
/JPGiftManager/Assets.xcassets/Live_Red_ccb.imageset/Live_Red_ccb@2x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/baiyidjp/JPGiftManager/768830504fd7e51bba8104b15509236dfd2523f3/JPGiftManager/Assets.xcassets/Live_Red_ccb.imageset/Live_Red_ccb@2x.png
--------------------------------------------------------------------------------
/JPGiftManager/Assets.xcassets/Live_Red_ccb.imageset/Live_Red_ccb@3x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/baiyidjp/JPGiftManager/768830504fd7e51bba8104b15509236dfd2523f3/JPGiftManager/Assets.xcassets/Live_Red_ccb.imageset/Live_Red_ccb@3x.png
--------------------------------------------------------------------------------
/JPGiftManager/Base.lproj/LaunchScreen.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/JPGiftManager/Base.lproj/Main.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
32 |
44 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
--------------------------------------------------------------------------------
/JPGiftManager/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | NSAppTransportSecurity
6 |
7 | NSAllowsArbitraryLoads
8 |
9 |
10 | CFBundleDevelopmentRegion
11 | $(DEVELOPMENT_LANGUAGE)
12 | CFBundleExecutable
13 | $(EXECUTABLE_NAME)
14 | CFBundleIdentifier
15 | $(PRODUCT_BUNDLE_IDENTIFIER)
16 | CFBundleInfoDictionaryVersion
17 | 6.0
18 | CFBundleName
19 | $(PRODUCT_NAME)
20 | CFBundlePackageType
21 | APPL
22 | CFBundleShortVersionString
23 | 1.0
24 | CFBundleVersion
25 | 1
26 | LSRequiresIPhoneOS
27 |
28 | UILaunchStoryboardName
29 | LaunchScreen
30 | UIMainStoryboardFile
31 | Main
32 | UIRequiredDeviceCapabilities
33 |
34 | armv7
35 |
36 | UISupportedInterfaceOrientations
37 |
38 | UIInterfaceOrientationPortrait
39 | UIInterfaceOrientationLandscapeLeft
40 | UIInterfaceOrientationLandscapeRight
41 |
42 | UISupportedInterfaceOrientations~ipad
43 |
44 | UIInterfaceOrientationPortrait
45 | UIInterfaceOrientationPortraitUpsideDown
46 | UIInterfaceOrientationLandscapeLeft
47 | UIInterfaceOrientationLandscapeRight
48 |
49 |
50 |
51 |
--------------------------------------------------------------------------------
/JPGiftManager/JPGiftManager/JPGiftCellModel.h:
--------------------------------------------------------------------------------
1 | //
2 | // JPGiftCellModel.h 礼物选择页面的Model
3 | // JPGiftManager
4 | //
5 | // Created by Keep丶Dream on 2018/3/13.
6 | // Copyright © 2018年 dong. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface JPGiftCellModel : NSObject
12 |
13 | /** id */
14 | @property(nonatomic,copy)NSString *id;
15 | /** icon */
16 | @property(nonatomic,copy)NSString *icon;
17 | /** icon_gif */
18 | @property(nonatomic,copy)NSString *icon_gif;
19 | /** name */
20 | @property(nonatomic,copy)NSString *name;
21 | /** type */
22 | @property(nonatomic,copy)NSString *type;
23 | /** 价格 */
24 | @property(nonatomic,copy)NSString *value;
25 | /** 是否选中 */
26 | @property(nonatomic,assign)BOOL isSelected;
27 | /** username */
28 | @property(nonatomic,copy)NSString *username;
29 | /** cost_type 0星星 1测测币 */
30 | @property(nonatomic,copy)NSString *cost_type;
31 |
32 | @end
33 |
--------------------------------------------------------------------------------
/JPGiftManager/JPGiftManager/JPGiftCellModel.m:
--------------------------------------------------------------------------------
1 | //
2 | // JPGiftCellModel.m
3 | // JPGiftManager
4 | //
5 | // Created by Keep丶Dream on 2018/3/13.
6 | // Copyright © 2018年 dong. All rights reserved.
7 | //
8 |
9 | #import "JPGiftCellModel.h"
10 |
11 | @implementation JPGiftCellModel
12 |
13 | @end
14 |
--------------------------------------------------------------------------------
/JPGiftManager/JPGiftManager/JPGiftCollectionViewCell.h:
--------------------------------------------------------------------------------
1 | //
2 | // JPGiftCollectionViewCell.h 礼物选择页面--展示礼物测cell
3 | // JPGiftManager
4 | //
5 | // Created by Keep丶Dream on 2018/3/13.
6 | // Copyright © 2018年 dong. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @class JPGiftCellModel;
12 | @interface JPGiftCollectionViewCell : UICollectionViewCell
13 |
14 | /** model */
15 | @property(nonatomic,strong) JPGiftCellModel *model;
16 |
17 |
18 | @end
19 |
--------------------------------------------------------------------------------
/JPGiftManager/JPGiftManager/JPGiftCollectionViewCell.m:
--------------------------------------------------------------------------------
1 | //
2 | // JPGiftCollectionViewCell.m
3 | // JPGiftManager
4 | //
5 | // Created by Keep丶Dream on 2018/3/13.
6 | // Copyright © 2018年 dong. All rights reserved.
7 | //
8 |
9 | #import "JPGiftCollectionViewCell.h"
10 | #import "JPGiftCellModel.h"
11 | #import "UIImageView+WebCache.h"
12 |
13 | @interface JPGiftCollectionViewCell()
14 | /** bg */
15 | @property(nonatomic,strong) UIView *bgView;
16 | /** image */
17 | @property(nonatomic,strong) UIImageView *giftImageView;
18 | /** name */
19 | @property(nonatomic,strong) UILabel *giftNameLabel;
20 | /** money */
21 | @property(nonatomic,strong) UILabel *moneyLabel;
22 | /** moneyicon */
23 | @property(nonatomic,strong) UIImageView *moneyImage;
24 |
25 | @end
26 |
27 | @implementation JPGiftCollectionViewCell
28 |
29 | - (instancetype)initWithFrame:(CGRect)frame
30 | {
31 | self = [super initWithFrame:frame];
32 | if (self) {
33 |
34 | [self p_SetUI];
35 | }
36 | return self;
37 | }
38 |
39 | #pragma mark -设置UI
40 | - (void)p_SetUI {
41 |
42 | self.bgView = [[UIView alloc] initWithFrame:self.bounds];
43 | self.bgView.backgroundColor = [UIColor clearColor];
44 | [self.contentView addSubview:self.bgView];
45 |
46 | self.giftImageView = [[UIImageView alloc] initWithFrame:CGRectMake((self.bounds.size.width-70)*0.5, 11, 70, 55)];
47 | [self.contentView addSubview:self.giftImageView];
48 |
49 | self.giftNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.giftImageView.frame), self.bounds.size.width, 16)];
50 | self.giftNameLabel.text = @"礼物名";
51 | self.giftNameLabel.textColor = [UIColor whiteColor];
52 | self.giftNameLabel.textAlignment = NSTextAlignmentCenter;
53 | self.giftNameLabel.font = [UIFont systemFontOfSize:12];
54 | [self.contentView addSubview:self.giftNameLabel];
55 |
56 | UILabel *moneyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.giftNameLabel.frame), 30, 16)];
57 | moneyLabel.textColor = [UIColor whiteColor];
58 | moneyLabel.font = [UIFont systemFontOfSize:12];
59 | moneyLabel.textAlignment = NSTextAlignmentCenter;
60 | self.moneyLabel = moneyLabel;
61 | [self.contentView addSubview:moneyLabel];
62 |
63 | UIImageView *moneyImage = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMinX(moneyLabel.frame)-4, moneyLabel.frame.origin.y+4, 10, 10)];
64 | self.moneyImage = moneyImage;
65 | [self.contentView addSubview:moneyImage];
66 | }
67 |
68 | - (void)setModel:(JPGiftCellModel *)model {
69 |
70 | _model = model;
71 | [self.giftImageView sd_setImageWithURL:[NSURL URLWithString:model.icon] placeholderImage:[UIImage imageNamed:@""]];
72 | self.giftNameLabel.text = model.name;
73 | self.bgView.backgroundColor = model.isSelected ? [UIColor orangeColor] : [UIColor clearColor];
74 | BOOL isCcb = [model.cost_type boolValue];
75 | UIImage *img = [UIImage imageNamed:isCcb ? @"Live_Red_ccb" : @"Cece_live_star_small"];
76 | self.moneyImage.image = img;
77 |
78 | NSString *moneyValue = [NSString stringWithFormat:@"%zd",[model.value integerValue]/100];
79 | self.moneyLabel.text = moneyValue;
80 |
81 | CGSize size = [moneyValue sizeWithAttributes:@{NSFontAttributeName:self.moneyLabel.font}];
82 | CGFloat w = size.width+1;
83 | CGFloat labelX = (self.contentView.bounds.size.width-w+4+10)*0.5;
84 | self.moneyLabel.frame = CGRectMake(labelX, CGRectGetMaxY(self.giftNameLabel.frame), w, 16);
85 | CGFloat imageX = CGRectGetMinX(self.moneyLabel.frame)-4-10;
86 | self.moneyImage.frame = CGRectMake(imageX, self.moneyLabel.frame.origin.y+4, 10, 10);
87 | }
88 |
89 | @end
90 |
--------------------------------------------------------------------------------
/JPGiftManager/JPGiftManager/JPGiftCountLabel.h:
--------------------------------------------------------------------------------
1 | //
2 | // JPGiftCountLabel.h 文字描边
3 | // JPGiftManager
4 | //
5 | // Created by Keep丶Dream on 2018/3/13.
6 | // Copyright © 2018年 dong. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface JPGiftCountLabel : UILabel
12 |
13 | @end
14 |
--------------------------------------------------------------------------------
/JPGiftManager/JPGiftManager/JPGiftCountLabel.m:
--------------------------------------------------------------------------------
1 | //
2 | // JPGiftCountLabel.m
3 | // JPGiftManager
4 | //
5 | // Created by Keep丶Dream on 2018/3/13.
6 | // Copyright © 2018年 dong. All rights reserved.
7 | //
8 |
9 | #import "JPGiftCountLabel.h"
10 |
11 | @implementation JPGiftCountLabel
12 |
13 | - (void)drawTextInRect:(CGRect)rect {
14 |
15 | CGSize shadowOffset = self.shadowOffset;
16 | UIColor *textColor = self.textColor;
17 |
18 | CGContextRef c = UIGraphicsGetCurrentContext();
19 | CGContextSetLineWidth(c, 5);
20 | CGContextSetLineJoin(c, kCGLineJoinRound);
21 |
22 | CGContextSetTextDrawingMode(c, kCGTextStroke);
23 | self.textColor = [UIColor orangeColor];
24 | [super drawTextInRect:rect];
25 |
26 | CGContextSetTextDrawingMode(c, kCGTextFill);
27 | self.textColor = textColor;
28 | self.shadowOffset = CGSizeMake(0, 0);
29 | [super drawTextInRect:rect];
30 |
31 | self.shadowOffset = shadowOffset;
32 |
33 | }
34 |
35 | @end
36 |
--------------------------------------------------------------------------------
/JPGiftManager/JPGiftManager/JPGiftModel.h:
--------------------------------------------------------------------------------
1 | //
2 | // JPGiftModel.h 发送的礼物的Model
3 | // JPGiftManager
4 | //
5 | // Created by Keep丶Dream on 2018/3/13.
6 | // Copyright © 2018年 dong. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface JPGiftModel : NSObject
12 |
13 | /** usericon */
14 | @property(nonatomic,copy)NSString *userIcon;
15 | /** username */
16 | @property(nonatomic,copy)NSString *userName;
17 | /** giftname */
18 | @property(nonatomic,copy)NSString *giftName;
19 | /** giftimage */
20 | @property(nonatomic,copy)NSString *giftImage;
21 | /** gift gifimage */
22 | @property(nonatomic,copy)NSString *giftGifImage;
23 | /** count */
24 | @property(nonatomic,assign) NSInteger defaultCount; //0
25 | /** 发送的数 */
26 | @property(nonatomic,assign) NSInteger sendCount;
27 | /** 礼物ID */
28 | @property(nonatomic,copy)NSString *giftId;
29 | /** 礼物操作的唯一Key */
30 | @property(nonatomic,copy)NSString *giftKey;
31 | @end
32 |
--------------------------------------------------------------------------------
/JPGiftManager/JPGiftManager/JPGiftModel.m:
--------------------------------------------------------------------------------
1 | //
2 | // JPGiftModel.m
3 | // JPGiftManager
4 | //
5 | // Created by Keep丶Dream on 2018/3/13.
6 | // Copyright © 2018年 dong. All rights reserved.
7 | //
8 |
9 | #import "JPGiftModel.h"
10 |
11 | @implementation JPGiftModel
12 |
13 | - (NSString *)giftKey {
14 |
15 | return [NSString stringWithFormat:@"%@%@",self.giftName,self.giftId];
16 | }
17 |
18 | @end
19 |
--------------------------------------------------------------------------------
/JPGiftManager/JPGiftManager/JPGiftOperation.h:
--------------------------------------------------------------------------------
1 | //
2 | // JPGiftOperation.h 送礼物的操作
3 | // JPGiftManager
4 | //
5 | // Created by Keep丶Dream on 2018/3/13.
6 | // Copyright © 2018年 dong. All rights reserved.
7 | //
8 |
9 | #import
10 | #import
11 |
12 | @class JPGiftModel,JPGiftShowView;
13 |
14 | typedef void(^completeOpBlock)(BOOL finished,NSString *giftKey);
15 |
16 | @interface JPGiftOperation : NSOperation
17 |
18 | /**
19 | 增加一个操作
20 |
21 | @param giftShowView 礼物显示的View
22 | @param backView 礼物要显示在的父view
23 | @param model 礼物的数据
24 | @param completeBlock 回调操作结束
25 | @return 操作
26 | */
27 | + (instancetype)addOperationWithView:(JPGiftShowView *)giftShowView
28 | OnView:(UIView *)backView
29 | Info:(JPGiftModel *)model
30 | completeBlock:(completeOpBlock)completeBlock;
31 |
32 |
33 | /** 礼物展示的父view */
34 | @property(nonatomic,strong) UIView *backView;
35 | /** ext */
36 | @property(nonatomic,strong) JPGiftModel *model;
37 | /** block */
38 | @property(nonatomic,copy) completeOpBlock opFinishedBlock;
39 | /** showview */
40 | @property(nonatomic,strong) JPGiftShowView *giftShowView;
41 |
42 | @end
43 |
--------------------------------------------------------------------------------
/JPGiftManager/JPGiftManager/JPGiftOperation.m:
--------------------------------------------------------------------------------
1 | //
2 | // JPGiftOperation.m
3 | // JPGiftManager
4 | //
5 | // Created by Keep丶Dream on 2018/3/13.
6 | // Copyright © 2018年 dong. All rights reserved.
7 | //
8 |
9 | #import "JPGiftOperation.h"
10 | #import "JPGiftShowView.h"
11 | #import "JPGiftModel.h"
12 |
13 |
14 | @implementation JPGiftOperation
15 |
16 | @synthesize finished = _finished;
17 | @synthesize executing = _executing;
18 |
19 | - (instancetype)init
20 | {
21 | self = [super init];
22 | if (self) {
23 |
24 | _executing = NO;
25 | _finished = NO;
26 | }
27 | return self;
28 | }
29 |
30 | + (instancetype)addOperationWithView:(JPGiftShowView *)giftShowView OnView:(UIView *)backView Info:(JPGiftModel *)model completeBlock:(completeOpBlock)completeBlock {
31 |
32 | JPGiftOperation *op = [[JPGiftOperation alloc] init];
33 | op.giftShowView = giftShowView;
34 | op.model = model;
35 | op.backView = backView;
36 | op.opFinishedBlock = completeBlock;
37 | return op;
38 |
39 | }
40 |
41 | - (void)start {
42 |
43 | if ([self isCancelled]) {
44 | _finished = YES;
45 | return;
46 | }
47 |
48 | _executing = YES;
49 | NSLog(@"当前队列-- %@",self.model.giftName);
50 | [[NSOperationQueue mainQueue] addOperationWithBlock:^{
51 |
52 | [self.backView addSubview:self.giftShowView];
53 | [self.giftShowView showGiftShowViewWithModel:self.model completeBlock:^(BOOL finished,NSString *giftKey) {
54 | self.finished = finished;
55 | if (self.opFinishedBlock) {
56 | self.opFinishedBlock(finished,giftKey);
57 | }
58 | }];
59 | }];
60 |
61 | }
62 |
63 | #pragma mark - 手动触发 KVO
64 | - (void)setExecuting:(BOOL)executing
65 | {
66 | [self willChangeValueForKey:@"isExecuting"];
67 | _executing = executing;
68 | [self didChangeValueForKey:@"isExecuting"];
69 | }
70 |
71 | - (void)setFinished:(BOOL)finished
72 | {
73 | [self willChangeValueForKey:@"isFinished"];
74 | _finished = finished;
75 | [self didChangeValueForKey:@"isFinished"];
76 | }
77 |
78 |
79 | @end
80 |
--------------------------------------------------------------------------------
/JPGiftManager/JPGiftManager/JPGiftShowManager.h:
--------------------------------------------------------------------------------
1 | //
2 | // JPGiftShowManager.h 送礼物逻辑的管理
3 | // JPGiftManager
4 | //
5 | // Created by Keep丶Dream on 2018/3/14.
6 | // Copyright © 2018年 dong. All rights reserved.
7 | //
8 |
9 | #import
10 | #import
11 |
12 | @class JPGiftModel;
13 | typedef void(^completeBlock)(BOOL finished);
14 | typedef void(^completeShowGifImageBlock)(JPGiftModel *giftModel);
15 |
16 | @interface JPGiftShowManager : NSObject
17 |
18 | + (instancetype)sharedManager;
19 |
20 | /**
21 | 送礼物(不处理第一次展示当前礼物逻辑)
22 |
23 | @param backView 礼物动效展示父view
24 | @param giftModel 礼物的数据
25 | @param completeBlock 展示完毕回调
26 | */
27 |
28 | - (void)showGiftViewWithBackView:(UIView *)backView
29 | info:(JPGiftModel *)giftModel
30 | completeBlock:(completeBlock)completeBlock;
31 |
32 | /**
33 | 送礼物(回调第一次展示当前礼物的逻辑)
34 |
35 | @param backView 礼物动效展示父view
36 | @param giftModel 礼物的数据
37 | @param completeBlock 展示完毕回调
38 | @param completeShowGifImageBlock 第一次展示当前礼物的回调(为了显示gif)
39 | */
40 | - (void)showGiftViewWithBackView:(UIView *)backView
41 | info:(JPGiftModel *)giftModel
42 | completeBlock:(completeBlock)completeBlock
43 | completeShowGifImageBlock:(completeShowGifImageBlock)completeShowGifImageBlock;
44 |
45 | @end
46 |
--------------------------------------------------------------------------------
/JPGiftManager/JPGiftManager/JPGiftShowView.h:
--------------------------------------------------------------------------------
1 | //
2 | // JPGiftShowView.h 礼物的展示view
3 | // JPGiftManager
4 | //
5 | // Created by Keep丶Dream on 2018/3/13.
6 | // Copyright © 2018年 dong. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "JPGiftCountLabel.h"
11 |
12 | @class JPGiftModel;
13 | typedef void(^completeShowViewBlock)(BOOL finished,NSString *giftKey);
14 |
15 | typedef void(^completeShowViewKeyBlock)(JPGiftModel *giftModel);
16 |
17 | static const CGFloat showGiftView_UserIcon_WH = 44; //头像宽高
18 | static const CGFloat showGiftView_UserName_W = 60;//名字宽 -
19 | static const CGFloat showGiftView_UserName_H = 16;//名字高 -
20 | static const CGFloat showGiftView_UserName_L = 2;//名字左
21 | static const CGFloat showGiftView_GiftIcon_H = 50;//图片高
22 | static const CGFloat showGiftView_GiftIcon_W = showGiftView_GiftIcon_H*(70/55.0);//图片宽
23 | static const CGFloat showGiftView_UserIcon_LT = (showGiftView_GiftIcon_H-showGiftView_UserIcon_WH)*0.5;//头像距离上/左
24 | static const CGFloat showGiftView_XNum_W = 50;//礼物数宽
25 | static const CGFloat showGiftView_XNum_H = 30;//礼物数高
26 | static const CGFloat showGiftView_XNum_L = 5;//礼物数左
27 |
28 |
29 | @interface JPGiftShowView : UIView
30 |
31 | /**
32 | 展示礼物动效
33 |
34 | @param giftModel 礼物的数据
35 | @param completeBlock 展示完毕回调
36 | */
37 | - (void)showGiftShowViewWithModel:(JPGiftModel *)giftModel
38 | completeBlock:(completeShowViewBlock)completeBlock;
39 |
40 | /**
41 | 隐藏礼物
42 | */
43 | - (void)hiddenGiftShowView;
44 |
45 | /** 背景 */
46 | @property(nonatomic,strong) UIView *bgView;
47 | /** icon */
48 | @property(nonatomic,strong) UIImageView *userIconView;
49 | /** name */
50 | @property(nonatomic,strong) UILabel *userNameLabel;
51 | /** giftName */
52 | @property(nonatomic,strong) UILabel *giftNameLabel;
53 | /** giftImage */
54 | @property(nonatomic,strong) UIImageView *giftImageView;
55 | /** count */
56 | @property(nonatomic,strong) JPGiftCountLabel *countLabel;
57 | /** 礼物数 */
58 | @property(nonatomic,assign) NSInteger giftCount;
59 | /** 当前礼物总数 */
60 | @property(nonatomic,assign) NSInteger currentGiftCount;
61 | /** block */
62 | @property(nonatomic,copy)completeShowViewBlock showViewFinishBlock;
63 | /** 返回当前礼物的唯一key */
64 | @property(nonatomic,copy)completeShowViewKeyBlock showViewKeyBlock;
65 | /** model */
66 | @property(nonatomic,strong) JPGiftModel *finishModel;
67 |
68 | @end
69 |
--------------------------------------------------------------------------------
/JPGiftManager/JPGiftManager/JPGiftShowView.m:
--------------------------------------------------------------------------------
1 | //
2 | // JPGiftShowView.m
3 | // JPGiftManager
4 | //
5 | // Created by Keep丶Dream on 2018/3/13.
6 | // Copyright © 2018年 dong. All rights reserved.
7 | //
8 |
9 | #import "JPGiftShowView.h"
10 | #import "JPGiftModel.h"
11 | #import "UIImageView+WebCache.h"
12 |
13 | static const NSInteger animationTime = 5;
14 |
15 | @interface JPGiftShowView()
16 |
17 | @end
18 |
19 | @implementation JPGiftShowView
20 |
21 | - (instancetype)init
22 | {
23 | self = [super init];
24 | if (self) {
25 |
26 | }
27 | return self;
28 | }
29 |
30 | - (instancetype)initWithFrame:(CGRect)frame
31 | {
32 | self = [super initWithFrame:frame];
33 | if (self) {
34 | //245 50
35 | self.backgroundColor = [UIColor clearColor];
36 | self.hidden = YES;
37 | [self p_SetUI];
38 | }
39 | return self;
40 | }
41 |
42 | #pragma mark -设置UI
43 | - (void)p_SetUI {
44 |
45 | self.bgView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, self.frame.size.width-showGiftView_XNum_W-showGiftView_XNum_L, showGiftView_GiftIcon_H)];
46 | self.bgView.backgroundColor = [UIColor blackColor];
47 | self.bgView.layer.cornerRadius = showGiftView_GiftIcon_H*0.5;
48 | [self addSubview:self.bgView];
49 |
50 | self.userIconView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.bgView.frame)+showGiftView_UserIcon_LT, showGiftView_UserIcon_LT, showGiftView_UserIcon_WH, showGiftView_UserIcon_WH)];
51 | self.userIconView.layer.cornerRadius = showGiftView_UserIcon_WH*0.5;
52 | self.userIconView.layer.masksToBounds = YES;
53 | [self addSubview:self.userIconView];
54 |
55 | self.userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.userIconView.frame)+showGiftView_UserName_L, (showGiftView_GiftIcon_H-2*showGiftView_UserName_H-5)*0.5, showGiftView_UserName_W, showGiftView_UserName_H)];
56 | self.userNameLabel.text = @"董江鹏";
57 | self.userNameLabel.textColor = [UIColor whiteColor];
58 | self.userNameLabel.font = [UIFont systemFontOfSize:11];
59 | [self addSubview:self.userNameLabel];
60 |
61 | self.giftNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.userNameLabel.frame),CGRectGetMaxY(self.userNameLabel.frame)+5, showGiftView_UserName_W*2, showGiftView_UserName_H)];
62 | self.giftNameLabel.text = @"礼物";
63 | self.giftNameLabel.textColor = [UIColor colorWithRed:255/255.0 green:214/255.0 blue:84/255.0 alpha:1];
64 | self.giftNameLabel.font = [UIFont systemFontOfSize:12];
65 | [self addSubview:self.giftNameLabel];
66 |
67 | self.giftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.userNameLabel.frame), 0, showGiftView_GiftIcon_W, showGiftView_GiftIcon_H)];
68 | [self addSubview:self.giftImageView];
69 |
70 | self.countLabel = [[JPGiftCountLabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.giftImageView.frame)+showGiftView_XNum_L, (showGiftView_GiftIcon_H-showGiftView_XNum_H)*0.5, showGiftView_XNum_W, showGiftView_XNum_H)];
71 | self.countLabel.textColor = [UIColor whiteColor];
72 | self.countLabel.font = [UIFont systemFontOfSize:20];
73 | self.countLabel.textAlignment = NSTextAlignmentCenter;
74 | self.countLabel.text = @"";
75 | [self addSubview:self.countLabel];
76 |
77 | }
78 |
79 | - (void)showGiftShowViewWithModel:(JPGiftModel *)giftModel completeBlock:(completeShowViewBlock)completeBlock{
80 |
81 | self.finishModel = giftModel;
82 | [self.userIconView sd_setImageWithURL:[NSURL URLWithString:giftModel.userIcon] placeholderImage:[UIImage imageNamed:@""]];
83 | self.userNameLabel.text = giftModel.userName;
84 | self.giftNameLabel.text = [NSString stringWithFormat:@"送 %@",giftModel.giftName];
85 | [self.giftImageView sd_setImageWithURL:[NSURL URLWithString:giftModel.giftImage] placeholderImage:[UIImage imageNamed:@""]];
86 | self.hidden = NO;
87 | self.showViewFinishBlock = completeBlock;
88 | NSLog(@"当前展示的礼物--%@",giftModel.giftName);
89 | if (self.showViewKeyBlock && self.currentGiftCount == 0) {
90 | self.showViewKeyBlock(giftModel);
91 | }
92 | [UIView animateWithDuration:0.3 animations:^{
93 | self.frame =CGRectMake(0, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
94 | } completion:^(BOOL finished) {
95 |
96 | self.currentGiftCount = 0;
97 | [self setGiftCount:giftModel.defaultCount];
98 |
99 | }];}
100 |
101 | - (void)hiddenGiftShowView {
102 |
103 | [UIView animateWithDuration:0.3 animations:^{
104 | self.frame =CGRectMake(-self.frame.size.width, self.frame.origin.y-50, self.frame.size.width, self.frame.size.height);
105 | } completion:^(BOOL finished) {
106 |
107 | if (self.showViewFinishBlock) {
108 | self.showViewFinishBlock(YES, self.finishModel.giftKey);
109 | self.finishModel = nil;
110 | }
111 | self.frame =CGRectMake(-self.frame.size.width, self.frame.origin.y+50, self.frame.size.width, self.frame.size.height);
112 |
113 | self.hidden = YES;
114 | self.currentGiftCount = 0;
115 | self.countLabel.text = @"";
116 | }];
117 | }
118 |
119 | - (void)setGiftCount:(NSInteger)giftCount {
120 |
121 | _giftCount = giftCount;
122 | self.currentGiftCount += giftCount;
123 | self.countLabel.text = [NSString stringWithFormat:@"x %zd",self.currentGiftCount];
124 | NSLog(@"累计礼物数 %zd",self.currentGiftCount);
125 | if (self.currentGiftCount > 1) {
126 | [self p_SetAnimation:self.countLabel];
127 | [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hiddenGiftShowView) object:nil];//可以取消成功。
128 | [self performSelector:@selector(hiddenGiftShowView) withObject:nil afterDelay:animationTime];
129 |
130 | }else {
131 | [self performSelector:@selector(hiddenGiftShowView) withObject:nil afterDelay:animationTime];
132 | }
133 | }
134 |
135 | - (void)p_SetAnimation:(UIView *)view {
136 |
137 | CABasicAnimation*pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
138 | pulse.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
139 | pulse.duration = 0.08;
140 | pulse.repeatCount= 1;
141 | pulse.autoreverses= YES;
142 | pulse.fromValue= [NSNumber numberWithFloat:1.0];
143 | pulse.toValue= [NSNumber numberWithFloat:1.5];
144 | [[view layer] addAnimation:pulse forKey:nil];
145 | }
146 |
147 |
148 | @end
149 |
--------------------------------------------------------------------------------
/JPGiftManager/JPGiftManager/JPGiftView.h:
--------------------------------------------------------------------------------
1 | //
2 | // JPGiftView.h 礼物选择页面
3 | // JPGiftManager
4 | //
5 | // Created by Keep丶Dream on 2018/3/13.
6 | // Copyright © 2018年 dong. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @class JPGiftView,JPGiftCellModel;
12 | @protocol JPGiftViewDelegate
13 |
14 | /**
15 | 赠送礼物
16 |
17 | @param giftView 礼物的选择的view
18 | @param model 礼物展示的数据
19 | */
20 | - (void)giftViewSendGiftInView:(JPGiftView *)giftView data:(JPGiftCellModel *)model;
21 |
22 | /**
23 | 充值
24 |
25 | @param giftView view
26 | */
27 | - (void)giftViewGetMoneyInView:(JPGiftView *)giftView;
28 | @end
29 |
30 |
31 | @interface JPGiftView : UIView
32 |
33 | /** data */
34 | @property(nonatomic,strong) NSArray *dataArray;
35 |
36 | - (void)showGiftView;
37 |
38 | - (void)hiddenGiftView;
39 |
40 | @property(nonatomic,weak)id delegate;
41 |
42 | @end
43 |
--------------------------------------------------------------------------------
/JPGiftManager/JPGiftManager/JPGiftView.m:
--------------------------------------------------------------------------------
1 | //
2 | // JPGiftView.m
3 | // JPGiftManager
4 | //
5 | // Created by Keep丶Dream on 2018/3/13.
6 | // Copyright © 2018年 dong. All rights reserved.
7 | //
8 |
9 | #import "JPGiftView.h"
10 | #import "JPGiftCollectionViewCell.h"
11 | #import "JPGiftCellModel.h"
12 | #import "JPHorizontalLayout.h"
13 |
14 | //获取屏幕 宽度、高度
15 | #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
16 | #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
17 |
18 | // 判断是否是iPhone X
19 | #define iPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)
20 | // 状态栏高度
21 | #define STATUS_BAR_HEIGHT (iPhoneX ? 44.f : 20.f)
22 | // 导航栏高度
23 | #define Nav_Bar_HEIGHT (iPhoneX ? 88.f : 64.f)
24 | // 导航+状态
25 | #define Nav_Status_Height (STATUS_BAR_HEIGHT+Nav_Bar_HEIGHT)
26 | // tabBar高度
27 | #define TAB_BAR_HEIGHT (iPhoneX ? (49.f+34.f) : 49.f)
28 | // home indicator
29 | #define HOME_INDICATOR_HEIGHT (iPhoneX ? 34.f : 0.f)
30 | //距离底部的间距
31 | #define Bottom_Margin(margin) ((margin)+HOME_INDICATOR_HEIGHT)
32 |
33 | static NSString *cellID = @"JPGiftCollectionViewCell";
34 |
35 | @interface JPGiftView()
36 | /** 底部功能栏 */
37 | @property(nonatomic,strong) UIView *bottomView;
38 | /** 礼物显示 */
39 | @property(nonatomic,strong) UICollectionView *collectionView;
40 | /** ccb余额 */
41 | @property(nonatomic,strong) UILabel *ccbLabel;
42 | /** 上一次点击的model */
43 | @property(nonatomic,strong) JPGiftCellModel *preModel;
44 | /** pagecontro */
45 | @property(nonatomic,strong) UIPageControl *pageControl;
46 | /** money */
47 | @property(nonatomic,strong) UILabel *moneyLabel;
48 |
49 | @end
50 |
51 | @implementation JPGiftView
52 |
53 |
54 | - (instancetype)init
55 | {
56 | self = [super init];
57 | if (self) {
58 |
59 | self.backgroundColor = [UIColor clearColor];
60 | self.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
61 |
62 | [self p_SetUI];
63 | }
64 | return self;
65 | }
66 |
67 |
68 | #pragma mark -设置UI
69 | - (void)p_SetUI {
70 |
71 | UIView *bottomView = [[UIView alloc] initWithFrame: CGRectMake(0, self.frame.size.height-Bottom_Margin(44), self.frame.size.width, Bottom_Margin(44))];
72 | bottomView.backgroundColor = [UIColor blackColor];
73 | [self addSubview:bottomView];
74 | self.bottomView = bottomView;
75 |
76 | self.pageControl = [[UIPageControl alloc]initWithFrame: CGRectMake(CGRectGetWidth(bottomView.frame)*0.5-15, 0, 30, CGRectGetHeight(bottomView.frame))];
77 | self.pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
78 | self.pageControl.pageIndicatorTintColor = [UIColor grayColor];
79 | self.pageControl.hidden = YES;
80 | [bottomView addSubview:self.pageControl];
81 |
82 |
83 | UIButton *getMoneyBtn = [[UIButton alloc] initWithFrame:CGRectMake(5, 12, 30, 20)];
84 | [getMoneyBtn setTitle:@"充值" forState:UIControlStateNormal];
85 | [getMoneyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
86 | [getMoneyBtn setBackgroundColor:[UIColor purpleColor]];
87 | [getMoneyBtn.titleLabel setFont:[UIFont systemFontOfSize:12]];
88 | [getMoneyBtn addTarget:self action:@selector(p_ClickGetMoneyBtn) forControlEvents:UIControlEventTouchUpInside];
89 | [bottomView addSubview:getMoneyBtn];
90 |
91 | UIImageView *ccbImage = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(getMoneyBtn.frame)+5, CGRectGetMinY(getMoneyBtn.frame), 20, 20)];
92 | ccbImage.image = [UIImage imageNamed:@"Live_Red_ccb"];
93 | [bottomView addSubview:ccbImage];
94 |
95 | UILabel *moneyLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(ccbImage.frame)+5, CGRectGetMinY(ccbImage.frame), 100, 20)];
96 | moneyLabel.text = @"999";
97 | moneyLabel.textColor = [UIColor whiteColor];
98 | moneyLabel.font = [UIFont systemFontOfSize:13];
99 | [bottomView addSubview:moneyLabel];
100 | self.moneyLabel = moneyLabel;
101 |
102 | UIButton *sendBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.bottomView.frame.size.width-60, 2, 60, 40)];
103 | [sendBtn setBackgroundColor:[UIColor orangeColor]];
104 | [sendBtn setTitle:@"发送" forState:UIControlStateNormal];
105 | [sendBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
106 | [sendBtn addTarget:self action:@selector(p_ClickSendBtn) forControlEvents:UIControlEventTouchUpInside];
107 | [bottomView addSubview:sendBtn];
108 |
109 | //110*125
110 | CGFloat itemW = SCREEN_WIDTH/4.0;
111 | CGFloat itemH = itemW*125/110.0;
112 | JPHorizontalLayout *layout = [[JPHorizontalLayout alloc] init];
113 |
114 | UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, CGRectGetMinY(bottomView.frame)-1-2*itemH, SCREEN_WIDTH, 2*itemH) collectionViewLayout:layout];
115 | collectionView.delegate = self;
116 | collectionView.dataSource = self;
117 | collectionView.bounces = NO;
118 | [collectionView registerClass:[JPGiftCollectionViewCell class] forCellWithReuseIdentifier:cellID];
119 | collectionView.pagingEnabled = YES;
120 | [self addSubview:collectionView];
121 | self.collectionView = collectionView;
122 | }
123 |
124 | - (void)setDataArray:(NSArray *)dataArray {
125 |
126 | _dataArray = dataArray;
127 |
128 | self.pageControl.numberOfPages = (dataArray.count-1)/8+1;
129 | self.pageControl.currentPage = 0;
130 | self.pageControl.hidden = !((dataArray.count-1)/8);
131 |
132 | [self.collectionView reloadData];
133 | }
134 |
135 | - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
136 |
137 | return self.dataArray.count;
138 | }
139 |
140 | - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
141 |
142 | JPGiftCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
143 |
144 | if (indexPath.item < self.dataArray.count) {
145 | JPGiftCellModel *model = self.dataArray[indexPath.item];
146 | cell.model = model;
147 | }
148 |
149 | return cell;
150 | }
151 |
152 | - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
153 |
154 | if (indexPath.item < self.dataArray.count) {
155 | JPGiftCellModel *model = self.dataArray[indexPath.item];
156 | model.isSelected = !model.isSelected;
157 | if ([self.preModel isEqual:model]) {
158 | [collectionView reloadData];
159 | }else {
160 | self.preModel.isSelected = NO;
161 | [UIView performWithoutAnimation:^{
162 | [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
163 | }];
164 |
165 | }
166 | self.preModel = model;
167 | }
168 |
169 | }
170 |
171 | - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
172 |
173 | CGFloat x = scrollView.contentOffset.x;
174 | self.pageControl.currentPage = x/SCREEN_WIDTH+0.5;
175 | }
176 |
177 | #pragma mark -发送
178 | - (void)p_ClickSendBtn {
179 |
180 | //找到已选中的礼物
181 | BOOL isBack = NO;
182 | for (JPGiftCellModel *model in self.dataArray) {
183 | if (model.isSelected) {
184 | isBack = YES;
185 | if ([self.delegate respondsToSelector:@selector(giftViewSendGiftInView:data:)]) {
186 | [self.delegate giftViewSendGiftInView:self data:model];
187 | }
188 | }
189 | }
190 | if (!isBack) {
191 | //提示选择礼物
192 | NSLog(@"没有选择礼物");
193 | }
194 |
195 | }
196 |
197 | #pragma mark -充值
198 | - (void)p_ClickGetMoneyBtn {
199 |
200 | if ([self.delegate respondsToSelector:@selector(giftViewGetMoneyInView:)]) {
201 | [self.delegate giftViewGetMoneyInView:self];
202 | }
203 | }
204 |
205 |
206 |
207 | - (void)showGiftView {
208 |
209 | UIWindow *window = [UIApplication sharedApplication].keyWindow;
210 | [window addSubview:self];
211 |
212 | [UIView animateWithDuration:0.3 animations:^{
213 | self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
214 | }];
215 |
216 | }
217 |
218 | - (void)hiddenGiftView {
219 |
220 | [UIView animateWithDuration:0.3 animations:^{
221 | self.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
222 | } completion:^(BOOL finished) {
223 | [self removeFromSuperview];
224 | }];
225 | }
226 |
227 | - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
228 |
229 | [self hiddenGiftView];
230 | }
231 |
232 | @end
233 |
--------------------------------------------------------------------------------
/JPGiftManager/JPGiftManager/JPHorizontalLayout.h:
--------------------------------------------------------------------------------
1 | //
2 | // JPHorizontalLayout.h
3 | // JPGiftManager 自定义排序规则
4 | //
5 | // Created by Keep丶Dream on 2018/3/15.
6 | // Copyright © 2018年 dong. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface JPHorizontalLayout : UICollectionViewFlowLayout
12 |
13 | @end
14 |
--------------------------------------------------------------------------------
/JPGiftManager/JPGiftManager/JPHorizontalLayout.m:
--------------------------------------------------------------------------------
1 | //
2 | // JPHorizontalLayout.m
3 | // JPGiftManager
4 | //
5 | // Created by Keep丶Dream on 2018/3/15.
6 | // Copyright © 2018年 dong. All rights reserved.
7 | //
8 |
9 | #import "JPHorizontalLayout.h"
10 |
11 | //获取屏幕 宽度、高度
12 | #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
13 | #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
14 |
15 | @interface JPHorizontalLayout()
16 | /** 存放cell全部布局属性 */
17 | @property(nonatomic,strong) NSMutableArray *cellAttributesArray;
18 |
19 | @end
20 |
21 | @implementation JPHorizontalLayout
22 |
23 | - (NSMutableArray *)cellAttributesArray{
24 |
25 | if (!_cellAttributesArray) {
26 |
27 | _cellAttributesArray = [NSMutableArray array];
28 | }
29 | return _cellAttributesArray;
30 | }
31 |
32 |
33 | - (void)prepareLayout {
34 |
35 | [super prepareLayout];
36 |
37 |
38 | CGFloat itemW = SCREEN_WIDTH/4.0;
39 | CGFloat itemH = itemW*105/93.8;
40 | self.itemSize = CGSizeMake(itemW, itemH);
41 | self.minimumLineSpacing = 0;
42 | self.minimumInteritemSpacing = 0;
43 | self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
44 |
45 | //刷新后清除所有已布局的属性 重新获取
46 | [self.cellAttributesArray removeAllObjects];
47 |
48 | NSInteger cellCount = [self.collectionView numberOfItemsInSection:0];
49 | for (NSInteger i = 0; i < cellCount; i++) {
50 |
51 | NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
52 | UICollectionViewLayoutAttributes *attibute = [self layoutAttributesForItemAtIndexPath:indexPath];
53 | NSInteger page = i / 8;//第几页
54 | NSInteger row = i % 4 + page*4;//第几列
55 | NSInteger col = i / 4 - page*2;//第几行
56 | attibute.frame = CGRectMake(row*itemW, col*itemH, itemW, itemH);
57 | [self.cellAttributesArray addObject:attibute];
58 | }
59 | }
60 |
61 | - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{
62 |
63 | return self.cellAttributesArray;
64 | }
65 |
66 | - (CGSize)collectionViewContentSize{
67 |
68 | NSInteger cellCount = [self.collectionView numberOfItemsInSection:0];
69 | NSInteger page = (cellCount-1) / 8 + 1;
70 | return CGSizeMake(SCREEN_WIDTH*page, 0);
71 | }
72 |
73 |
74 |
75 | @end
76 |
--------------------------------------------------------------------------------
/JPGiftManager/SDWebImage/MKAnnotationView+WebCache.h:
--------------------------------------------------------------------------------
1 | //
2 | // MKAnnotationView+WebCache.h
3 | // SDWebImage
4 | //
5 | // Created by Olivier Poitrey on 14/03/12.
6 | // Copyright (c) 2012 Dailymotion. All rights reserved.
7 | //
8 |
9 | #import "MapKit/MapKit.h"
10 | #import "SDWebImageManager.h"
11 |
12 | /**
13 | * Integrates SDWebImage async downloading and caching of remote images with MKAnnotationView.
14 | */
15 | @interface MKAnnotationView (WebCache)
16 |
17 | /**
18 | * Get the current image URL.
19 | *
20 | * Note that because of the limitations of categories this property can get out of sync
21 | * if you use sd_setImage: directly.
22 | */
23 | - (NSURL *)sd_imageURL;
24 |
25 | /**
26 | * Set the imageView `image` with an `url`.
27 | *
28 | * The download is asynchronous and cached.
29 | *
30 | * @param url The url for the image.
31 | */
32 | - (void)sd_setImageWithURL:(NSURL *)url;
33 |
34 | /**
35 | * Set the imageView `image` with an `url` and a placeholder.
36 | *
37 | * The download is asynchronous and cached.
38 | *
39 | * @param url The url for the image.
40 | * @param placeholder The image to be set initially, until the image request finishes.
41 | * @see sd_setImageWithURL:placeholderImage:options:
42 | */
43 | - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder;
44 |
45 | /**
46 | * Set the imageView `image` with an `url`, placeholder and custom options.
47 | *
48 | * The download is asynchronous and cached.
49 | *
50 | * @param url The url for the image.
51 | * @param placeholder The image to be set initially, until the image request finishes.
52 | * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
53 | */
54 |
55 | - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options;
56 |
57 | /**
58 | * Set the imageView `image` with an `url`.
59 | *
60 | * The download is asynchronous and cached.
61 | *
62 | * @param url The url for the image.
63 | * @param completedBlock A block called when operation has been completed. This block has no return value
64 | * and takes the requested UIImage as first parameter. In case of error the image parameter
65 | * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
66 | * indicating if the image was retrieved from the local cache or from the network.
67 | * The fourth parameter is the original image url.
68 | */
69 | - (void)sd_setImageWithURL:(NSURL *)url completed:(SDWebImageCompletionBlock)completedBlock;
70 |
71 | /**
72 | * Set the imageView `image` with an `url`, placeholder.
73 | *
74 | * The download is asynchronous and cached.
75 | *
76 | * @param url The url for the image.
77 | * @param placeholder The image to be set initially, until the image request finishes.
78 | * @param completedBlock A block called when operation has been completed. This block has no return value
79 | * and takes the requested UIImage as first parameter. In case of error the image parameter
80 | * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
81 | * indicating if the image was retrieved from the local cache or from the network.
82 | * The fourth parameter is the original image url.
83 | */
84 | - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock;
85 |
86 | /**
87 | * Set the imageView `image` with an `url`, placeholder and custom options.
88 | *
89 | * The download is asynchronous and cached.
90 | *
91 | * @param url The url for the image.
92 | * @param placeholder The image to be set initially, until the image request finishes.
93 | * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
94 | * @param completedBlock A block called when operation has been completed. This block has no return value
95 | * and takes the requested UIImage as first parameter. In case of error the image parameter
96 | * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
97 | * indicating if the image was retrieved from the local cache or from the network.
98 | * The fourth parameter is the original image url.
99 | */
100 | - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock;
101 |
102 | /**
103 | * Cancel the current download
104 | */
105 | - (void)sd_cancelCurrentImageLoad;
106 |
107 | @end
108 |
109 |
110 | @interface MKAnnotationView (WebCacheDeprecated)
111 |
112 | - (NSURL *)imageURL __deprecated_msg("Use `sd_imageURL`");
113 |
114 | - (void)setImageWithURL:(NSURL *)url __deprecated_msg("Method deprecated. Use `sd_setImageWithURL:`");
115 | - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder __deprecated_msg("Method deprecated. Use `sd_setImageWithURL:placeholderImage:`");
116 | - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options __deprecated_msg("Method deprecated. Use `sd_setImageWithURL:placeholderImage:options:`");
117 |
118 | - (void)setImageWithURL:(NSURL *)url completed:(SDWebImageCompletedBlock)completedBlock __deprecated_msg("Method deprecated. Use `sd_setImageWithURL:completed:`");
119 | - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletedBlock)completedBlock __deprecated_msg("Method deprecated. Use `sd_setImageWithURL:placeholderImage:completed:`");
120 | - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock __deprecated_msg("Method deprecated. Use `sd_setImageWithURL:placeholderImage:options:completed:`");
121 |
122 | - (void)cancelCurrentImageLoad __deprecated_msg("Use `sd_cancelCurrentImageLoad`");
123 |
124 | @end
125 |
--------------------------------------------------------------------------------
/JPGiftManager/SDWebImage/MKAnnotationView+WebCache.m:
--------------------------------------------------------------------------------
1 | //
2 | // MKAnnotationView+WebCache.m
3 | // SDWebImage
4 | //
5 | // Created by Olivier Poitrey on 14/03/12.
6 | // Copyright (c) 2012 Dailymotion. All rights reserved.
7 | //
8 |
9 | #import "MKAnnotationView+WebCache.h"
10 | #import "objc/runtime.h"
11 | #import "UIView+WebCacheOperation.h"
12 |
13 | static char imageURLKey;
14 |
15 | @implementation MKAnnotationView (WebCache)
16 |
17 | - (NSURL *)sd_imageURL {
18 | return objc_getAssociatedObject(self, &imageURLKey);
19 | }
20 |
21 | - (void)sd_setImageWithURL:(NSURL *)url {
22 | [self sd_setImageWithURL:url placeholderImage:nil options:0 completed:nil];
23 | }
24 |
25 | - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder {
26 | [self sd_setImageWithURL:url placeholderImage:placeholder options:0 completed:nil];
27 | }
28 |
29 | - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options {
30 | [self sd_setImageWithURL:url placeholderImage:placeholder options:options completed:nil];
31 | }
32 |
33 | - (void)sd_setImageWithURL:(NSURL *)url completed:(SDWebImageCompletionBlock)completedBlock {
34 | [self sd_setImageWithURL:url placeholderImage:nil options:0 completed:completedBlock];
35 | }
36 |
37 | - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock {
38 | [self sd_setImageWithURL:url placeholderImage:placeholder options:0 completed:completedBlock];
39 | }
40 |
41 | - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock {
42 | [self sd_cancelCurrentImageLoad];
43 |
44 | objc_setAssociatedObject(self, &imageURLKey, url, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
45 | self.image = placeholder;
46 |
47 | if (url) {
48 | __weak __typeof(self)wself = self;
49 | id operation = [SDWebImageManager.sharedManager downloadImageWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
50 | if (!wself) return;
51 | dispatch_main_sync_safe(^{
52 | __strong MKAnnotationView *sself = wself;
53 | if (!sself) return;
54 | if (image) {
55 | sself.image = image;
56 | }
57 | if (completedBlock && finished) {
58 | completedBlock(image, error, cacheType, url);
59 | }
60 | });
61 | }];
62 | [self sd_setImageLoadOperation:operation forKey:@"MKAnnotationViewImage"];
63 | } else {
64 | dispatch_main_async_safe(^{
65 | NSError *error = [NSError errorWithDomain:SDWebImageErrorDomain code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}];
66 | if (completedBlock) {
67 | completedBlock(nil, error, SDImageCacheTypeNone, url);
68 | }
69 | });
70 | }
71 | }
72 |
73 | - (void)sd_cancelCurrentImageLoad {
74 | [self sd_cancelImageLoadOperationWithKey:@"MKAnnotationViewImage"];
75 | }
76 |
77 | @end
78 |
79 |
80 | @implementation MKAnnotationView (WebCacheDeprecated)
81 |
82 | - (NSURL *)imageURL {
83 | return [self sd_imageURL];
84 | }
85 |
86 | - (void)setImageWithURL:(NSURL *)url {
87 | [self sd_setImageWithURL:url placeholderImage:nil options:0 completed:nil];
88 | }
89 |
90 | - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder {
91 | [self sd_setImageWithURL:url placeholderImage:placeholder options:0 completed:nil];
92 | }
93 |
94 | - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options {
95 | [self sd_setImageWithURL:url placeholderImage:placeholder options:options completed:nil];
96 | }
97 |
98 | - (void)setImageWithURL:(NSURL *)url completed:(SDWebImageCompletedBlock)completedBlock {
99 | [self sd_setImageWithURL:url placeholderImage:nil options:0 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
100 | if (completedBlock) {
101 | completedBlock(image, error, cacheType);
102 | }
103 | }];
104 | }
105 |
106 | - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletedBlock)completedBlock {
107 | [self sd_setImageWithURL:url placeholderImage:placeholder options:0 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
108 | if (completedBlock) {
109 | completedBlock(image, error, cacheType);
110 | }
111 | }];
112 | }
113 |
114 | - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock {
115 | [self sd_setImageWithURL:url placeholderImage:placeholder options:options completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
116 | if (completedBlock) {
117 | completedBlock(image, error, cacheType);
118 | }
119 | }];
120 | }
121 |
122 | - (void)cancelCurrentImageLoad {
123 | [self sd_cancelCurrentImageLoad];
124 | }
125 |
126 | @end
127 |
--------------------------------------------------------------------------------
/JPGiftManager/SDWebImage/NSData+ImageContentType.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by Fabrice Aneche on 06/01/14.
3 | // Copyright (c) 2014 Dailymotion. All rights reserved.
4 | //
5 |
6 | #import
7 |
8 | @interface NSData (ImageContentType)
9 |
10 | /**
11 | * Compute the content type for an image data
12 | *
13 | * @param data the input data
14 | *
15 | * @return the content type as string (i.e. image/jpeg, image/gif)
16 | */
17 | + (NSString *)sd_contentTypeForImageData:(NSData *)data;
18 |
19 | @end
20 |
21 |
22 | @interface NSData (ImageContentTypeDeprecated)
23 |
24 | + (NSString *)contentTypeForImageData:(NSData *)data __deprecated_msg("Use `sd_contentTypeForImageData:`");
25 |
26 | @end
27 |
--------------------------------------------------------------------------------
/JPGiftManager/SDWebImage/NSData+ImageContentType.m:
--------------------------------------------------------------------------------
1 | //
2 | // Created by Fabrice Aneche on 06/01/14.
3 | // Copyright (c) 2014 Dailymotion. All rights reserved.
4 | //
5 |
6 | #import "NSData+ImageContentType.h"
7 |
8 |
9 | @implementation NSData (ImageContentType)
10 |
11 | + (NSString *)sd_contentTypeForImageData:(NSData *)data {
12 | uint8_t c;
13 | [data getBytes:&c length:1];
14 | switch (c) {
15 | case 0xFF:
16 | return @"image/jpeg";
17 | case 0x89:
18 | return @"image/png";
19 | case 0x47:
20 | return @"image/gif";
21 | case 0x49:
22 | case 0x4D:
23 | return @"image/tiff";
24 | case 0x52:
25 | // R as RIFF for WEBP
26 | if ([data length] < 12) {
27 | return nil;
28 | }
29 |
30 | NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, 12)] encoding:NSASCIIStringEncoding];
31 | if ([testString hasPrefix:@"RIFF"] && [testString hasSuffix:@"WEBP"]) {
32 | return @"image/webp";
33 | }
34 |
35 | return nil;
36 | }
37 | return nil;
38 | }
39 |
40 | @end
41 |
42 |
43 | @implementation NSData (ImageContentTypeDeprecated)
44 |
45 | + (NSString *)contentTypeForImageData:(NSData *)data {
46 | return [self sd_contentTypeForImageData:data];
47 | }
48 |
49 | @end
50 |
--------------------------------------------------------------------------------
/JPGiftManager/SDWebImage/SDWebImageCompat.h:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of the SDWebImage package.
3 | * (c) Olivier Poitrey
4 | * (c) Jamie Pinkham
5 | *
6 | * For the full copyright and license information, please view the LICENSE
7 | * file that was distributed with this source code.
8 | */
9 |
10 | #import
11 |
12 | #ifdef __OBJC_GC__
13 | #error SDWebImage does not support Objective-C Garbage Collection
14 | #endif
15 |
16 | #if __IPHONE_OS_VERSION_MIN_REQUIRED != 20000 && __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_5_0
17 | #error SDWebImage doesn't support Deployment Target version < 5.0
18 | #endif
19 |
20 | #if !TARGET_OS_IPHONE
21 | #import
22 | #ifndef UIImage
23 | #define UIImage NSImage
24 | #endif
25 | #ifndef UIImageView
26 | #define UIImageView NSImageView
27 | #endif
28 | #else
29 |
30 | #import
31 |
32 | #endif
33 |
34 | #ifndef NS_ENUM
35 | #define NS_ENUM(_type, _name) enum _name : _type _name; enum _name : _type
36 | #endif
37 |
38 | #ifndef NS_OPTIONS
39 | #define NS_OPTIONS(_type, _name) enum _name : _type _name; enum _name : _type
40 | #endif
41 |
42 | #if OS_OBJECT_USE_OBJC
43 | #undef SDDispatchQueueRelease
44 | #undef SDDispatchQueueSetterSementics
45 | #define SDDispatchQueueRelease(q)
46 | #define SDDispatchQueueSetterSementics strong
47 | #else
48 | #undef SDDispatchQueueRelease
49 | #undef SDDispatchQueueSetterSementics
50 | #define SDDispatchQueueRelease(q) (dispatch_release(q))
51 | #define SDDispatchQueueSetterSementics assign
52 | #endif
53 |
54 | extern UIImage *SDScaledImageForKey(NSString *key, UIImage *image);
55 |
56 | typedef void(^SDWebImageNoParamsBlock)();
57 |
58 | extern NSString *const SDWebImageErrorDomain;
59 |
60 | #define dispatch_main_sync_safe(block)\
61 | if ([NSThread isMainThread]) {\
62 | block();\
63 | } else {\
64 | dispatch_sync(dispatch_get_main_queue(), block);\
65 | }
66 |
67 | #define dispatch_main_async_safe(block)\
68 | if ([NSThread isMainThread]) {\
69 | block();\
70 | } else {\
71 | dispatch_async(dispatch_get_main_queue(), block);\
72 | }
73 |
--------------------------------------------------------------------------------
/JPGiftManager/SDWebImage/SDWebImageCompat.m:
--------------------------------------------------------------------------------
1 | //
2 | // SDWebImageCompat.m
3 | // SDWebImage
4 | //
5 | // Created by Olivier Poitrey on 11/12/12.
6 | // Copyright (c) 2012 Dailymotion. All rights reserved.
7 | //
8 |
9 | #import "SDWebImageCompat.h"
10 |
11 | #if !__has_feature(objc_arc)
12 | #error SDWebImage is ARC only. Either turn on ARC for the project or use -fobjc-arc flag
13 | #endif
14 |
15 | inline UIImage *SDScaledImageForKey(NSString *key, UIImage *image) {
16 | if (!image) {
17 | return nil;
18 | }
19 |
20 | if ([image.images count] > 0) {
21 | NSMutableArray *scaledImages = [NSMutableArray array];
22 |
23 | for (UIImage *tempImage in image.images) {
24 | [scaledImages addObject:SDScaledImageForKey(key, tempImage)];
25 | }
26 |
27 | return [UIImage animatedImageWithImages:scaledImages duration:image.duration];
28 | }
29 | else {
30 | if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
31 | CGFloat scale = [UIScreen mainScreen].scale;
32 | if (key.length >= 8) {
33 | NSRange range = [key rangeOfString:@"@2x."];
34 | if (range.location != NSNotFound) {
35 | scale = 2.0;
36 | }
37 |
38 | range = [key rangeOfString:@"@3x."];
39 | if (range.location != NSNotFound) {
40 | scale = 3.0;
41 | }
42 | }
43 |
44 | UIImage *scaledImage = [[UIImage alloc] initWithCGImage:image.CGImage scale:scale orientation:image.imageOrientation];
45 | image = scaledImage;
46 | }
47 | return image;
48 | }
49 | }
50 |
51 | NSString *const SDWebImageErrorDomain = @"SDWebImageErrorDomain";
52 |
--------------------------------------------------------------------------------
/JPGiftManager/SDWebImage/SDWebImageDecoder.h:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of the SDWebImage package.
3 | * (c) Olivier Poitrey
4 | *
5 | * Created by james on 9/28/11.
6 | *
7 | * For the full copyright and license information, please view the LICENSE
8 | * file that was distributed with this source code.
9 | */
10 |
11 | #import
12 | #import "SDWebImageCompat.h"
13 |
14 | @interface UIImage (ForceDecode)
15 |
16 | + (UIImage *)decodedImageWithImage:(UIImage *)image;
17 |
18 | @end
19 |
--------------------------------------------------------------------------------
/JPGiftManager/SDWebImage/SDWebImageDecoder.m:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of the SDWebImage package.
3 | * (c) Olivier Poitrey
4 | *
5 | * Created by james on 9/28/11.
6 | *
7 | * For the full copyright and license information, please view the LICENSE
8 | * file that was distributed with this source code.
9 | */
10 |
11 | #import "SDWebImageDecoder.h"
12 |
13 | @implementation UIImage (ForceDecode)
14 |
15 | + (UIImage *)decodedImageWithImage:(UIImage *)image {
16 | // while downloading huge amount of images
17 | // autorelease the bitmap context
18 | // and all vars to help system to free memory
19 | // when there are memory warning.
20 | // on iOS7, do not forget to call
21 | // [[SDImageCache sharedImageCache] clearMemory];
22 |
23 | if (image == nil) { // Prevent "CGBitmapContextCreateImage: invalid context 0x0" error
24 | return nil;
25 | }
26 |
27 | @autoreleasepool{
28 | // do not decode animated images
29 | if (image.images != nil) {
30 | return image;
31 | }
32 |
33 | CGImageRef imageRef = image.CGImage;
34 |
35 | CGImageAlphaInfo alpha = CGImageGetAlphaInfo(imageRef);
36 | BOOL anyAlpha = (alpha == kCGImageAlphaFirst ||
37 | alpha == kCGImageAlphaLast ||
38 | alpha == kCGImageAlphaPremultipliedFirst ||
39 | alpha == kCGImageAlphaPremultipliedLast);
40 | if (anyAlpha) {
41 | return image;
42 | }
43 |
44 | // current
45 | CGColorSpaceModel imageColorSpaceModel = CGColorSpaceGetModel(CGImageGetColorSpace(imageRef));
46 | CGColorSpaceRef colorspaceRef = CGImageGetColorSpace(imageRef);
47 |
48 | BOOL unsupportedColorSpace = (imageColorSpaceModel == kCGColorSpaceModelUnknown ||
49 | imageColorSpaceModel == kCGColorSpaceModelMonochrome ||
50 | imageColorSpaceModel == kCGColorSpaceModelCMYK ||
51 | imageColorSpaceModel == kCGColorSpaceModelIndexed);
52 | if (unsupportedColorSpace) {
53 | colorspaceRef = CGColorSpaceCreateDeviceRGB();
54 | }
55 |
56 | size_t width = CGImageGetWidth(imageRef);
57 | size_t height = CGImageGetHeight(imageRef);
58 | NSUInteger bytesPerPixel = 4;
59 | NSUInteger bytesPerRow = bytesPerPixel * width;
60 | NSUInteger bitsPerComponent = 8;
61 |
62 |
63 | // kCGImageAlphaNone is not supported in CGBitmapContextCreate.
64 | // Since the original image here has no alpha info, use kCGImageAlphaNoneSkipLast
65 | // to create bitmap graphics contexts without alpha info.
66 | CGContextRef context = CGBitmapContextCreate(NULL,
67 | width,
68 | height,
69 | bitsPerComponent,
70 | bytesPerRow,
71 | colorspaceRef,
72 | kCGBitmapByteOrderDefault|kCGImageAlphaNoneSkipLast);
73 |
74 | // Draw the image into the context and retrieve the new bitmap image without alpha
75 | CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
76 | CGImageRef imageRefWithoutAlpha = CGBitmapContextCreateImage(context);
77 | UIImage *imageWithoutAlpha = [UIImage imageWithCGImage:imageRefWithoutAlpha
78 | scale:image.scale
79 | orientation:image.imageOrientation];
80 |
81 | if (unsupportedColorSpace) {
82 | CGColorSpaceRelease(colorspaceRef);
83 | }
84 |
85 | CGContextRelease(context);
86 | CGImageRelease(imageRefWithoutAlpha);
87 |
88 | return imageWithoutAlpha;
89 | }
90 | }
91 |
92 | @end
93 |
--------------------------------------------------------------------------------
/JPGiftManager/SDWebImage/SDWebImageDownloader.h:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of the SDWebImage package.
3 | * (c) Olivier Poitrey
4 | *
5 | * For the full copyright and license information, please view the LICENSE
6 | * file that was distributed with this source code.
7 | */
8 |
9 | #import
10 | #import "SDWebImageCompat.h"
11 | #import "SDWebImageOperation.h"
12 |
13 | typedef NS_OPTIONS(NSUInteger, SDWebImageDownloaderOptions) {
14 | SDWebImageDownloaderLowPriority = 1 << 0,
15 | SDWebImageDownloaderProgressiveDownload = 1 << 1,
16 |
17 | /**
18 | * By default, request prevent the use of NSURLCache. With this flag, NSURLCache
19 | * is used with default policies.
20 | */
21 | SDWebImageDownloaderUseNSURLCache = 1 << 2,
22 |
23 | /**
24 | * Call completion block with nil image/imageData if the image was read from NSURLCache
25 | * (to be combined with `SDWebImageDownloaderUseNSURLCache`).
26 | */
27 |
28 | SDWebImageDownloaderIgnoreCachedResponse = 1 << 3,
29 | /**
30 | * In iOS 4+, continue the download of the image if the app goes to background. This is achieved by asking the system for
31 | * extra time in background to let the request finish. If the background task expires the operation will be cancelled.
32 | */
33 |
34 | SDWebImageDownloaderContinueInBackground = 1 << 4,
35 |
36 | /**
37 | * Handles cookies stored in NSHTTPCookieStore by setting
38 | * NSMutableURLRequest.HTTPShouldHandleCookies = YES;
39 | */
40 | SDWebImageDownloaderHandleCookies = 1 << 5,
41 |
42 | /**
43 | * Enable to allow untrusted SSL certificates.
44 | * Useful for testing purposes. Use with caution in production.
45 | */
46 | SDWebImageDownloaderAllowInvalidSSLCertificates = 1 << 6,
47 |
48 | /**
49 | * Put the image in the high priority queue.
50 | */
51 | SDWebImageDownloaderHighPriority = 1 << 7,
52 | };
53 |
54 | typedef NS_ENUM(NSInteger, SDWebImageDownloaderExecutionOrder) {
55 | /**
56 | * Default value. All download operations will execute in queue style (first-in-first-out).
57 | */
58 | SDWebImageDownloaderFIFOExecutionOrder,
59 |
60 | /**
61 | * All download operations will execute in stack style (last-in-first-out).
62 | */
63 | SDWebImageDownloaderLIFOExecutionOrder
64 | };
65 |
66 | extern NSString *const SDWebImageDownloadStartNotification;
67 | extern NSString *const SDWebImageDownloadStopNotification;
68 |
69 | typedef void(^SDWebImageDownloaderProgressBlock)(NSInteger receivedSize, NSInteger expectedSize);
70 |
71 | typedef void(^SDWebImageDownloaderCompletedBlock)(UIImage *image, NSData *data, NSError *error, BOOL finished);
72 |
73 | typedef NSDictionary *(^SDWebImageDownloaderHeadersFilterBlock)(NSURL *url, NSDictionary *headers);
74 |
75 | /**
76 | * Asynchronous downloader dedicated and optimized for image loading.
77 | */
78 | @interface SDWebImageDownloader : NSObject
79 |
80 | /**
81 | * Decompressing images that are downloaded and cached can improve performance but can consume lot of memory.
82 | * Defaults to YES. Set this to NO if you are experiencing a crash due to excessive memory consumption.
83 | */
84 | @property (assign, nonatomic) BOOL shouldDecompressImages;
85 |
86 | @property (assign, nonatomic) NSInteger maxConcurrentDownloads;
87 |
88 | /**
89 | * Shows the current amount of downloads that still need to be downloaded
90 | */
91 | @property (readonly, nonatomic) NSUInteger currentDownloadCount;
92 |
93 |
94 | /**
95 | * The timeout value (in seconds) for the download operation. Default: 15.0.
96 | */
97 | @property (assign, nonatomic) NSTimeInterval downloadTimeout;
98 |
99 |
100 | /**
101 | * Changes download operations execution order. Default value is `SDWebImageDownloaderFIFOExecutionOrder`.
102 | */
103 | @property (assign, nonatomic) SDWebImageDownloaderExecutionOrder executionOrder;
104 |
105 | /**
106 | * Singleton method, returns the shared instance
107 | *
108 | * @return global shared instance of downloader class
109 | */
110 | + (SDWebImageDownloader *)sharedDownloader;
111 |
112 | /**
113 | * Set the default URL credential to be set for request operations.
114 | */
115 | @property (strong, nonatomic) NSURLCredential *urlCredential;
116 |
117 | /**
118 | * Set username
119 | */
120 | @property (strong, nonatomic) NSString *username;
121 |
122 | /**
123 | * Set password
124 | */
125 | @property (strong, nonatomic) NSString *password;
126 |
127 | /**
128 | * Set filter to pick headers for downloading image HTTP request.
129 | *
130 | * This block will be invoked for each downloading image request, returned
131 | * NSDictionary will be used as headers in corresponding HTTP request.
132 | */
133 | @property (nonatomic, copy) SDWebImageDownloaderHeadersFilterBlock headersFilter;
134 |
135 | /**
136 | * Set a value for a HTTP header to be appended to each download HTTP request.
137 | *
138 | * @param value The value for the header field. Use `nil` value to remove the header.
139 | * @param field The name of the header field to set.
140 | */
141 | - (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field;
142 |
143 | /**
144 | * Returns the value of the specified HTTP header field.
145 | *
146 | * @return The value associated with the header field field, or `nil` if there is no corresponding header field.
147 | */
148 | - (NSString *)valueForHTTPHeaderField:(NSString *)field;
149 |
150 | /**
151 | * Sets a subclass of `SDWebImageDownloaderOperation` as the default
152 | * `NSOperation` to be used each time SDWebImage constructs a request
153 | * operation to download an image.
154 | *
155 | * @param operationClass The subclass of `SDWebImageDownloaderOperation` to set
156 | * as default. Passing `nil` will revert to `SDWebImageDownloaderOperation`.
157 | */
158 | - (void)setOperationClass:(Class)operationClass;
159 |
160 | /**
161 | * Creates a SDWebImageDownloader async downloader instance with a given URL
162 | *
163 | * The delegate will be informed when the image is finish downloaded or an error has happen.
164 | *
165 | * @see SDWebImageDownloaderDelegate
166 | *
167 | * @param url The URL to the image to download
168 | * @param options The options to be used for this download
169 | * @param progressBlock A block called repeatedly while the image is downloading
170 | * @param completedBlock A block called once the download is completed.
171 | * If the download succeeded, the image parameter is set, in case of error,
172 | * error parameter is set with the error. The last parameter is always YES
173 | * if SDWebImageDownloaderProgressiveDownload isn't use. With the
174 | * SDWebImageDownloaderProgressiveDownload option, this block is called
175 | * repeatedly with the partial image object and the finished argument set to NO
176 | * before to be called a last time with the full image and finished argument
177 | * set to YES. In case of error, the finished argument is always YES.
178 | *
179 | * @return A cancellable SDWebImageOperation
180 | */
181 | - (id )downloadImageWithURL:(NSURL *)url
182 | options:(SDWebImageDownloaderOptions)options
183 | progress:(SDWebImageDownloaderProgressBlock)progressBlock
184 | completed:(SDWebImageDownloaderCompletedBlock)completedBlock;
185 |
186 | /**
187 | * Sets the download queue suspension state
188 | */
189 | - (void)setSuspended:(BOOL)suspended;
190 |
191 | @end
192 |
--------------------------------------------------------------------------------
/JPGiftManager/SDWebImage/SDWebImageDownloaderOperation.h:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of the SDWebImage package.
3 | * (c) Olivier Poitrey
4 | *
5 | * For the full copyright and license information, please view the LICENSE
6 | * file that was distributed with this source code.
7 | */
8 |
9 | #import
10 | #import "SDWebImageDownloader.h"
11 | #import "SDWebImageOperation.h"
12 |
13 | extern NSString *const SDWebImageDownloadStartNotification;
14 | extern NSString *const SDWebImageDownloadReceiveResponseNotification;
15 | extern NSString *const SDWebImageDownloadStopNotification;
16 | extern NSString *const SDWebImageDownloadFinishNotification;
17 |
18 | @interface SDWebImageDownloaderOperation : NSOperation
19 |
20 | /**
21 | * The request used by the operation's connection.
22 | */
23 | @property (strong, nonatomic, readonly) NSURLRequest *request;
24 |
25 |
26 | @property (assign, nonatomic) BOOL shouldDecompressImages;
27 |
28 | /**
29 | * Whether the URL connection should consult the credential storage for authenticating the connection. `YES` by default.
30 | *
31 | * This is the value that is returned in the `NSURLConnectionDelegate` method `-connectionShouldUseCredentialStorage:`.
32 | */
33 | @property (nonatomic, assign) BOOL shouldUseCredentialStorage;
34 |
35 | /**
36 | * The credential used for authentication challenges in `-connection:didReceiveAuthenticationChallenge:`.
37 | *
38 | * This will be overridden by any shared credentials that exist for the username or password of the request URL, if present.
39 | */
40 | @property (nonatomic, strong) NSURLCredential *credential;
41 |
42 | /**
43 | * The SDWebImageDownloaderOptions for the receiver.
44 | */
45 | @property (assign, nonatomic, readonly) SDWebImageDownloaderOptions options;
46 |
47 | /**
48 | * The expected size of data.
49 | */
50 | @property (assign, nonatomic) NSInteger expectedSize;
51 |
52 | /**
53 | * The response returned by the operation's connection.
54 | */
55 | @property (strong, nonatomic) NSURLResponse *response;
56 |
57 | /**
58 | * Initializes a `SDWebImageDownloaderOperation` object
59 | *
60 | * @see SDWebImageDownloaderOperation
61 | *
62 | * @param request the URL request
63 | * @param options downloader options
64 | * @param progressBlock the block executed when a new chunk of data arrives.
65 | * @note the progress block is executed on a background queue
66 | * @param completedBlock the block executed when the download is done.
67 | * @note the completed block is executed on the main queue for success. If errors are found, there is a chance the block will be executed on a background queue
68 | * @param cancelBlock the block executed if the download (operation) is cancelled
69 | *
70 | * @return the initialized instance
71 | */
72 | - (id)initWithRequest:(NSURLRequest *)request
73 | options:(SDWebImageDownloaderOptions)options
74 | progress:(SDWebImageDownloaderProgressBlock)progressBlock
75 | completed:(SDWebImageDownloaderCompletedBlock)completedBlock
76 | cancelled:(SDWebImageNoParamsBlock)cancelBlock;
77 |
78 | @end
79 |
--------------------------------------------------------------------------------
/JPGiftManager/SDWebImage/SDWebImageOperation.h:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of the SDWebImage package.
3 | * (c) Olivier Poitrey
4 | *
5 | * For the full copyright and license information, please view the LICENSE
6 | * file that was distributed with this source code.
7 | */
8 |
9 | #import
10 |
11 | @protocol SDWebImageOperation
12 |
13 | - (void)cancel;
14 |
15 | @end
16 |
--------------------------------------------------------------------------------
/JPGiftManager/SDWebImage/SDWebImagePrefetcher.h:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of the SDWebImage package.
3 | * (c) Olivier Poitrey
4 | *
5 | * For the full copyright and license information, please view the LICENSE
6 | * file that was distributed with this source code.
7 | */
8 |
9 | #import
10 | #import "SDWebImageManager.h"
11 |
12 | @class SDWebImagePrefetcher;
13 |
14 | @protocol SDWebImagePrefetcherDelegate
15 |
16 | @optional
17 |
18 | /**
19 | * Called when an image was prefetched.
20 | *
21 | * @param imagePrefetcher The current image prefetcher
22 | * @param imageURL The image url that was prefetched
23 | * @param finishedCount The total number of images that were prefetched (successful or not)
24 | * @param totalCount The total number of images that were to be prefetched
25 | */
26 | - (void)imagePrefetcher:(SDWebImagePrefetcher *)imagePrefetcher didPrefetchURL:(NSURL *)imageURL finishedCount:(NSUInteger)finishedCount totalCount:(NSUInteger)totalCount;
27 |
28 | /**
29 | * Called when all images are prefetched.
30 | * @param imagePrefetcher The current image prefetcher
31 | * @param totalCount The total number of images that were prefetched (whether successful or not)
32 | * @param skippedCount The total number of images that were skipped
33 | */
34 | - (void)imagePrefetcher:(SDWebImagePrefetcher *)imagePrefetcher didFinishWithTotalCount:(NSUInteger)totalCount skippedCount:(NSUInteger)skippedCount;
35 |
36 | @end
37 |
38 | typedef void(^SDWebImagePrefetcherProgressBlock)(NSUInteger noOfFinishedUrls, NSUInteger noOfTotalUrls);
39 | typedef void(^SDWebImagePrefetcherCompletionBlock)(NSUInteger noOfFinishedUrls, NSUInteger noOfSkippedUrls);
40 |
41 | /**
42 | * Prefetch some URLs in the cache for future use. Images are downloaded in low priority.
43 | */
44 | @interface SDWebImagePrefetcher : NSObject
45 |
46 | /**
47 | * The web image manager
48 | */
49 | @property (strong, nonatomic, readonly) SDWebImageManager *manager;
50 |
51 | /**
52 | * Maximum number of URLs to prefetch at the same time. Defaults to 3.
53 | */
54 | @property (nonatomic, assign) NSUInteger maxConcurrentDownloads;
55 |
56 | /**
57 | * SDWebImageOptions for prefetcher. Defaults to SDWebImageLowPriority.
58 | */
59 | @property (nonatomic, assign) SDWebImageOptions options;
60 |
61 | /**
62 | * Queue options for Prefetcher. Defaults to Main Queue.
63 | */
64 | @property (nonatomic, assign) dispatch_queue_t prefetcherQueue;
65 |
66 | @property (weak, nonatomic) id delegate;
67 |
68 | /**
69 | * Return the global image prefetcher instance.
70 | */
71 | + (SDWebImagePrefetcher *)sharedImagePrefetcher;
72 |
73 | /**
74 | * Allows you to instantiate a prefetcher with any arbitrary image manager.
75 | */
76 | - (id)initWithImageManager:(SDWebImageManager *)manager;
77 |
78 | /**
79 | * Assign list of URLs to let SDWebImagePrefetcher to queue the prefetching,
80 | * currently one image is downloaded at a time,
81 | * and skips images for failed downloads and proceed to the next image in the list
82 | *
83 | * @param urls list of URLs to prefetch
84 | */
85 | - (void)prefetchURLs:(NSArray *)urls;
86 |
87 | /**
88 | * Assign list of URLs to let SDWebImagePrefetcher to queue the prefetching,
89 | * currently one image is downloaded at a time,
90 | * and skips images for failed downloads and proceed to the next image in the list
91 | *
92 | * @param urls list of URLs to prefetch
93 | * @param progressBlock block to be called when progress updates;
94 | * first parameter is the number of completed (successful or not) requests,
95 | * second parameter is the total number of images originally requested to be prefetched
96 | * @param completionBlock block to be called when prefetching is completed
97 | * first param is the number of completed (successful or not) requests,
98 | * second parameter is the number of skipped requests
99 | */
100 | - (void)prefetchURLs:(NSArray *)urls progress:(SDWebImagePrefetcherProgressBlock)progressBlock completed:(SDWebImagePrefetcherCompletionBlock)completionBlock;
101 |
102 | /**
103 | * Remove and cancel queued list
104 | */
105 | - (void)cancelPrefetching;
106 |
107 |
108 | @end
109 |
--------------------------------------------------------------------------------
/JPGiftManager/SDWebImage/SDWebImagePrefetcher.m:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of the SDWebImage package.
3 | * (c) Olivier Poitrey
4 | *
5 | * For the full copyright and license information, please view the LICENSE
6 | * file that was distributed with this source code.
7 | */
8 |
9 | #import "SDWebImagePrefetcher.h"
10 |
11 | @interface SDWebImagePrefetcher ()
12 |
13 | @property (strong, nonatomic) SDWebImageManager *manager;
14 | @property (strong, nonatomic) NSArray *prefetchURLs;
15 | @property (assign, nonatomic) NSUInteger requestedCount;
16 | @property (assign, nonatomic) NSUInteger skippedCount;
17 | @property (assign, nonatomic) NSUInteger finishedCount;
18 | @property (assign, nonatomic) NSTimeInterval startedTime;
19 | @property (copy, nonatomic) SDWebImagePrefetcherCompletionBlock completionBlock;
20 | @property (copy, nonatomic) SDWebImagePrefetcherProgressBlock progressBlock;
21 |
22 | @end
23 |
24 | @implementation SDWebImagePrefetcher
25 |
26 | + (SDWebImagePrefetcher *)sharedImagePrefetcher {
27 | static dispatch_once_t once;
28 | static id instance;
29 | dispatch_once(&once, ^{
30 | instance = [self new];
31 | });
32 | return instance;
33 | }
34 |
35 | - (id)init {
36 | return [self initWithImageManager:[SDWebImageManager new]];
37 | }
38 |
39 | - (id)initWithImageManager:(SDWebImageManager *)manager {
40 | if ((self = [super init])) {
41 | _manager = manager;
42 | _options = SDWebImageLowPriority;
43 | _prefetcherQueue = dispatch_get_main_queue();
44 | self.maxConcurrentDownloads = 3;
45 | }
46 | return self;
47 | }
48 |
49 | - (void)setMaxConcurrentDownloads:(NSUInteger)maxConcurrentDownloads {
50 | self.manager.imageDownloader.maxConcurrentDownloads = maxConcurrentDownloads;
51 | }
52 |
53 | - (NSUInteger)maxConcurrentDownloads {
54 | return self.manager.imageDownloader.maxConcurrentDownloads;
55 | }
56 |
57 | - (void)startPrefetchingAtIndex:(NSUInteger)index {
58 | if (index >= self.prefetchURLs.count) return;
59 | self.requestedCount++;
60 | [self.manager downloadImageWithURL:self.prefetchURLs[index] options:self.options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
61 | if (!finished) return;
62 | self.finishedCount++;
63 |
64 | if (image) {
65 | if (self.progressBlock) {
66 | self.progressBlock(self.finishedCount,[self.prefetchURLs count]);
67 | }
68 | }
69 | else {
70 | if (self.progressBlock) {
71 | self.progressBlock(self.finishedCount,[self.prefetchURLs count]);
72 | }
73 | // Add last failed
74 | self.skippedCount++;
75 | }
76 | if ([self.delegate respondsToSelector:@selector(imagePrefetcher:didPrefetchURL:finishedCount:totalCount:)]) {
77 | [self.delegate imagePrefetcher:self
78 | didPrefetchURL:self.prefetchURLs[index]
79 | finishedCount:self.finishedCount
80 | totalCount:self.prefetchURLs.count
81 | ];
82 | }
83 | if (self.prefetchURLs.count > self.requestedCount) {
84 | dispatch_async(self.prefetcherQueue, ^{
85 | [self startPrefetchingAtIndex:self.requestedCount];
86 | });
87 | } else if (self.finishedCount == self.requestedCount) {
88 | [self reportStatus];
89 | if (self.completionBlock) {
90 | self.completionBlock(self.finishedCount, self.skippedCount);
91 | self.completionBlock = nil;
92 | }
93 | self.progressBlock = nil;
94 | }
95 | }];
96 | }
97 |
98 | - (void)reportStatus {
99 | NSUInteger total = [self.prefetchURLs count];
100 | if ([self.delegate respondsToSelector:@selector(imagePrefetcher:didFinishWithTotalCount:skippedCount:)]) {
101 | [self.delegate imagePrefetcher:self
102 | didFinishWithTotalCount:(total - self.skippedCount)
103 | skippedCount:self.skippedCount
104 | ];
105 | }
106 | }
107 |
108 | - (void)prefetchURLs:(NSArray *)urls {
109 | [self prefetchURLs:urls progress:nil completed:nil];
110 | }
111 |
112 | - (void)prefetchURLs:(NSArray *)urls progress:(SDWebImagePrefetcherProgressBlock)progressBlock completed:(SDWebImagePrefetcherCompletionBlock)completionBlock {
113 | [self cancelPrefetching]; // Prevent duplicate prefetch request
114 | self.startedTime = CFAbsoluteTimeGetCurrent();
115 | self.prefetchURLs = urls;
116 | self.completionBlock = completionBlock;
117 | self.progressBlock = progressBlock;
118 |
119 | if (urls.count == 0) {
120 | if (completionBlock) {
121 | completionBlock(0,0);
122 | }
123 | } else {
124 | // Starts prefetching from the very first image on the list with the max allowed concurrency
125 | NSUInteger listCount = self.prefetchURLs.count;
126 | for (NSUInteger i = 0; i < self.maxConcurrentDownloads && self.requestedCount < listCount; i++) {
127 | [self startPrefetchingAtIndex:i];
128 | }
129 | }
130 | }
131 |
132 | - (void)cancelPrefetching {
133 | self.prefetchURLs = nil;
134 | self.skippedCount = 0;
135 | self.requestedCount = 0;
136 | self.finishedCount = 0;
137 | [self.manager cancelAll];
138 | }
139 |
140 | @end
141 |
--------------------------------------------------------------------------------
/JPGiftManager/SDWebImage/UIImage+GIF.h:
--------------------------------------------------------------------------------
1 | //
2 | // UIImage+GIF.h
3 | // LBGIFImage
4 | //
5 | // Created by Laurin Brandner on 06.01.12.
6 | // Copyright (c) 2012 __MyCompanyName__. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface UIImage (GIF)
12 |
13 | + (UIImage *)sd_animatedGIFNamed:(NSString *)name;
14 |
15 | + (UIImage *)sd_animatedGIFWithData:(NSData *)data;
16 |
17 | - (UIImage *)sd_animatedImageByScalingAndCroppingToSize:(CGSize)size;
18 |
19 | @end
20 |
--------------------------------------------------------------------------------
/JPGiftManager/SDWebImage/UIImage+GIF.m:
--------------------------------------------------------------------------------
1 | //
2 | // UIImage+GIF.m
3 | // LBGIFImage
4 | //
5 | // Created by Laurin Brandner on 06.01.12.
6 | // Copyright (c) 2012 __MyCompanyName__. All rights reserved.
7 | //
8 |
9 | #import "UIImage+GIF.h"
10 | #import
11 |
12 | @implementation UIImage (GIF)
13 |
14 | + (UIImage *)sd_animatedGIFWithData:(NSData *)data {
15 | if (!data) {
16 | return nil;
17 | }
18 |
19 | CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
20 |
21 | size_t count = CGImageSourceGetCount(source);
22 |
23 | UIImage *animatedImage;
24 |
25 | if (count <= 1) {
26 | animatedImage = [[UIImage alloc] initWithData:data];
27 | }
28 | else {
29 | NSMutableArray *images = [NSMutableArray array];
30 |
31 | NSTimeInterval duration = 0.0f;
32 |
33 | for (size_t i = 0; i < count; i++) {
34 | CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);
35 | if (!image) {
36 | continue;
37 | }
38 |
39 | duration += [self sd_frameDurationAtIndex:i source:source];
40 |
41 | [images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];
42 |
43 | CGImageRelease(image);
44 | }
45 |
46 | if (!duration) {
47 | duration = (1.0f / 10.0f) * count;
48 | }
49 |
50 | animatedImage = [UIImage animatedImageWithImages:images duration:duration];
51 | }
52 |
53 | CFRelease(source);
54 |
55 | return animatedImage;
56 | }
57 |
58 | + (float)sd_frameDurationAtIndex:(NSUInteger)index source:(CGImageSourceRef)source {
59 | float frameDuration = 0.1f;
60 | CFDictionaryRef cfFrameProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil);
61 | NSDictionary *frameProperties = (__bridge NSDictionary *)cfFrameProperties;
62 | NSDictionary *gifProperties = frameProperties[(NSString *)kCGImagePropertyGIFDictionary];
63 |
64 | NSNumber *delayTimeUnclampedProp = gifProperties[(NSString *)kCGImagePropertyGIFUnclampedDelayTime];
65 | if (delayTimeUnclampedProp) {
66 | frameDuration = [delayTimeUnclampedProp floatValue];
67 | }
68 | else {
69 |
70 | NSNumber *delayTimeProp = gifProperties[(NSString *)kCGImagePropertyGIFDelayTime];
71 | if (delayTimeProp) {
72 | frameDuration = [delayTimeProp floatValue];
73 | }
74 | }
75 |
76 | // Many annoying ads specify a 0 duration to make an image flash as quickly as possible.
77 | // We follow Firefox's behavior and use a duration of 100 ms for any frames that specify
78 | // a duration of <= 10 ms. See and
79 | // for more information.
80 |
81 | if (frameDuration < 0.011f) {
82 | frameDuration = 0.100f;
83 | }
84 |
85 | CFRelease(cfFrameProperties);
86 | return frameDuration;
87 | }
88 |
89 | + (UIImage *)sd_animatedGIFNamed:(NSString *)name {
90 | CGFloat scale = [UIScreen mainScreen].scale;
91 |
92 | if (scale > 1.0f) {
93 | NSString *retinaPath = [[NSBundle mainBundle] pathForResource:[name stringByAppendingString:@"@2x"] ofType:@"gif"];
94 |
95 | NSData *data = [NSData dataWithContentsOfFile:retinaPath];
96 |
97 | if (data) {
98 | return [UIImage sd_animatedGIFWithData:data];
99 | }
100 |
101 | NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"];
102 |
103 | data = [NSData dataWithContentsOfFile:path];
104 |
105 | if (data) {
106 | return [UIImage sd_animatedGIFWithData:data];
107 | }
108 |
109 | return [UIImage imageNamed:name];
110 | }
111 | else {
112 | NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"];
113 |
114 | NSData *data = [NSData dataWithContentsOfFile:path];
115 |
116 | if (data) {
117 | return [UIImage sd_animatedGIFWithData:data];
118 | }
119 |
120 | return [UIImage imageNamed:name];
121 | }
122 | }
123 |
124 | - (UIImage *)sd_animatedImageByScalingAndCroppingToSize:(CGSize)size {
125 | if (CGSizeEqualToSize(self.size, size) || CGSizeEqualToSize(size, CGSizeZero)) {
126 | return self;
127 | }
128 |
129 | CGSize scaledSize = size;
130 | CGPoint thumbnailPoint = CGPointZero;
131 |
132 | CGFloat widthFactor = size.width / self.size.width;
133 | CGFloat heightFactor = size.height / self.size.height;
134 | CGFloat scaleFactor = (widthFactor > heightFactor) ? widthFactor : heightFactor;
135 | scaledSize.width = self.size.width * scaleFactor;
136 | scaledSize.height = self.size.height * scaleFactor;
137 |
138 | if (widthFactor > heightFactor) {
139 | thumbnailPoint.y = (size.height - scaledSize.height) * 0.5;
140 | }
141 | else if (widthFactor < heightFactor) {
142 | thumbnailPoint.x = (size.width - scaledSize.width) * 0.5;
143 | }
144 |
145 | NSMutableArray *scaledImages = [NSMutableArray array];
146 |
147 | for (UIImage *image in self.images) {
148 | UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
149 |
150 | [image drawInRect:CGRectMake(thumbnailPoint.x, thumbnailPoint.y, scaledSize.width, scaledSize.height)];
151 | UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
152 |
153 | [scaledImages addObject:newImage];
154 |
155 | UIGraphicsEndImageContext();
156 | }
157 |
158 | return [UIImage animatedImageWithImages:scaledImages duration:self.duration];
159 | }
160 |
161 | @end
162 |
--------------------------------------------------------------------------------
/JPGiftManager/SDWebImage/UIImage+MultiFormat.h:
--------------------------------------------------------------------------------
1 | //
2 | // UIImage+MultiFormat.h
3 | // SDWebImage
4 | //
5 | // Created by Olivier Poitrey on 07/06/13.
6 | // Copyright (c) 2013 Dailymotion. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface UIImage (MultiFormat)
12 |
13 | + (UIImage *)sd_imageWithData:(NSData *)data;
14 |
15 | @end
16 |
--------------------------------------------------------------------------------
/JPGiftManager/SDWebImage/UIImage+MultiFormat.m:
--------------------------------------------------------------------------------
1 | //
2 | // UIImage+MultiFormat.m
3 | // SDWebImage
4 | //
5 | // Created by Olivier Poitrey on 07/06/13.
6 | // Copyright (c) 2013 Dailymotion. All rights reserved.
7 | //
8 |
9 | #import "UIImage+MultiFormat.h"
10 | #import "UIImage+GIF.h"
11 | #import "NSData+ImageContentType.h"
12 | #import
13 |
14 | #ifdef SD_WEBP
15 | #import "UIImage+WebP.h"
16 | #endif
17 |
18 | @implementation UIImage (MultiFormat)
19 |
20 | + (UIImage *)sd_imageWithData:(NSData *)data {
21 | if (!data) {
22 | return nil;
23 | }
24 |
25 | UIImage *image;
26 | NSString *imageContentType = [NSData sd_contentTypeForImageData:data];
27 | if ([imageContentType isEqualToString:@"image/gif"]) {
28 | image = [UIImage sd_animatedGIFWithData:data];
29 | }
30 | #ifdef SD_WEBP
31 | else if ([imageContentType isEqualToString:@"image/webp"])
32 | {
33 | image = [UIImage sd_imageWithWebPData:data];
34 | }
35 | #endif
36 | else {
37 | image = [[UIImage alloc] initWithData:data];
38 | UIImageOrientation orientation = [self sd_imageOrientationFromImageData:data];
39 | if (orientation != UIImageOrientationUp) {
40 | image = [UIImage imageWithCGImage:image.CGImage
41 | scale:image.scale
42 | orientation:orientation];
43 | }
44 | }
45 |
46 |
47 | return image;
48 | }
49 |
50 |
51 | +(UIImageOrientation)sd_imageOrientationFromImageData:(NSData *)imageData {
52 | UIImageOrientation result = UIImageOrientationUp;
53 | CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
54 | if (imageSource) {
55 | CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL);
56 | if (properties) {
57 | CFTypeRef val;
58 | int exifOrientation;
59 | val = CFDictionaryGetValue(properties, kCGImagePropertyOrientation);
60 | if (val) {
61 | CFNumberGetValue(val, kCFNumberIntType, &exifOrientation);
62 | result = [self sd_exifOrientationToiOSOrientation:exifOrientation];
63 | } // else - if it's not set it remains at up
64 | CFRelease((CFTypeRef) properties);
65 | } else {
66 | //NSLog(@"NO PROPERTIES, FAIL");
67 | }
68 | CFRelease(imageSource);
69 | }
70 | return result;
71 | }
72 |
73 | #pragma mark EXIF orientation tag converter
74 | // Convert an EXIF image orientation to an iOS one.
75 | // reference see here: http://sylvana.net/jpegcrop/exif_orientation.html
76 | + (UIImageOrientation) sd_exifOrientationToiOSOrientation:(int)exifOrientation {
77 | UIImageOrientation orientation = UIImageOrientationUp;
78 | switch (exifOrientation) {
79 | case 1:
80 | orientation = UIImageOrientationUp;
81 | break;
82 |
83 | case 3:
84 | orientation = UIImageOrientationDown;
85 | break;
86 |
87 | case 8:
88 | orientation = UIImageOrientationLeft;
89 | break;
90 |
91 | case 6:
92 | orientation = UIImageOrientationRight;
93 | break;
94 |
95 | case 2:
96 | orientation = UIImageOrientationUpMirrored;
97 | break;
98 |
99 | case 4:
100 | orientation = UIImageOrientationDownMirrored;
101 | break;
102 |
103 | case 5:
104 | orientation = UIImageOrientationLeftMirrored;
105 | break;
106 |
107 | case 7:
108 | orientation = UIImageOrientationRightMirrored;
109 | break;
110 | default:
111 | break;
112 | }
113 | return orientation;
114 | }
115 |
116 |
117 |
118 | @end
119 |
--------------------------------------------------------------------------------
/JPGiftManager/SDWebImage/UIImage+WebP.h:
--------------------------------------------------------------------------------
1 | //
2 | // UIImage+WebP.h
3 | // SDWebImage
4 | //
5 | // Created by Olivier Poitrey on 07/06/13.
6 | // Copyright (c) 2013 Dailymotion. All rights reserved.
7 | //
8 |
9 | #ifdef SD_WEBP
10 |
11 | #import
12 |
13 | // Fix for issue #416 Undefined symbols for architecture armv7 since WebP introduction when deploying to device
14 | void WebPInitPremultiplyNEON(void);
15 |
16 | void WebPInitUpsamplersNEON(void);
17 |
18 | void VP8DspInitNEON(void);
19 |
20 | @interface UIImage (WebP)
21 |
22 | + (UIImage *)sd_imageWithWebPData:(NSData *)data;
23 |
24 | @end
25 |
26 | #endif
27 |
--------------------------------------------------------------------------------
/JPGiftManager/SDWebImage/UIImage+WebP.m:
--------------------------------------------------------------------------------
1 | //
2 | // UIImage+WebP.m
3 | // SDWebImage
4 | //
5 | // Created by Olivier Poitrey on 07/06/13.
6 | // Copyright (c) 2013 Dailymotion. All rights reserved.
7 | //
8 |
9 | #ifdef SD_WEBP
10 | #import "UIImage+WebP.h"
11 |
12 | #if !COCOAPODS
13 | #import "webp/decode.h"
14 | #else
15 | #import "libwebp/webp/decode.h"
16 | #endif
17 |
18 | // Callback for CGDataProviderRelease
19 | static void FreeImageData(void *info, const void *data, size_t size)
20 | {
21 | free((void *)data);
22 | }
23 |
24 | @implementation UIImage (WebP)
25 |
26 | + (UIImage *)sd_imageWithWebPData:(NSData *)data {
27 | WebPDecoderConfig config;
28 | if (!WebPInitDecoderConfig(&config)) {
29 | return nil;
30 | }
31 |
32 | if (WebPGetFeatures(data.bytes, data.length, &config.input) != VP8_STATUS_OK) {
33 | return nil;
34 | }
35 |
36 | config.output.colorspace = config.input.has_alpha ? MODE_rgbA : MODE_RGB;
37 | config.options.use_threads = 1;
38 |
39 | // Decode the WebP image data into a RGBA value array.
40 | if (WebPDecode(data.bytes, data.length, &config) != VP8_STATUS_OK) {
41 | return nil;
42 | }
43 |
44 | int width = config.input.width;
45 | int height = config.input.height;
46 | if (config.options.use_scaling) {
47 | width = config.options.scaled_width;
48 | height = config.options.scaled_height;
49 | }
50 |
51 | // Construct a UIImage from the decoded RGBA value array.
52 | CGDataProviderRef provider =
53 | CGDataProviderCreateWithData(NULL, config.output.u.RGBA.rgba, config.output.u.RGBA.size, FreeImageData);
54 | CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
55 | CGBitmapInfo bitmapInfo = config.input.has_alpha ? kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast : 0;
56 | size_t components = config.input.has_alpha ? 4 : 3;
57 | CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
58 | CGImageRef imageRef = CGImageCreate(width, height, 8, components * 8, components * width, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
59 |
60 | CGColorSpaceRelease(colorSpaceRef);
61 | CGDataProviderRelease(provider);
62 |
63 | UIImage *image = [[UIImage alloc] initWithCGImage:imageRef];
64 | CGImageRelease(imageRef);
65 |
66 | return image;
67 | }
68 |
69 | @end
70 |
71 | #if !COCOAPODS
72 | // Functions to resolve some undefined symbols when using WebP and force_load flag
73 | void WebPInitPremultiplyNEON(void) {}
74 | void WebPInitUpsamplersNEON(void) {}
75 | void VP8DspInitNEON(void) {}
76 | #endif
77 |
78 | #endif
79 |
--------------------------------------------------------------------------------
/JPGiftManager/SDWebImage/UIImageView+HighlightedWebCache.h:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of the SDWebImage package.
3 | * (c) Olivier Poitrey
4 | *
5 | * For the full copyright and license information, please view the LICENSE
6 | * file that was distributed with this source code.
7 | */
8 |
9 | #import
10 | #import "SDWebImageCompat.h"
11 | #import "SDWebImageManager.h"
12 |
13 | /**
14 | * Integrates SDWebImage async downloading and caching of remote images with UIImageView for highlighted state.
15 | */
16 | @interface UIImageView (HighlightedWebCache)
17 |
18 | /**
19 | * Set the imageView `highlightedImage` with an `url`.
20 | *
21 | * The download is asynchronous and cached.
22 | *
23 | * @param url The url for the image.
24 | */
25 | - (void)sd_setHighlightedImageWithURL:(NSURL *)url;
26 |
27 | /**
28 | * Set the imageView `highlightedImage` with an `url` and custom options.
29 | *
30 | * The download is asynchronous and cached.
31 | *
32 | * @param url The url for the image.
33 | * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
34 | */
35 | - (void)sd_setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options;
36 |
37 | /**
38 | * Set the imageView `highlightedImage` with an `url`.
39 | *
40 | * The download is asynchronous and cached.
41 | *
42 | * @param url The url for the image.
43 | * @param completedBlock A block called when operation has been completed. This block has no return value
44 | * and takes the requested UIImage as first parameter. In case of error the image parameter
45 | * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
46 | * indicating if the image was retrieved from the local cache or from the network.
47 | * The fourth parameter is the original image url.
48 | */
49 | - (void)sd_setHighlightedImageWithURL:(NSURL *)url completed:(SDWebImageCompletionBlock)completedBlock;
50 |
51 | /**
52 | * Set the imageView `highlightedImage` with an `url` and custom options.
53 | *
54 | * The download is asynchronous and cached.
55 | *
56 | * @param url The url for the image.
57 | * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
58 | * @param completedBlock A block called when operation has been completed. This block has no return value
59 | * and takes the requested UIImage as first parameter. In case of error the image parameter
60 | * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
61 | * indicating if the image was retrieved from the local cache or from the network.
62 | * The fourth parameter is the original image url.
63 | */
64 | - (void)sd_setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock;
65 |
66 | /**
67 | * Set the imageView `highlightedImage` with an `url` and custom options.
68 | *
69 | * The download is asynchronous and cached.
70 | *
71 | * @param url The url for the image.
72 | * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
73 | * @param progressBlock A block called while image is downloading
74 | * @param completedBlock A block called when operation has been completed. This block has no return value
75 | * and takes the requested UIImage as first parameter. In case of error the image parameter
76 | * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
77 | * indicating if the image was retrieved from the local cache or from the network.
78 | * The fourth parameter is the original image url.
79 | */
80 | - (void)sd_setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock;
81 |
82 | /**
83 | * Cancel the current download
84 | */
85 | - (void)sd_cancelCurrentHighlightedImageLoad;
86 |
87 | @end
88 |
89 |
90 | @interface UIImageView (HighlightedWebCacheDeprecated)
91 |
92 | - (void)setHighlightedImageWithURL:(NSURL *)url __deprecated_msg("Method deprecated. Use `sd_setHighlightedImageWithURL:`");
93 | - (void)setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options __deprecated_msg("Method deprecated. Use `sd_setHighlightedImageWithURL:options:`");
94 | - (void)setHighlightedImageWithURL:(NSURL *)url completed:(SDWebImageCompletedBlock)completedBlock __deprecated_msg("Method deprecated. Use `sd_setHighlightedImageWithURL:completed:`");
95 | - (void)setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock __deprecated_msg("Method deprecated. Use `sd_setHighlightedImageWithURL:options:completed:`");
96 | - (void)setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock __deprecated_msg("Method deprecated. Use `sd_setHighlightedImageWithURL:options:progress:completed:`");
97 |
98 | - (void)cancelCurrentHighlightedImageLoad __deprecated_msg("Use `sd_cancelCurrentHighlightedImageLoad`");
99 |
100 | @end
101 |
--------------------------------------------------------------------------------
/JPGiftManager/SDWebImage/UIImageView+HighlightedWebCache.m:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of the SDWebImage package.
3 | * (c) Olivier Poitrey
4 | *
5 | * For the full copyright and license information, please view the LICENSE
6 | * file that was distributed with this source code.
7 | */
8 |
9 | #import "UIImageView+HighlightedWebCache.h"
10 | #import "UIView+WebCacheOperation.h"
11 |
12 | #define UIImageViewHighlightedWebCacheOperationKey @"highlightedImage"
13 |
14 | @implementation UIImageView (HighlightedWebCache)
15 |
16 | - (void)sd_setHighlightedImageWithURL:(NSURL *)url {
17 | [self sd_setHighlightedImageWithURL:url options:0 progress:nil completed:nil];
18 | }
19 |
20 | - (void)sd_setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options {
21 | [self sd_setHighlightedImageWithURL:url options:options progress:nil completed:nil];
22 | }
23 |
24 | - (void)sd_setHighlightedImageWithURL:(NSURL *)url completed:(SDWebImageCompletionBlock)completedBlock {
25 | [self sd_setHighlightedImageWithURL:url options:0 progress:nil completed:completedBlock];
26 | }
27 |
28 | - (void)sd_setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock {
29 | [self sd_setHighlightedImageWithURL:url options:options progress:nil completed:completedBlock];
30 | }
31 |
32 | - (void)sd_setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock {
33 | [self sd_cancelCurrentHighlightedImageLoad];
34 |
35 | if (url) {
36 | __weak __typeof(self)wself = self;
37 | id operation = [SDWebImageManager.sharedManager downloadImageWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
38 | if (!wself) return;
39 | dispatch_main_sync_safe (^
40 | {
41 | if (!wself) return;
42 | if (image && (options & SDWebImageAvoidAutoSetImage) && completedBlock)
43 | {
44 | completedBlock(image, error, cacheType, url);
45 | return;
46 | }
47 | else if (image) {
48 | wself.highlightedImage = image;
49 | [wself setNeedsLayout];
50 | }
51 | if (completedBlock && finished) {
52 | completedBlock(image, error, cacheType, url);
53 | }
54 | });
55 | }];
56 | [self sd_setImageLoadOperation:operation forKey:UIImageViewHighlightedWebCacheOperationKey];
57 | } else {
58 | dispatch_main_async_safe(^{
59 | NSError *error = [NSError errorWithDomain:SDWebImageErrorDomain code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}];
60 | if (completedBlock) {
61 | completedBlock(nil, error, SDImageCacheTypeNone, url);
62 | }
63 | });
64 | }
65 | }
66 |
67 | - (void)sd_cancelCurrentHighlightedImageLoad {
68 | [self sd_cancelImageLoadOperationWithKey:UIImageViewHighlightedWebCacheOperationKey];
69 | }
70 |
71 | @end
72 |
73 |
74 | @implementation UIImageView (HighlightedWebCacheDeprecated)
75 |
76 | - (void)setHighlightedImageWithURL:(NSURL *)url {
77 | [self sd_setHighlightedImageWithURL:url options:0 progress:nil completed:nil];
78 | }
79 |
80 | - (void)setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options {
81 | [self sd_setHighlightedImageWithURL:url options:options progress:nil completed:nil];
82 | }
83 |
84 | - (void)setHighlightedImageWithURL:(NSURL *)url completed:(SDWebImageCompletedBlock)completedBlock {
85 | [self sd_setHighlightedImageWithURL:url options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
86 | if (completedBlock) {
87 | completedBlock(image, error, cacheType);
88 | }
89 | }];
90 | }
91 |
92 | - (void)setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock {
93 | [self sd_setHighlightedImageWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
94 | if (completedBlock) {
95 | completedBlock(image, error, cacheType);
96 | }
97 | }];
98 | }
99 |
100 | - (void)setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock {
101 | [self sd_setHighlightedImageWithURL:url options:0 progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
102 | if (completedBlock) {
103 | completedBlock(image, error, cacheType);
104 | }
105 | }];
106 | }
107 |
108 | - (void)cancelCurrentHighlightedImageLoad {
109 | [self sd_cancelCurrentHighlightedImageLoad];
110 | }
111 |
112 | @end
113 |
--------------------------------------------------------------------------------
/JPGiftManager/SDWebImage/UIView+WebCacheOperation.h:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of the SDWebImage package.
3 | * (c) Olivier Poitrey
4 | *
5 | * For the full copyright and license information, please view the LICENSE
6 | * file that was distributed with this source code.
7 | */
8 |
9 | #import
10 | #import "SDWebImageManager.h"
11 |
12 | @interface UIView (WebCacheOperation)
13 |
14 | /**
15 | * Set the image load operation (storage in a UIView based dictionary)
16 | *
17 | * @param operation the operation
18 | * @param key key for storing the operation
19 | */
20 | - (void)sd_setImageLoadOperation:(id)operation forKey:(NSString *)key;
21 |
22 | /**
23 | * Cancel all operations for the current UIView and key
24 | *
25 | * @param key key for identifying the operations
26 | */
27 | - (void)sd_cancelImageLoadOperationWithKey:(NSString *)key;
28 |
29 | /**
30 | * Just remove the operations corresponding to the current UIView and key without cancelling them
31 | *
32 | * @param key key for identifying the operations
33 | */
34 | - (void)sd_removeImageLoadOperationWithKey:(NSString *)key;
35 |
36 | @end
37 |
--------------------------------------------------------------------------------
/JPGiftManager/SDWebImage/UIView+WebCacheOperation.m:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of the SDWebImage package.
3 | * (c) Olivier Poitrey
4 | *
5 | * For the full copyright and license information, please view the LICENSE
6 | * file that was distributed with this source code.
7 | */
8 |
9 | #import "UIView+WebCacheOperation.h"
10 | #import "objc/runtime.h"
11 |
12 | static char loadOperationKey;
13 |
14 | @implementation UIView (WebCacheOperation)
15 |
16 | - (NSMutableDictionary *)operationDictionary {
17 | NSMutableDictionary *operations = objc_getAssociatedObject(self, &loadOperationKey);
18 | if (operations) {
19 | return operations;
20 | }
21 | operations = [NSMutableDictionary dictionary];
22 | objc_setAssociatedObject(self, &loadOperationKey, operations, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
23 | return operations;
24 | }
25 |
26 | - (void)sd_setImageLoadOperation:(id)operation forKey:(NSString *)key {
27 | [self sd_cancelImageLoadOperationWithKey:key];
28 | NSMutableDictionary *operationDictionary = [self operationDictionary];
29 | [operationDictionary setObject:operation forKey:key];
30 | }
31 |
32 | - (void)sd_cancelImageLoadOperationWithKey:(NSString *)key {
33 | // Cancel in progress downloader from queue
34 | NSMutableDictionary *operationDictionary = [self operationDictionary];
35 | id operations = [operationDictionary objectForKey:key];
36 | if (operations) {
37 | if ([operations isKindOfClass:[NSArray class]]) {
38 | for (id operation in operations) {
39 | if (operation) {
40 | [operation cancel];
41 | }
42 | }
43 | } else if ([operations conformsToProtocol:@protocol(SDWebImageOperation)]){
44 | [(id) operations cancel];
45 | }
46 | [operationDictionary removeObjectForKey:key];
47 | }
48 | }
49 |
50 | - (void)sd_removeImageLoadOperationWithKey:(NSString *)key {
51 | NSMutableDictionary *operationDictionary = [self operationDictionary];
52 | [operationDictionary removeObjectForKey:key];
53 | }
54 |
55 | @end
56 |
--------------------------------------------------------------------------------
/JPGiftManager/ViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.h
3 | // JPGiftManager
4 | //
5 | // Created by Keep丶Dream on 2018/3/13.
6 | // Copyright © 2018年 dong. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface ViewController : UIViewController
12 |
13 |
14 | @end
15 |
16 |
--------------------------------------------------------------------------------
/JPGiftManager/ViewController.m:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.m
3 | // JPGiftManager
4 | //
5 | // Created by Keep丶Dream on 2018/3/13.
6 | // Copyright © 2018年 dong. All rights reserved.
7 | //
8 |
9 | #import "ViewController.h"
10 | #import "JPGiftView.h"
11 | #import "JPGiftCellModel.h"
12 | #import "YYModel.h"
13 | #import "JPGiftModel.h"
14 | #import "JPGiftShowManager.h"
15 | #import "UIImageView+WebCache.h"
16 |
17 | @interface ViewController ()
18 | /** gift */
19 | @property(nonatomic,strong) JPGiftView *giftView;
20 | /** gifimage */
21 | @property(nonatomic,strong) UIImageView *gifImageView;
22 | @property (weak, nonatomic) IBOutlet UIButton *changeBtn;
23 |
24 | @end
25 |
26 | @implementation ViewController
27 |
28 | - (void)viewDidLoad {
29 | [super viewDidLoad];
30 | // Do any additional setup after loading the view, typically from a nib.
31 |
32 | NSString *filePath=[[NSBundle mainBundle]pathForResource:@"data" ofType:@"json"];
33 | NSData *jsonData = [NSData dataWithContentsOfFile:filePath];
34 | NSDictionary *responseObject = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil];
35 | NSArray *data = [responseObject objectForKey:@"data"];
36 | NSMutableArray *dataArr = [NSMutableArray arrayWithArray:data];
37 | self.giftView.dataArray = [NSArray yy_modelArrayWithClass:[JPGiftCellModel class] json:dataArr];
38 |
39 | }
40 |
41 | - (UIImageView *)gifImageView{
42 |
43 | if (!_gifImageView) {
44 |
45 | _gifImageView = [[UIImageView alloc] initWithFrame:CGRectMake(7.5, 0, 360, 225)];
46 | _gifImageView.hidden = YES;
47 | }
48 | return _gifImageView;
49 | }
50 |
51 | - (JPGiftView *)giftView{
52 |
53 | if (!_giftView) {
54 |
55 | _giftView = [[JPGiftView alloc] init];
56 | _giftView.delegate = self;
57 | }
58 | return _giftView;
59 | }
60 |
61 | - (IBAction)clickGift:(id)sender {
62 |
63 | [self.giftView showGiftView];
64 | }
65 | - (IBAction)changeGifType:(id)sender {
66 |
67 | self.changeBtn.selected = !self.changeBtn.selected;
68 | }
69 |
70 | - (void)giftViewSendGiftInView:(JPGiftView *)giftView data:(JPGiftCellModel *)model {
71 |
72 | NSLog(@"点击-- %@",model.name);
73 | JPGiftModel *giftModel = [[JPGiftModel alloc] init];
74 | giftModel.userIcon = model.icon;
75 | giftModel.userName = model.username;
76 | giftModel.giftName = model.name;
77 | giftModel.giftImage = model.icon;
78 | giftModel.giftGifImage = model.icon_gif;
79 | giftModel.giftId = model.id;
80 | giftModel.defaultCount = 0;
81 | giftModel.sendCount = 1;
82 |
83 | if (self.changeBtn.selected) {
84 |
85 | [[JPGiftShowManager sharedManager] showGiftViewWithBackView:self.view info:giftModel completeBlock:^(BOOL finished) {
86 | //结束
87 | } completeShowGifImageBlock:^(JPGiftModel *giftModel) {
88 | //展示gifimage
89 | dispatch_async(dispatch_get_main_queue(), ^{
90 |
91 | UIWindow *window = [UIApplication sharedApplication].keyWindow;
92 | [window addSubview:self.gifImageView];
93 | [self.gifImageView sd_setImageWithURL:[NSURL URLWithString:giftModel.giftGifImage]];
94 | self.gifImageView.hidden = NO;
95 |
96 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
97 | self.gifImageView.hidden = YES;
98 | [self.gifImageView sd_setImageWithURL:[NSURL URLWithString:@""]];
99 | [self.gifImageView removeFromSuperview];
100 | });
101 | });
102 | }];
103 | }else {
104 |
105 | [[JPGiftShowManager sharedManager] showGiftViewWithBackView:self.view info:giftModel completeBlock:^(BOOL finished) {
106 | //结束
107 | }
108 | ];
109 |
110 | }
111 | }
112 |
113 |
114 | - (void)giftViewGetMoneyInView:(JPGiftView *)giftView {
115 |
116 | NSLog(@"充值");
117 | }
118 |
119 | @end
120 |
--------------------------------------------------------------------------------
/JPGiftManager/YYModel/YYModel.h:
--------------------------------------------------------------------------------
1 | //
2 | // YYModel.h
3 | // YYModel
4 | //
5 | // Created by ibireme on 15/5/10.
6 | // Copyright (c) 2015 ibireme.
7 | //
8 | // This source code is licensed under the MIT-style license found in the
9 | // LICENSE file in the root directory of this source tree.
10 | //
11 |
12 | #import
13 |
14 | #if __has_include()
15 | FOUNDATION_EXPORT double YYModelVersionNumber;
16 | FOUNDATION_EXPORT const unsigned char YYModelVersionString[];
17 | #import
18 | #import
19 | #else
20 | #import "NSObject+YYModel.h"
21 | #import "YYClassInfo.h"
22 | #endif
23 |
--------------------------------------------------------------------------------
/JPGiftManager/gif/giftimage.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/baiyidjp/JPGiftManager/768830504fd7e51bba8104b15509236dfd2523f3/JPGiftManager/gif/giftimage.gif
--------------------------------------------------------------------------------
/JPGiftManager/main.m:
--------------------------------------------------------------------------------
1 | //
2 | // main.m
3 | // JPGiftManager
4 | //
5 | // Created by Keep丶Dream on 2018/3/13.
6 | // Copyright © 2018年 dong. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "AppDelegate.h"
11 |
12 | int main(int argc, char * argv[]) {
13 | @autoreleasepool {
14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # iOS 直播间送礼物
2 |
3 |
4 | ----------
5 | - [点击查看-从0开始实现一个直播礼物系统](https://juejin.im/post/5acc605e51882555712caf7f)
6 |
7 | - demo基本实现了主流直播间礼物的逻辑
8 |
9 | - 按照队列顺序显示用户所送礼物,累加当前礼物,支持同时显示两人的
10 | 礼物
11 |
12 |
13 | ----------
14 |
15 |
16 | - 先来最终效果图
17 |
18 | 
19 |
20 |
21 | ----------
22 |
23 |
24 | - 一句代码调用送礼物
25 |
26 | - 具体的使用可以查看demo中viewController中的调用 (Model的数据必传,需要拼.实际项目中可以直接用服务器返回的数据Model话就行了).
27 | ```
28 | /**
29 | 送礼物(不处理第一次展示当前礼物逻辑)
30 |
31 | @param backView 礼物动画(左出)展示父view
32 | @param giftModel 礼物的数据
33 | @param completeBlock 展示完毕回调
34 | */
35 |
36 | - (void)showGiftViewWithBackView:(UIView *)backView
37 | info:(JPGiftModel *)giftModel
38 | completeBlock:(completeBlock)completeBlock;
39 |
40 | /**
41 | 送礼物(回调第一次展示当前礼物的逻辑)
42 |
43 | @param backView 礼物动画(左出)展示父view
44 | @param giftModel 礼物的数据
45 | @param completeBlock 展示完毕回调
46 | @param completeShowGifImageBlock 第一次展示当前礼物的回调(为了显示gif)
47 | */
48 | - (void)showGiftViewWithBackView:(UIView *)backView
49 | info:(JPGiftModel *)giftModel
50 | completeBlock:(completeBlock)completeBlock
51 | completeShowGifImageBlock:(completeShowGifImageBlock)completeShowGifImageBlock;
52 |
53 | ```
54 |
--------------------------------------------------------------------------------