第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

SDWebImage加載多個圖片內(nèi)存崩潰的問題

標(biāo)簽:
區(qū)塊鏈

SDWebImage大家肯定都恨熟悉了,国内外太多的App使用其进行图片加载。

但是最近在使用过程中发现,我用SDWebImage加载多个图片,类似微博动态那种,在加载的过程中。我发现当图片分辨率比较大的时候(不是图片大),加载几张图片就崩溃了。

网上说可以每次加载图片清空memcache,但是效果并不好。

 [[SDImageCache sharedImageCache] setValue:nil forKey:@"memCache"];


也有说把使用下面这个方法的地方全部注掉

+ (UIImage *)decodedImageWithImage:(UIImage *)image 

但是效果并不明显。同时加载5-7张高分辨率图片还是会立即崩溃

我们使用SDWebimage肯定都会做三件事,一判断本地是否有这张图,二有的时候直接从本地取图片,三没有的时候去网络下载。

大概是像下面这样


NSString *logoString = [_currentDic stringValueForKey:@"team_img"];if(logoString.length>0){[[SDImageCache sharedImageCache] queryDiskCacheForKey:logoString done:^(UIImage *image, SDImageCacheType cacheType) {if (image) {[_teamImage setImage:image];}else{[_teamImage sd_setImageWithURL:kNSUrl(logoString)placeholderImage:IMGNAMED(@"defaultAvatar2")options:SDWebImageRefreshCachedcompleted:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {if (image) {[[SDImageCache sharedImageCache] storeImage:image forKey:logoString toDisk:YES];}}];}}];}


在内部都会使用到下面这个方法


- (UIImage *)diskImageForKey:(NSString *)key {NSData *data = [self diskImageDataBySearchingAllPathsForKey:key];if (data) {UIImage *image = [UIImage sd_imageWithData:data];image = [self scaledImageForKey:key image:image];image = [UIImage decodedImageWithImage:image];return image;}else {return nil;}}


我发现这里

 UIImage *image = [UIImage sd_imageWithData:data];

图片取出来的时候就已经巨大无比,占用了很大的内存,导致内存来不及释放就崩溃。

抽丝剥茧我们进入

sd_imageWithData方法

发现这里面对图片的处理是直接按照原大小进行的,如果几千是分辨率这里导致占用了大量内存。


所以我们需要在这里对图片做一次等比的压缩。

我们在

UIImage+MultiFormat这个类里面添加如下压缩方法,

+(UIImage *)compressImageWith:(UIImage *)image{float imageWidth = image.size.width;float imageHeight = image.size.height;float width = 640;float height = image.size.height/(image.size.width/width);float widthScale = imageWidth /width;float heightScale = imageHeight /height;// 创建一个bitmap的context// 并把它设置成为当前正在使用的contextUIGraphicsBeginImageContext(CGSizeMake(width, height));if (widthScale > heightScale) {[image drawInRect:CGRectMake(0, 0, imageWidth /heightScale , height)];}else {[image drawInRect:CGRectMake(0, 0, width , imageHeight /widthScale)];}// 从当前context中创建一个改变大小后的图片UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();// 使当前的context出堆栈UIGraphicsEndImageContext();return newImage;}

再在上面箭头代码后面对图片进行压缩

#ifdef SD_WEBPelse if ([imageContentType isEqualToString:@"image/webp"]){image = [UIImage sd_imageWithWebPData:data];}#endifelse {image = [[UIImage alloc] initWithData:data];if (data.length/1024 > 128) {image = [self compressImageWith:image];}UIImageOrientation orientation = [self sd_imageOrientationFromImageData:data];if (orientation != UIImageOrientationUp) {image = [UIImage imageWithCGImage:image.CGImagescale:image.scaleorientation:orientation];}

到了这里还需要进行最后一步。就是在SDWebImageDownloaderOperation的connectionDidFinishLoading方法里面的:


UIImage *image = [UIImage sd_imageWithData:self.imageData];

//将等比压缩过的image在赋在转成data赋给self.imageData
NSData *data = UIImageJPEGRepresentation(image, 1);
self.imageData = [NSMutableData dataWithData:data];

   再配合    [[SDImageCache sharedImageCache] setValue:nil forKey:@"memCache"];(图片加载后使用)大功告成,亲测内存基本变化不大,自动释放也来得及。



點擊查看更多內(nèi)容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評論
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學(xué)

大額優(yōu)惠券免費領(lǐng)

立即參與 放棄機會
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報

0/150
提交
取消