日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用SDWebImage加载多个图片内存崩溃的问题

發布時間:2024/1/18 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用SDWebImage加载多个图片内存崩溃的问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用SDWebImage加載多個圖片時,在加載的過程中,當圖片分辨率比較大的時候,加載幾張圖片就崩潰了。需要對圖片進行處理,避免內存崩潰問題。

一、預加載圖片URL數組

預加載URL數組

[[SDWebImagePrefetcher sharedImagePrefetcher] prefetchURLs:array progress:^(NSUInteger noOfFinishedUrls, NSUInteger noOfTotalUrls) {} completed:^(NSUInteger noOfFinishedUrls, NSUInteger noOfSkippedUrls) {}];

二、獲取圖片數組

NSMutableArray *imageArray = [NSMutableArray arrayWithArray:array]; for (NSInteger i = 0 ; i < array.count ; i++) {NSString *imageStr = array[i];NSURL *imgUrl = [NSURL URLWithString:imageStr];// 根據URL獲取key值NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:imgUrl];if (key.length) { // UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:key];// 獲取緩存中圖片dataNSData *data = [[SDImageCache sharedImageCache] diskImageDataForKey:key];UIImage *image = nil;CGFloat imgMaxSide = [UIScreen mainScreen].scale * 100;// 判斷圖片大小if (data.length > imgMaxSide * imgMaxSide * 4) {// 獲取圖片緩存路徑NSString *cacheImagePath = [[SDImageCache sharedImageCache] cachePathForKey:key];if (cacheImagePath.length) {// 壓縮圖片image = [self thumbImageFromLargeFile:cacheImagePath withConfirmedMaxPixelSize:100];}} else {image = [UIImage imageWithData:data];}// 替換數組中的圖片if (image == nil) {imageArray[i] = [UIImage imageNamed:@"default"];}else{imageArray[i] = image;}} }

三、獲取圖片縮略圖

/// 獲取圖片的縮略圖 /// @param filePath 圖片本地路徑 /// @param maxPixelSize 圖片最大像素寬度 - (UIImage *)thumbImageFromLargeFile:(NSString *)filePath withConfirmedMaxPixelSize:(CGFloat)maxPixelSize {// Create the image source (from path)CGImageSourceRef src = CGImageSourceCreateWithURL((__bridge CFURLRef) [NSURL fileURLWithPath:filePath], NULL);// Create thumbnail optionsCFDictionaryRef options = (__bridge CFDictionaryRef) @{(id) kCGImageSourceCreateThumbnailWithTransform : @YES,(id) kCGImageSourceCreateThumbnailFromImageAlways : @YES,(id) kCGImageSourceThumbnailMaxPixelSize : @(maxPixelSize)};// Generate the thumbnailCGImageRef thumbnail = CGImageSourceCreateThumbnailAtIndex(src, 0, options);CFRelease(src);UIImage *image = [[UIImage alloc] initWithCGImage:thumbnail];CFRelease(thumbnail);return image; }

總結

以上是生活随笔為你收集整理的使用SDWebImage加载多个图片内存崩溃的问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。