#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface XImgDownloader : NSObject
/**
* SD 下载
* 入参 网络图片地址
* 返回 图片 data
*/
+ (void)goDownloadURLStr:(NSString *)imgStr imgData:(ObjBlock)imgData;
+ (void)goDownloadURLStr:(NSString *)imgStr imgData:(ObjBlock)imgData k:(NSUInteger)k;
@end
NS_ASSUME_NONNULL_END
=================== =================== ===================
#import "XImgDownloader.h"
#import <SDWebImage.h>
@implementation XImgDownloader
+ (void)goDownloadURLStr:(NSString *)imgStr imgData:(ObjBlock)imgData
{
SDWebImageManager *manager = [SDWebImageManager sharedManager];
NSString *key = [manager cacheKeyForURL:[NSURL URLWithString:imgStr]];
SDImageCache *cache = [SDImageCache sharedImageCache];
UIImage *image = [cache imageFromCacheForKey:key];
if (image)
{
NSData *data = UIImageJPEGRepresentation(image, 0.8);
imgData(data);
}
else
{
SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader];
[downloader downloadImageWithURL:[NSURL URLWithString:imgStr] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
if (!error)
{
NSData *data = UIImageJPEGRepresentation(image, 0.8);
imgData(data);
}
}];
}
}
+ (void)goDownloadURLStr:(NSString *)imgStr imgData:(ObjBlock)imgData k:(NSUInteger)k;
{
SDWebImageManager *manager = [SDWebImageManager sharedManager];
NSString *key = [manager cacheKeyForURL:[NSURL URLWithString:imgStr]];
SDImageCache *cache = [SDImageCache sharedImageCache];
UIImage *image = [cache imageFromCacheForKey:key];
if (image)
{
//NSData *data = UIImageJPEGRepresentation(image, 0.8);
imgData([self scaledImageForKey:k image:image]);
}
else
{
SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader];
[downloader downloadImageWithURL:[NSURL URLWithString:imgStr] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
if (!error)
{
//NSData *data = UIImageJPEGRepresentation(image, 0.8);
imgData([self scaledImageForKey:k image:image]);
}
}];
}
}
+ (UIImage *)scaledImageForKey64_image:(UIImage *)image
{
// 检查图片大小是否超过64KB
NSData *imageData = UIImageJPEGRepresentation(image, 1.0); // 使用JPEG格式压缩
if (imageData.length > 64 * 1024) { // 64KB = 64 * 1024 bytes
CGFloat maxSize = 64.0 * 1024.0; // 最大64KB
CGFloat compression = 0.9f; // 初始压缩质量
while (imageData.length > maxSize && compression > 0.1f) {
compression -= 0.1f; // 逐步减小压缩质量
imageData = UIImageJPEGRepresentation(image, compression);
}
image = [UIImage imageWithData:imageData]; // 重新创建图片
}
return image;
}
+ (NSData *)scaledImageForKey:(NSUInteger)k image:(UIImage *)image;
{
// 检查图片大小是否超过64KB
NSData *imageData = UIImageJPEGRepresentation(image, 1.0); // 使用JPEG格式压缩
if (imageData.length > k * 1024) { // 64KB = 64 * 1024 bytes
CGFloat maxSize = k * 1024.0; // 最大64KB
CGFloat compression = 0.9f; // 初始压缩质量
while (imageData.length > maxSize && compression > 0.1f) {
compression -= 0.1f; // 逐步减小压缩质量
imageData = UIImageJPEGRepresentation(image, compression);
}
//image = [UIImage imageWithData:imageData]; // 重新创建图片
}
return imageData;
}
@end