140 lines
5.0 KiB
Mathematica
140 lines
5.0 KiB
Mathematica
|
|
//
|
|||
|
|
// UIImage+QX.m
|
|||
|
|
// QXLive
|
|||
|
|
//
|
|||
|
|
// Created by 启星 on 2025/4/28.
|
|||
|
|
//
|
|||
|
|
|
|||
|
|
#import "UIImage+QX.h"
|
|||
|
|
|
|||
|
|
@implementation UIImage (QX)
|
|||
|
|
// 指定尺寸 进行图片压缩
|
|||
|
|
- (NSData *)qx_compressImageQualityWithToByte:(NSInteger)maxLength {
|
|||
|
|
CGFloat compression = 1;
|
|||
|
|
NSData *data = UIImageJPEGRepresentation(self, compression);
|
|||
|
|
if (data.length <= maxLength) {
|
|||
|
|
return data;
|
|||
|
|
}
|
|||
|
|
compression = (CGFloat)maxLength / data.length;
|
|||
|
|
return UIImageJPEGRepresentation(self, compression);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
+ (UIImage *)qx_convertViewToImageWith:(UIView *)view {
|
|||
|
|
//UIGraphicsBeginImageContextWithOptions(区域大小, 是否是非透明的, 屏幕密度);
|
|||
|
|
UIGraphicsBeginImageContextWithOptions(view.frame.size, YES, [UIScreen mainScreen].scale);
|
|||
|
|
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
|
|||
|
|
UIImage *imageRet = UIGraphicsGetImageFromCurrentImageContext();
|
|||
|
|
UIGraphicsEndImageContext();
|
|||
|
|
|
|||
|
|
return imageRet;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
+ (UIImage *)qx_imageWithColor:(UIColor *)color {
|
|||
|
|
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
|
|||
|
|
UIGraphicsBeginImageContext(rect.size);
|
|||
|
|
CGContextRef context = UIGraphicsGetCurrentContext();
|
|||
|
|
|
|||
|
|
CGContextSetFillColorWithColor(context, [color CGColor]);
|
|||
|
|
CGContextFillRect(context, rect);
|
|||
|
|
|
|||
|
|
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
|
|||
|
|
UIGraphicsEndImageContext();
|
|||
|
|
|
|||
|
|
return image;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
+ (UIImage *)qx_gradientColorImageFromColors:(NSArray *)colors gradientType:(GradientType)gradientType imgSize:(CGSize)imgSize {
|
|||
|
|
NSMutableArray *ar = [NSMutableArray array];
|
|||
|
|
for (UIColor *c in colors) {
|
|||
|
|
[ar addObject:(id)c.CGColor];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
UIGraphicsBeginImageContextWithOptions(imgSize, YES, 1);
|
|||
|
|
CGContextRef context = UIGraphicsGetCurrentContext();
|
|||
|
|
CGContextSaveGState(context);
|
|||
|
|
CGColorSpaceRef colorSpace = CGColorGetColorSpace([[colors lastObject] CGColor]);
|
|||
|
|
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)ar, NULL);
|
|||
|
|
CGPoint start;
|
|||
|
|
CGPoint end;
|
|||
|
|
|
|||
|
|
switch (gradientType) {
|
|||
|
|
case GradientTypeTopToBottom:
|
|||
|
|
start = CGPointMake(0.0, 0.0);
|
|||
|
|
end = CGPointMake(0.0, imgSize.height);
|
|||
|
|
break;
|
|||
|
|
case GradientTypeLeftToRight:
|
|||
|
|
start = CGPointMake(0.0, 0.0);
|
|||
|
|
end = CGPointMake(imgSize.width, 0.0);
|
|||
|
|
break;
|
|||
|
|
case GradientTypeUpleftToLowright:
|
|||
|
|
start = CGPointMake(0.0, 0.0);
|
|||
|
|
end = CGPointMake(imgSize.width, imgSize.height);
|
|||
|
|
break;
|
|||
|
|
case GradientTypeUprightToLowleft:
|
|||
|
|
start = CGPointMake(imgSize.width, 0.0);
|
|||
|
|
end = CGPointMake(0.0, imgSize.height);
|
|||
|
|
break;
|
|||
|
|
default: break;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
|
|||
|
|
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
|
|||
|
|
CGGradientRelease(gradient);
|
|||
|
|
CGContextRestoreGState(context);
|
|||
|
|
CGColorSpaceRelease(colorSpace);
|
|||
|
|
UIGraphicsEndImageContext();
|
|||
|
|
return image;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
- (instancetype)qx_imageFitWithSize:(CGSize)size fillColor:(UIColor *)fillColor {
|
|||
|
|
UIGraphicsBeginImageContextWithOptions(size, YES, 0);
|
|||
|
|
CGRect rect = (CGRect) { CGPointZero, size };
|
|||
|
|
[fillColor setFill];
|
|||
|
|
UIRectFill(rect);
|
|||
|
|
[self drawInRect:rect];
|
|||
|
|
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
|
|||
|
|
UIGraphicsEndImageContext();
|
|||
|
|
return image;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
- (instancetype)qx_imageCornerWithRadius:(CGFloat)radius byRoundingCorners:(UIRectCorner)corners fillColor:(UIColor *)fillColor zoomSize:(CGSize)zoomSize {
|
|||
|
|
UIGraphicsBeginImageContextWithOptions(zoomSize, YES, 0);
|
|||
|
|
CGRect rect = (CGRect) { CGPointZero, zoomSize };
|
|||
|
|
[fillColor setFill];
|
|||
|
|
UIRectFill(rect);
|
|||
|
|
CGSize size = CGSizeMake(radius, radius);
|
|||
|
|
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:size];
|
|||
|
|
[path addClip];
|
|||
|
|
[self drawInRect:rect];
|
|||
|
|
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
|
|||
|
|
return img;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
- (instancetype)qx_imageCornerWithRadius:(CGFloat)radius fillColor:(UIColor *)fillColor zoomSize:(CGSize)zoomSize {
|
|||
|
|
return [self qx_imageCornerWithRadius:radius byRoundingCorners:UIRectCornerAllCorners fillColor:fillColor zoomSize:zoomSize];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
+ (instancetype)qx_getAppLauchImage {
|
|||
|
|
CGSize viewSize = [UIScreen mainScreen].bounds.size;
|
|||
|
|
NSString *viewOrienttation = @"Portrait";
|
|||
|
|
NSString *launchImage = nil;
|
|||
|
|
NSArray *imageDict = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
|
|||
|
|
for (NSDictionary *dict in imageDict) {
|
|||
|
|
if (launchImage) {
|
|||
|
|
return [UIImage imageNamed:launchImage];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
CGSize imageSize = CGSizeFromString(dict[@"UILaunchImageSize"]);
|
|||
|
|
if (CGSizeEqualToSize(imageSize, viewSize) && [viewOrienttation isEqualToString:dict[@"UILaunchImageOrientation"]]) {
|
|||
|
|
launchImage = dict[@"UILaunchImageName"];
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if (!launchImage) {
|
|||
|
|
// 取不到就直接取,6s的
|
|||
|
|
return [UIImage imageNamed:@"LaunchImage-800-667h"];
|
|||
|
|
}
|
|||
|
|
return [UIImage imageNamed:launchImage];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@end
|