圖片下載,指定圖片KB,XImgDownloader,


#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface XImgDownloader : NSObject

/**
 *  SD  下載
 *  入?yún)?網(wǎng)絡(luò)圖片地址
 *  返回  圖片 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; // 初始?jí)嚎s質(zhì)量
        while (imageData.length > maxSize && compression > 0.1f) {
            compression -= 0.1f; // 逐步減小壓縮質(zhì)量
            imageData = UIImageJPEGRepresentation(image, compression);
        }
        image = [UIImage imageWithData:imageData]; // 重新創(chuàng)建圖片
    }
    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; // 初始?jí)嚎s質(zhì)量
        while (imageData.length > maxSize && compression > 0.1f) {
            compression -= 0.1f; // 逐步減小壓縮質(zhì)量
            imageData = UIImageJPEGRepresentation(image, compression);
        }
        //image = [UIImage imageWithData:imageData]; // 重新創(chuàng)建圖片
    }
    return imageData;
}


@end


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容