在用到在一张图片中截取某一部分图片中,用到CGImageRef类中的CGImageCreateWithImageInRect函数,但是操作不当就会容易出现内存泄露。
- UIImage *imaRun = [UIImage imageNamed:@"run.png"];// 原图片 A
- for(int i = 0 ;i < 7;i++)
- {
- CGRect rect = CGRectMake( 41*i,0 , 41, 64);
- CGImageRef imageRef = CGImageCreateWithImageInRect([imaRun CGImage], rect);
- UIImage *subIma = [UIImage imageWithCGImage:imageRef];
- [arrayrun addObject:subIma];
像上面的例子 ,在一张图片中按照大小截取7部分图片,添加到一个数组中循环输出形成动画。但是在测试的时候出现了内存泄露。进入头文件查找到一个函数 CGImageRelease
释放内存的,但是不仔细的话就不会加上,添加上这个函数后内存泄露的问题就解决了代码如下:
- UIImage *imaRun = [UIImage imageNamed:@"run.png"];// 原图片 A
- for(int i = 0 ;i < 7;i++)
- {
- CGRect rect = CGRectMake( 41*i,0 , 41, 64);
- CGImageRef imageRef = CGImageCreateWithImageInRect([imaRun CGImage], rect);
- UIImage *subIma = [UIImage imageWithCGImage:imageRef];
- CGImageRelease(imageRef);
- [arrayrun addObject:subIma];
- }
- [imaRun release];