UIImage imageNamed引起的内存问题2

UIImage imageNamed引起的内存问题

  126人阅读  评论(0)  收藏  举报

前段时间是遇到莫名其妙的崩最后于找出来是什么鬼原因 

1 [UIImage imageNamed:];

存了多的大致内存用尽最后崩最后解决问题的方法如下 
首先只存减小了大小的然后需要用到大从直接取不 
 
很明个方法不够好几天以后是无声无息的崩经过多次的排除了其他代 
的内存泄露等问题再看console里面一堆系内存警告然后退出了后台程知道用挂了 
 
所以很明+imageNamed个方法直太异了即使什么清空存什么的估也不管用 
 
并不是什么难题如果你干脆放弃存的苹果的例子代中有么一个函数足可使用函数的注的很清楚 

复制代码
- (UIImage *)tileForScale:(CGFloat)scale row:( int)row col:( int)col{
      //  we use "imageWithContentsOfFile:" instead of "imageNamed:" here because we don't want UIImage to cache our tiles
    NSString *tileName = [NSString stringWithFormat: @" %@_%d_%d_%d ", imageName, ( int)(scale *  1000), col, row];
    NSString *path = [[NSBundle mainBundle] pathForResource:tileName ofType: @" png "]; 
    UIImage *image = [UIImage imageWithContentsOfFile:path];
     return image;
}
复制代码

或者你非要比如我可以用一个NSMutableDictionary 

复制代码
 1 - (UIImage*)thumbnailImage:(NSString*)fileName
 2 { 
 3    UIImage *thumbnail = [thumbnailCache objectForKey:fileName
 4     if (nil == thumbnail)
 5    {
 6       NSString *thumbnailFile = [NSString stringWithFormat: @" %@/thumbnails/%@.jpg ", [[NSBundle mainBundle] resourcePath], fileName];
 7       thumbnail = [UIImage imageWithContentsOfFile:thumbnailFile];
 8       [thumbnailCache setObject:thumbnail forKey:fileName];
 9    }
10     return thumbnail;
11 }
复制代码

 
如果遇到内存低的警告只要

1 [thumbnailCache removeAllObjects];
OK  

 
所以如何在有大量片的情况下千万不要使用

1 [UIImage imageNamed];
异的方法了 你可以 试试 上面的方法 希望 你有帮助

转载于:https://www.cnblogs.com/zsw-1993/archive/2012/10/12/4880806.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值