繪圖操作還原時內存暴增
- 目前在做繪圖板,每畫一筆需要生成一張圖片然后銷毀, 這時生成圖片的內存是沒有問題的, 但若用這些點的集合去在另一端進行還原操作,就會在1秒內調用多次畫筆去還原路徑生成圖片導致內存暴增,查閱后,
- 一是畫筆點的集合存儲過多,應進行限制
- 二是生成圖片時圖片沒有及時釋放導致內存暴增(本文解決這個問題)
問題描述
- 情景:短時間頻繁調用生成圖片方法(如1秒內調用10次)
- 問題:每調用一次,內存增加10M,由于arc自動管理內存,導致圖片沒有及時被釋放,內存瞬間飆到幾百兆至1G然后程序崩潰
- 其他: 若只調用一次,或調用不頻繁則不會導致內存暴增,arc會自動管理銷毀
- 解決辦法,手動嵌套一層autoreleasepool
原代碼->導致內存暴增
- (UIImage *)composeBrushToImage
{
UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, [UIScreen mainScreen].scale);
CGContextRef context = UIGraphicsGetCurrentContext();
[_composeView.layer renderInContext:context];
UIImage *getImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
_composeView.image = getImage;
return getImage;
}
解決后
- (UIImage *)composeBrushToImage
{
@autoreleasepool {
UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, [UIScreen mainScreen].scale);
CGContextRef context = UIGraphicsGetCurrentContext();
[_composeView.layer renderInContext:context];
UIImage *getImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
_composeView.image = getImage;
return getImage;
}
}
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。