使用SDWebImage加载多个图片内存崩溃的问题
生活随笔
收集整理的這篇文章主要介紹了
使用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加载多个图片内存崩溃的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: uestc oj 1002 解救小Q
- 下一篇: AGVs难在哪