永遠(yuǎn)要記得,成功的決心遠(yuǎn)勝于任何東西?!獊啿薄ち挚?/h4>
今后會(huì)分享一些幾年來總結(jié)的小技巧,今天分享下使用模型保存圖片,文筆不好,大家見諒!
基本上每個(gè)頁面都有圖片,圖片加載總是伴隨著內(nèi)存溢出、內(nèi)存泄漏或者是內(nèi)存消耗過大的問題。在使用tableView 和collectionview的時(shí)候每次上下滑動(dòng)都會(huì)重復(fù)加載圖片,消耗內(nèi)存,占用資源。直接上代碼:(collectionview)
UICollectionViewCell?* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
[self setCellImage:cell indexPath:indexPath withDict:memuDict];
cell.backgroundColor =?[UIColor whiteColor];
return cell;
- (void) setCellImage:(UICollectionViewCell * ) cell indexPath:(NSIndexPath *) indexPath withDict:(NSDictionary *) dict {
GJYHome_groupList_Model * listModel = self.model_groupList[indexPath.row ];//self.model_groupList保存模型數(shù)組
if (menuList.image == nil) {
WS(weakSelf);
[GJYWebImageManager sd_webViewWithData:menuList.menuIcon placeholderImage:@"你選擇的占位圖" WiBlock:^(UIImage *k) {
[cell.image setImage:k];
[listModel setImage:UIImagePNGRepresentation(k)];
[weakSelf.model_groupList replaceObjectAtIndex:indexPath.row withObject:listModel];
} else {
[cell.image setImage: [UIImage imageWithData:listModel.imageData]];
}]; // 這是我自己封裝的一個(gè)圖片下載的類
}
模型層:
@interface GJYHome_groupList_Model : NSObject
/** 菜單項(xiàng)名稱 */
@property (nonatomic , copy) NSString * menuName;
@property (nonatomic , strong) NSData * imageData;
@end
中心思想就是使用模型保存圖片數(shù)據(jù),滾動(dòng)時(shí)加載圖片模型數(shù)據(jù),好了。不算完美,有什么問題歡迎大家聊聊!