iOS用模型保存圖片

永遠(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ù),好了。不算完美,有什么問題歡迎大家聊聊!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容