ios 下CGImageCreateWithImageInRect 内存泄露

在用到在一张图片中截取某一部分图片中,用到CGImageRef类中的CGImageCreateWithImageInRect函数,但是操作不当就会容易出现内存泄露。
[html]  view plain copy
  1. UIImage *imaRun = [UIImage imageNamed:@"run.png"];// 原图片 A  
  2.    for(int  i = 0 ;i < 7;i++)  
  3.    {  
  4.        CGRect rect = CGRectMake( 41*i,0 , 41, 64);  
  5.        CGImageRef imageRef = CGImageCreateWithImageInRect([imaRun CGImage], rect);  
  6.          
  7.        UIImage *subIma = [UIImage imageWithCGImage:imageRef];  
[html]  view plain copy
  1. [arrayrun addObject:subIma];  


像上面的例子 ,在一张图片中按照大小截取7部分图片,添加到一个数组中循环输出形成动画。但是在测试的时候出现了内存泄露。进入头文件查找到一个函数 CGImageRelease

释放内存的,但是不仔细的话就不会加上,添加上这个函数后内存泄露的问题就解决了代码如下:

[html]  view plain copy
  1. UIImage *imaRun = [UIImage imageNamed:@"run.png"];// 原图片 A  
  2.     for(int  i = 0 ;i < 7;i++)  
  3.     {  
  4.         CGRect rect = CGRectMake( 41*i,0 , 41, 64);  
  5.         CGImageRef imageRef = CGImageCreateWithImageInRect([imaRun CGImage], rect);  
  6.           
  7.         UIImage *subIma = [UIImage imageWithCGImage:imageRef];  
  8.         CGImageRelease(imageRef);  
  9.         [arrayrun addObject:subIma];  
  10.     }  
  11.     [imaRun release];  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值