使用SDWebImage加载多个图片内存崩溃的问题
生活随笔
收集整理的這篇文章主要介紹了
使用SDWebImage加载多个图片内存崩溃的问题
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
使用SDWebImage加載多個(gè)圖片時(shí),在加載的過(guò)程中,當(dāng)圖片分辨率比較大的時(shí)候,加載幾張圖片就崩潰了。需要對(duì)圖片進(jìn)行處理,避免內(nèi)存崩潰問(wèn)題。
一、預(yù)加載圖片URL數(shù)組
預(yù)加載URL數(shù)組
[[SDWebImagePrefetcher sharedImagePrefetcher] prefetchURLs:array progress:^(NSUInteger noOfFinishedUrls, NSUInteger noOfTotalUrls) {} completed:^(NSUInteger noOfFinishedUrls, NSUInteger noOfSkippedUrls) {}];二、獲取圖片數(shù)組
NSMutableArray *imageArray = [NSMutableArray arrayWithArray:array]; for (NSInteger i = 0 ; i < array.count ; i++) {NSString *imageStr = array[i];NSURL *imgUrl = [NSURL URLWithString:imageStr];// 根據(jù)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];}// 替換數(shù)組中的圖片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; }總結(jié)
以上是生活随笔為你收集整理的使用SDWebImage加载多个图片内存崩溃的问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: uestc oj 1002 解救小Q
- 下一篇: AGVs难在哪