Move the NSBezierPath and UIColor category into implementation because it's now for internal use only

This commit is contained in:
DreamPiggy 2018-03-18 22:05:47 +08:00
parent 67285ee722
commit ed0100c323
4 changed files with 60 additions and 61 deletions

View File

@ -21,6 +21,48 @@ NSString * _Nullable SDTransformedKeyForKey(NSString * _Nullable key, NSString *
return [[key stringByAppendingString:SDWebImageTransformerKeySeparator] stringByAppendingString:transformerKey];
}
@interface UIColor (HexString)
/**
Convenience way to get hex string from color. The output should always be 32-bit RGBA hex string like `#00000000`.
*/
@property (nonatomic, copy, readonly, nonnull) NSString *sd_hexString;
@end
@implementation UIColor (HexString)
- (NSString *)sd_hexString {
CGFloat red, green, blue, alpha;
#if SD_UIKIT
if (![self getRed:&red green:&green blue:&blue alpha:&alpha]) {
[self getWhite:&red alpha:&alpha];
green = red;
blue = red;
}
#else
@try {
[self getRed:&red green:&green blue:&blue alpha:&alpha];
}
@catch (NSException *exception) {
[self getWhite:&red alpha:&alpha];
green = red;
blue = red;
}
#endif
red = roundf(red * 255.f);
green = roundf(green * 255.f);
blue = roundf(blue * 255.f);
alpha = roundf(alpha * 255.f);
uint hex = ((uint)alpha << 24) | ((uint)red << 16) | ((uint)green << 8) | ((uint)blue);
return [NSString stringWithFormat:@"#%08x", hex];
}
@end
@interface SDWebImagePipelineTransformer ()
@property (nonatomic, copy, readwrite, nonnull) NSArray<id<SDWebImageTransformer>> *transformers;

View File

@ -26,28 +26,6 @@ typedef NS_OPTIONS(NSUInteger, SDRectCorner) {
};
#endif
#pragma mark - Useful category
@interface UIColor (Additions)
/**
Convenience way to get hex string from color. The output should always be 32-bit RGBA hex string like `#00000000`.
*/
@property (nonatomic, copy, readonly, nonnull) NSString *sd_hexString;
@end
#if SD_MAC
@interface NSBezierPath (Additions)
/**
Convenience way to create a bezier path with the specify rounding corners on macOS. Same as the one on `UIBezierPath`.
*/
+ (nonnull instancetype)sd_bezierPathWithRoundedRect:(NSRect)rect byRoundingCorners:(SDRectCorner)corners cornerRadius:(CGFloat)cornerRadius;
@end
#endif
/**
Provide some commen method for `UIImage`.
Image process is based on Core Graphics and vImage.
@ -135,7 +113,7 @@ typedef NS_OPTIONS(NSUInteger, SDRectCorner) {
/**
Return the pixel color array with specify rectangle. The rect is from the top-left to the bottom-right and 0-based. The returned the color is always be RGBA format. The image must be CG-based.
@note The rect's width/height should not be smaller than or equal to 0. The minX/minY should not be smaller than 0. The maxX/maxY should not be greater than width/height. Attention this limit is different from point(point: (0,0) like rect: (0, 0, 1, 1))
@note The rect's width/height should not be smaller than or equal to 0. The minX/minY should not be smaller than 0. The maxX/maxY should not be greater than width/height. Attention this limit is different from `sd_colorAtPoint:` (point: (0, 0) like rect: (0, 0, 1, 1))
@note The overhead of object creation means this method is best suited for infrequent color sampling. For heavy image processing, grab the raw bitmap data and process yourself.
@param rect The rectangle of pixels

View File

@ -13,10 +13,6 @@
#import <CoreImage/CoreImage.h>
#endif
#ifndef SD_SWAP // swap two value
#define SD_SWAP(_a_, _b_) do { __typeof__(_a_) _tmp_ = (_a_); (_a_) = (_b_); (_b_) = _tmp_; } while (0)
#endif
#if SD_MAC
static CGContextRef SDCGContextCreateARGBBitmapContext(CGSize size, BOOL opaque, CGFloat scale) {
size_t width = ceil(size.width * scale);
@ -225,42 +221,17 @@ static inline UIColor * SDGetColorFromPixel(Pixel_8888 pixel, CGBitmapInfo bitma
return [UIColor colorWithRed:r green:g blue:b alpha:a];
}
@implementation UIColor (Additions)
#if SD_MAC
@interface NSBezierPath (RoundedCorners)
- (NSString *)sd_hexString {
CGFloat red, green, blue, alpha;
#if SD_UIKIT
if (![self getRed:&red green:&green blue:&blue alpha:&alpha]) {
[self getWhite:&red alpha:&alpha];
green = red;
blue = red;
}
#else
@try {
[self getRed:&red green:&green blue:&blue alpha:&alpha];
}
@catch (NSException *exception) {
[self getWhite:&red alpha:&alpha];
green = red;
blue = red;
}
#endif
red = roundf(red * 255.f);
green = roundf(green * 255.f);
blue = roundf(blue * 255.f);
alpha = roundf(alpha * 255.f);
uint hex = ((uint)alpha << 24) | ((uint)red << 16) | ((uint)green << 8) | ((uint)blue);
return [NSString stringWithFormat:@"#%08x", hex];
}
/**
Convenience way to create a bezier path with the specify rounding corners on macOS. Same as the one on `UIBezierPath`.
*/
+ (nonnull instancetype)sd_bezierPathWithRoundedRect:(NSRect)rect byRoundingCorners:(SDRectCorner)corners cornerRadius:(CGFloat)cornerRadius;
@end
#if SD_MAC
@implementation NSBezierPath (Additions)
@implementation NSBezierPath (RoundedCorners)
+ (instancetype)sd_bezierPathWithRoundedRect:(NSRect)rect byRoundingCorners:(SDRectCorner)corners cornerRadius:(CGFloat)cornerRadius {
NSBezierPath *path = [NSBezierPath bezierPath];
@ -288,7 +259,6 @@ static inline UIColor * SDGetColorFromPixel(Pixel_8888 pixel, CGBitmapInfo bitma
}
@end
#endif
@implementation UIImage (Transform)
@ -664,7 +634,9 @@ static inline UIColor * SDGetColorFromPixel(Pixel_8888 pixel, CGBitmapInfo bitma
void *temp = malloc(tempSize);
for (int i = 0; i < iterations; i++) {
vImageBoxConvolve_ARGB8888(input, output, temp, 0, 0, radius, radius, NULL, kvImageEdgeExtend);
SD_SWAP(input, output);
vImage_Buffer *tmp = input;
input = output;
output = tmp;
}
free(temp);
}

View File

@ -18,6 +18,13 @@
#import <SDWebImage/UIImage+Transform.h>
#import <CoreImage/CoreImage.h>
// Internal header
@interface UIColor (HexString)
@property (nonatomic, copy, readonly, nonnull) NSString *sd_hexString;
@end
@interface SDCategoriesTests : SDTestCase
@property (nonatomic, strong) UIImage *testImage;