由于您尚未发布 imgSprite 的声明,我假设它的类遵循 Cocoa 命名约定。
在:
CGImageRef imgRef = [imgSprite CGImage];
该方法(非 NARC1 方法)返回您不拥有的对象,因此您不应释放它。
在:
[imgView setImage:[UIImage imageWithCGImage:CGImageCreateWithImageInRect(imgRef, CGRectMake(column*width, line, width, height))]];
参数是表达式:
CGImageCreateWithImageInRect(imgRef, CGRectMake(column*width, line, width, height))
CGImageCreateWithImageInRect()(名称遵循创建规则2的函数)返回您确实拥有的图像,因此您应该释放它,你没有。
在:
CGImageRelease(imgRef);
您发布的图片不是您拥有的,因此您不应发布它。
你有两个问题:你(可能过度)释放imgRef,并且你正在泄露CGImageCreateWithImageInRect()返回的图像。
您应该改为:
// you do not own imgRef, hence you shouldn’t release it
CGImageRef imgRef = [imgSprite CGImage];
// use a variable for the return value of CGImageCreateWithImageInRect()
// because you own the return value, hence you should release it later
CGImageRef imgInRect = CGImageCreateWithImageInRect(imgRef, CGRectMake(column*width, line, width, height));
[imgView setImage:[UIImage imageWithCGImage:imgInRect]];
CGImageRelease(imgInRect);
您可能想阅读Memory Management Programming Guide 和Memory Management Programming Guide for Core Foundation。
1NARC = 新建、分配、保留、复制
2Create Rule 声明如果您调用名称包含 Create 或 Copy 的函数,则您拥有返回值,因此您应该在不再需要它时释放它。