图片下载,指定图片KB,XImgDownloader,


#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


©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容