ibireme 9 жил өмнө
parent
commit
19cfbf9a3d

+ 1 - 1
Demo/YYWebImageDemo.xcodeproj/project.pbxproj

@@ -258,11 +258,11 @@
 				ABC423B11BE324F000703518 /* Categories */,
 				ABC423B11BE324F000703518 /* Categories */,
 				ABC423C91BE324F000703518 /* YYImageCache.h */,
 				ABC423C91BE324F000703518 /* YYImageCache.h */,
 				ABC423CA1BE324F000703518 /* YYImageCache.m */,
 				ABC423CA1BE324F000703518 /* YYImageCache.m */,
-				ABC423CB1BE324F000703518 /* YYWebImage.h */,
 				ABC423CC1BE324F000703518 /* YYWebImageManager.h */,
 				ABC423CC1BE324F000703518 /* YYWebImageManager.h */,
 				ABC423CD1BE324F000703518 /* YYWebImageManager.m */,
 				ABC423CD1BE324F000703518 /* YYWebImageManager.m */,
 				ABC423CE1BE324F000703518 /* YYWebImageOperation.h */,
 				ABC423CE1BE324F000703518 /* YYWebImageOperation.h */,
 				ABC423CF1BE324F000703518 /* YYWebImageOperation.m */,
 				ABC423CF1BE324F000703518 /* YYWebImageOperation.m */,
+				ABC423CB1BE324F000703518 /* YYWebImage.h */,
 			);
 			);
 			name = YYWebImage;
 			name = YYWebImage;
 			path = ../YYWebImage;
 			path = ../YYWebImage;

+ 1 - 1
Demo/YYWebImageDemo/YYWebImageExample.m

@@ -192,7 +192,7 @@
 
 
 - (void)reload {
 - (void)reload {
     [[YYImageCache sharedCache].memoryCache removeAllObjects];
     [[YYImageCache sharedCache].memoryCache removeAllObjects];
-    [[YYImageCache sharedCache].diskCache removeAllObjectsWithBlock:nil];
+    [[YYImageCache sharedCache].diskCache removeAllObjectsWithBlock:^{}];
     [self.tableView performSelector:@selector(reloadData) withObject:nil afterDelay:0.1];
     [self.tableView performSelector:@selector(reloadData) withObject:nil afterDelay:0.1];
 }
 }
 
 

+ 15 - 9
YYWebImage/YYImageCache.h

@@ -68,7 +68,7 @@ typedef NS_OPTIONS(NSUInteger, YYImageCacheType) {
  @discussion When fetch image from disk cache, it will use 'YYImage' to decode 
  @discussion When fetch image from disk cache, it will use 'YYImage' to decode 
  animated image such as WebP/APNG/GIF. Set to 'NO' to ignore animated image.
  animated image such as WebP/APNG/GIF. Set to 'NO' to ignore animated image.
  */
  */
-@property (assign) BOOL allowAnimatedImage;
+@property BOOL allowAnimatedImage;
 
 
 /**
 /**
  Whether decode the image to memory bitmap. Default is YES.
  Whether decode the image to memory bitmap. Default is YES.
@@ -76,7 +76,7 @@ typedef NS_OPTIONS(NSUInteger, YYImageCacheType) {
  @discussion If the value is YES, then the image will be decoded to memory bitmap
  @discussion If the value is YES, then the image will be decoded to memory bitmap
  for better display performance, but may cost more memory.
  for better display performance, but may cost more memory.
  */
  */
-@property (assign) BOOL decodeForDisplay;
+@property BOOL decodeForDisplay;
 
 
 
 
 #pragma mark - Initializer
 #pragma mark - Initializer
@@ -100,7 +100,7 @@ typedef NS_OPTIONS(NSUInteger, YYImageCacheType) {
  Once initialized you should not read and write to this directory.
  Once initialized you should not read and write to this directory.
  @result A new cache object, or nil if an error occurs.
  @result A new cache object, or nil if an error occurs.
  */
  */
-- (instancetype)initWithPath:(NSString *)path NS_DESIGNATED_INITIALIZER;
+- (nullable instancetype)initWithPath:(NSString *)path NS_DESIGNATED_INITIALIZER;
 
 
 
 
 #pragma mark - Access Methods
 #pragma mark - Access Methods
@@ -131,7 +131,10 @@ typedef NS_OPTIONS(NSUInteger, YYImageCacheType) {
  @param key       The key with which to associate the image. If nil, this method has no effect.
  @param key       The key with which to associate the image. If nil, this method has no effect.
  @param type      The cache type to store image.
  @param type      The cache type to store image.
  */
  */
-- (void)setImage:(UIImage *)image imageData:(NSData *)imageData forKey:(NSString *)key withType:(YYImageCacheType)type;
+- (void)setImage:(nullable UIImage *)image
+       imageData:(nullable NSData *)imageData
+          forKey:(NSString *)key
+        withType:(YYImageCacheType)type;
 
 
 /**
 /**
  Removes the image of the specified key in the cache (both memory and disk).
  Removes the image of the specified key in the cache (both memory and disk).
@@ -179,7 +182,7 @@ typedef NS_OPTIONS(NSUInteger, YYImageCacheType) {
  @param key A string identifying the image. If nil, just return nil.
  @param key A string identifying the image. If nil, just return nil.
  @return The image associated with key, or nil if no image is associated with key.
  @return The image associated with key, or nil if no image is associated with key.
  */
  */
-- (UIImage *)getImageForKey:(NSString *)key;
+- (nullable UIImage *)getImageForKey:(NSString *)key;
 
 
 /**
 /**
  Returns the image associated with a given key.
  Returns the image associated with a given key.
@@ -189,7 +192,7 @@ typedef NS_OPTIONS(NSUInteger, YYImageCacheType) {
  @param key A string identifying the image. If nil, just return nil.
  @param key A string identifying the image. If nil, just return nil.
  @return The image associated with key, or nil if no image is associated with key.
  @return The image associated with key, or nil if no image is associated with key.
  */
  */
-- (UIImage *)getImageForKey:(NSString *)key withType:(YYImageCacheType)type;
+- (nullable UIImage *)getImageForKey:(NSString *)key withType:(YYImageCacheType)type;
 
 
 /**
 /**
  Asynchronously get the image associated with a given key.
  Asynchronously get the image associated with a given key.
@@ -198,7 +201,9 @@ typedef NS_OPTIONS(NSUInteger, YYImageCacheType) {
  @param type  The cache type.
  @param type  The cache type.
  @param block A completion block which will be called on main thread.
  @param block A completion block which will be called on main thread.
  */
  */
-- (void)getImageForKey:(NSString *)key withType:(YYImageCacheType)type withBlock:(void(^)(UIImage *image, YYImageCacheType type))block;
+- (void)getImageForKey:(NSString *)key
+              withType:(YYImageCacheType)type
+             withBlock:(void(^)(UIImage * _Nullable image, YYImageCacheType type))block;
 
 
 /**
 /**
  Returns the image data associated with a given key.
  Returns the image data associated with a given key.
@@ -207,7 +212,7 @@ typedef NS_OPTIONS(NSUInteger, YYImageCacheType) {
  @param key A string identifying the image. If nil, just return nil.
  @param key A string identifying the image. If nil, just return nil.
  @return The image data associated with key, or nil if no image is associated with key.
  @return The image data associated with key, or nil if no image is associated with key.
  */
  */
-- (NSData *)getImageDataForKey:(NSString *)key;
+- (nullable NSData *)getImageDataForKey:(NSString *)key;
 
 
 /**
 /**
  Asynchronously get the image data associated with a given key.
  Asynchronously get the image data associated with a given key.
@@ -215,7 +220,8 @@ typedef NS_OPTIONS(NSUInteger, YYImageCacheType) {
  @param key   A string identifying the image. If nil, just return nil.
  @param key   A string identifying the image. If nil, just return nil.
  @param block A completion block which will be called on main thread.
  @param block A completion block which will be called on main thread.
  */
  */
-- (void)getImageDataForKey:(NSString *)key withBlock:(void(^)(NSData *imageData))block;
+- (void)getImageDataForKey:(NSString *)key
+                 withBlock:(void(^)(NSData * _Nullable imageData))block;
 
 
 @end
 @end
 
 

+ 1 - 1
YYWebImage/YYImageCache.m

@@ -100,7 +100,7 @@ static inline dispatch_queue_t YYImageCacheDecodeQueue() {
 
 
 - (instancetype)init {
 - (instancetype)init {
     @throw [NSException exceptionWithName:@"YYImageCache init error" reason:@"YYImageCache must be initialized with a path. Use 'initWithPath:' instead." userInfo:nil];
     @throw [NSException exceptionWithName:@"YYImageCache init error" reason:@"YYImageCache must be initialized with a path. Use 'initWithPath:' instead." userInfo:nil];
-    return [self initWithPath:nil];
+    return [self initWithPath:@""];
 }
 }
 
 
 - (instancetype)initWithPath:(NSString *)path {
 - (instancetype)initWithPath:(NSString *)path {

+ 26 - 18
YYWebImage/YYWebImageManager.h

@@ -19,6 +19,7 @@
 
 
 @class YYWebImageOperation;
 @class YYWebImageOperation;
 
 
+NS_ASSUME_NONNULL_BEGIN
 
 
 /// The options to control image operation.
 /// The options to control image operation.
 typedef NS_OPTIONS(NSUInteger, YYWebImageOptions) {
 typedef NS_OPTIONS(NSUInteger, YYWebImageOptions) {
@@ -138,7 +139,7 @@ typedef void(^YYWebImageProgressBlock)(NSInteger receivedSize, NSInteger expecte
  @param url   The image url (remote or local file path).
  @param url   The image url (remote or local file path).
  @return The transformed image.
  @return The transformed image.
  */
  */
-typedef UIImage *(^YYWebImageTransformBlock)(UIImage *image, NSURL *url);
+typedef UIImage * _Nullable (^YYWebImageTransformBlock)(UIImage *image, NSURL *url);
 
 
 /**
 /**
  The block invoked when image fetch finished or cancelled.
  The block invoked when image fetch finished or cancelled.
@@ -149,7 +150,11 @@ typedef UIImage *(^YYWebImageTransformBlock)(UIImage *image, NSURL *url);
  @param error       Error during image fetching.
  @param error       Error during image fetching.
  @param finished    If the operation is cancelled, this value is NO, otherwise YES.
  @param finished    If the operation is cancelled, this value is NO, otherwise YES.
  */
  */
-typedef void (^YYWebImageCompletionBlock)(UIImage *image, NSURL *url, YYWebImageFromType from, YYWebImageStage stage, NSError *error);
+typedef void (^YYWebImageCompletionBlock)(UIImage * _Nullable image,
+                                          NSURL *url,
+                                          YYWebImageFromType from,
+                                          YYWebImageStage stage,
+                                          NSError * _Nullable error);
 
 
 
 
 
 
@@ -174,7 +179,8 @@ typedef void (^YYWebImageCompletionBlock)(UIImage *image, NSURL *url, YYWebImage
                 (pass nil to make the new operation start immediately without queue).
                 (pass nil to make the new operation start immediately without queue).
  @return A new manager.
  @return A new manager.
  */
  */
-- (instancetype)initWithCache:(YYImageCache *)cache queue:(NSOperationQueue *)queue NS_DESIGNATED_INITIALIZER;
+- (instancetype)initWithCache:(nullable YYImageCache *)cache
+                        queue:(nullable NSOperationQueue *)queue NS_DESIGNATED_INITIALIZER;
 
 
 - (instancetype)init UNAVAILABLE_ATTRIBUTE;
 - (instancetype)init UNAVAILABLE_ATTRIBUTE;
 + (instancetype)new UNAVAILABLE_ATTRIBUTE;
 + (instancetype)new UNAVAILABLE_ATTRIBUTE;
@@ -189,17 +195,17 @@ typedef void (^YYWebImageCompletionBlock)(UIImage *image, NSURL *url, YYWebImage
  @param completion Completion block which will be invoked on background thread  (pass nil to avoid).
  @param completion Completion block which will be invoked on background thread  (pass nil to avoid).
  @return A new image operation.
  @return A new image operation.
  */
  */
-- (YYWebImageOperation *)requestImageWithURL:(NSURL *)url
-                                     options:(YYWebImageOptions)options
-                                    progress:(YYWebImageProgressBlock)progress
-                                   transform:(YYWebImageTransformBlock)transform
-                                  completion:(YYWebImageCompletionBlock)completion;
+- (nullable YYWebImageOperation *)requestImageWithURL:(NSURL *)url
+                                              options:(YYWebImageOptions)options
+                                             progress:(nullable YYWebImageProgressBlock)progress
+                                            transform:(nullable YYWebImageTransformBlock)transform
+                                           completion:(nullable YYWebImageCompletionBlock)completion;
 
 
 /**
 /**
  The image cache used by image operation. 
  The image cache used by image operation. 
  You can set it to nil to avoid image cache.
  You can set it to nil to avoid image cache.
  */
  */
-@property (nonatomic, strong) YYImageCache *cache;
+@property (nullable, nonatomic, strong) YYImageCache *cache;
 
 
 /**
 /**
  The operation queue on which image operations are scheduled and run.
  The operation queue on which image operations are scheduled and run.
@@ -208,7 +214,7 @@ typedef void (^YYWebImageCompletionBlock)(UIImage *image, NSURL *url, YYWebImage
  You can use this queue to control maximum number of concurrent operations, to obtain 
  You can use this queue to control maximum number of concurrent operations, to obtain 
  the status of the current operations, or to cancel all operations in this manager.
  the status of the current operations, or to cancel all operations in this manager.
  */
  */
-@property (nonatomic, strong) NSOperationQueue *queue;
+@property (nullable, nonatomic, strong) NSOperationQueue *queue;
 
 
 /**
 /**
  The shared transform block to process image. Default is nil.
  The shared transform block to process image. Default is nil.
@@ -216,27 +222,27 @@ typedef void (^YYWebImageCompletionBlock)(UIImage *image, NSURL *url, YYWebImage
  When called `requestImageWithURL:options:progress:transform:completion` and
  When called `requestImageWithURL:options:progress:transform:completion` and
  the `transform` is nil, this block will be used.
  the `transform` is nil, this block will be used.
  */
  */
-@property (nonatomic, copy) YYWebImageTransformBlock sharedTransformBlock;
+@property (nullable, nonatomic, copy) YYWebImageTransformBlock sharedTransformBlock;
 
 
 /**
 /**
  The image request timeout interval in seconds. Default is 15.
  The image request timeout interval in seconds. Default is 15.
  */
  */
-@property (nonatomic, assign) NSTimeInterval timeout;
+@property (nonatomic) NSTimeInterval timeout;
 
 
 /**
 /**
  The username used by NSURLCredential, default is nil.
  The username used by NSURLCredential, default is nil.
  */
  */
-@property (nonatomic, strong) NSString *username;
+@property (nullable, nonatomic) NSString *username;
 
 
 /**
 /**
  The password used by NSURLCredential, default is nil.
  The password used by NSURLCredential, default is nil.
  */
  */
-@property (nonatomic, strong) NSString *password;
+@property (nullable, nonatomic) NSString *password;
 
 
 /**
 /**
  The image HTTP request header. Default is "Accept:image/webp,image/\*;q=0.8".
  The image HTTP request header. Default is "Accept:image/webp,image/\*;q=0.8".
  */
  */
-@property (nonatomic, copy) NSDictionary *headers;
+@property (nullable, nonatomic, copy) NSDictionary<NSString *, NSString *> *headers;
 
 
 /**
 /**
  A block which will be invoked for each image HTTP request to do additional
  A block which will be invoked for each image HTTP request to do additional
@@ -244,14 +250,14 @@ typedef void (^YYWebImageCompletionBlock)(UIImage *image, NSURL *url, YYWebImage
  
  
  Use this block to add or remove HTTP header field for a specified URL.
  Use this block to add or remove HTTP header field for a specified URL.
  */
  */
-@property (nonatomic, copy) NSDictionary *(^headersFilter)(NSURL *url, NSDictionary *header);
+@property (nullable, nonatomic, copy) NSDictionary<NSString *, NSString *> *(^headersFilter)(NSURL *url, NSDictionary<NSString *, NSString *> * _Nullable header);
 
 
 /**
 /**
  A block which will be invoked for each image operation. Default is nil.
  A block which will be invoked for each image operation. Default is nil.
  
  
  Use this block to provide a custom image cache key for a specified URL.
  Use this block to provide a custom image cache key for a specified URL.
  */
  */
-@property (nonatomic, copy) NSString *(^cacheKeyFilter)(NSURL *url);
+@property (nullable, nonatomic, copy) NSString *(^cacheKeyFilter)(NSURL *url);
 
 
 /**
 /**
  Returns the HTTP headers for a specified URL.
  Returns the HTTP headers for a specified URL.
@@ -259,7 +265,7 @@ typedef void (^YYWebImageCompletionBlock)(UIImage *image, NSURL *url, YYWebImage
  @param url A specified URL.
  @param url A specified URL.
  @return HTTP headers.
  @return HTTP headers.
  */
  */
-- (NSDictionary *)headersForURL:(NSURL *)url;
+- (nullable NSDictionary<NSString *, NSString *> *)headersForURL:(NSURL *)url;
 
 
 /**
 /**
  Returns the cache key for a specified URL.
  Returns the cache key for a specified URL.
@@ -302,3 +308,5 @@ typedef void (^YYWebImageCompletionBlock)(UIImage *image, NSURL *url, YYWebImage
 + (NSInteger)currentNetworkActivityCount;
 + (NSInteger)currentNetworkActivityCount;
 
 
 @end
 @end
+
+NS_ASSUME_NONNULL_END

+ 16 - 12
YYWebImage/YYWebImageOperation.h

@@ -19,6 +19,8 @@
 #import "YYWebImageManager.h"
 #import "YYWebImageManager.h"
 #endif
 #endif
 
 
+NS_ASSUME_NONNULL_BEGIN
+
 /**
 /**
  The YYWebImageOperation class is an NSOperation subclass used to fetch image 
  The YYWebImageOperation class is an NSOperation subclass used to fetch image 
  from URL request.
  from URL request.
@@ -37,11 +39,11 @@
  */
  */
 @interface YYWebImageOperation : NSOperation
 @interface YYWebImageOperation : NSOperation
 
 
-@property (nonatomic, strong, readonly) NSURLRequest *request;     ///< The image URL request.
-@property (nonatomic, strong, readonly) NSURLResponse *response;   ///< The response for request.
-@property (nonatomic, assign, readonly) YYWebImageOptions options; ///< The operation's option.
-@property (nonatomic, strong, readonly) YYImageCache *cache;       ///< The image cache.
-@property (nonatomic, strong, readonly) NSString *cacheKey;        ///< The image cache key.
+@property (nonatomic, strong, readonly)           NSURLRequest      *request;  ///< The image URL request.
+@property (nullable, nonatomic, strong, readonly) NSURLResponse     *response; ///< The response for request.
+@property (nullable, nonatomic, strong, readonly) YYImageCache      *cache;    ///< The image cache.
+@property (nonatomic, strong, readonly)           NSString          *cacheKey; ///< The image cache key.
+@property (nonatomic, readonly)                   YYWebImageOptions options;   ///< The operation's option.
 
 
 /**
 /**
  Whether the URL connection should consult the credential storage for authenticating 
  Whether the URL connection should consult the credential storage for authenticating 
@@ -50,7 +52,7 @@
  @discussion This is the value that is returned in the `NSURLConnectionDelegate` 
  @discussion This is the value that is returned in the `NSURLConnectionDelegate` 
  method `-connectionShouldUseCredentialStorage:`.
  method `-connectionShouldUseCredentialStorage:`.
  */
  */
-@property (nonatomic, assign) BOOL shouldUseCredentialStorage;
+@property (nonatomic) BOOL shouldUseCredentialStorage;
 
 
 /**
 /**
  The credential used for authentication challenges in `-connection:didReceiveAuthenticationChallenge:`.
  The credential used for authentication challenges in `-connection:didReceiveAuthenticationChallenge:`.
@@ -58,7 +60,7 @@
  @discussion This will be overridden by any shared credentials that exist for the 
  @discussion This will be overridden by any shared credentials that exist for the 
  username or password of the request URL, if present.
  username or password of the request URL, if present.
  */
  */
-@property (nonatomic, strong) NSURLCredential *credential;
+@property (nullable, nonatomic, strong) NSURLCredential *credential;
 
 
 /**
 /**
  Creates and returns a new operation.
  Creates and returns a new operation.
@@ -81,13 +83,15 @@
  */
  */
 - (instancetype)initWithRequest:(NSURLRequest *)request
 - (instancetype)initWithRequest:(NSURLRequest *)request
                         options:(YYWebImageOptions)options
                         options:(YYWebImageOptions)options
-                          cache:(YYImageCache *)cache
-                       cacheKey:(NSString *)cacheKey
-                       progress:(YYWebImageProgressBlock)progress
-                      transform:(YYWebImageTransformBlock)transform
-                     completion:(YYWebImageCompletionBlock)completion NS_DESIGNATED_INITIALIZER;
+                          cache:(nullable YYImageCache *)cache
+                       cacheKey:(nullable NSString *)cacheKey
+                       progress:(nullable YYWebImageProgressBlock)progress
+                      transform:(nullable YYWebImageTransformBlock)transform
+                     completion:(nullable YYWebImageCompletionBlock)completion NS_DESIGNATED_INITIALIZER;
 
 
 - (instancetype)init UNAVAILABLE_ATTRIBUTE;
 - (instancetype)init UNAVAILABLE_ATTRIBUTE;
 + (instancetype)new UNAVAILABLE_ATTRIBUTE;
 + (instancetype)new UNAVAILABLE_ATTRIBUTE;
 
 
 @end
 @end
+
+NS_ASSUME_NONNULL_END

+ 1 - 1
YYWebImage/YYWebImageOperation.m

@@ -248,7 +248,7 @@ static void URLInBlackListAdd(NSURL *url) {
 
 
 - (instancetype)init {
 - (instancetype)init {
     @throw [NSException exceptionWithName:@"YYWebImageOperation init error" reason:@"YYWebImageOperation must be initialized with a request. Use the designated initializer to init." userInfo:nil];
     @throw [NSException exceptionWithName:@"YYWebImageOperation init error" reason:@"YYWebImageOperation must be initialized with a request. Use the designated initializer to init." userInfo:nil];
-    return [self initWithRequest:nil options:0 cache:nil cacheKey:nil progress:nil transform:nil completion:nil];
+    return [self initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@""]] options:0 cache:nil cacheKey:nil progress:nil transform:nil completion:nil];
 }
 }
 
 
 - (instancetype)initWithRequest:(NSURLRequest *)request
 - (instancetype)initWithRequest:(NSURLRequest *)request