2014-02-11 29 views
0

我正在使用此功能裁剪图像,当我使用它时,它不释放内存,我试图不使用它,内存工作正常。我不能似乎与it.Please帮助内存问题我创建了IOS 7

- (void) processImage:(UIImage *)image { 
haveImage = YES; 

UIGraphicsBeginImageContext(image.size); 
[image drawInRect: CGRectMake(0, 0, image.size.width, image.size.height)]; 


__weak UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 

CGRect cropRect = CGRectMake(0, 0, image.size.width, image.size.height - ((image.size.height - image.size.width)/2)); 
CGImageRef imageRef = CGImageCreateWithImageInRect([smallImage CGImage], cropRect); 

__weak UIImage *corteSuperior =[UIImage imageWithCGImage:imageRef]; 

cropRect = CGRectMake(0, ((image.size.height - image.size.width)/2), corteSuperior.size.width, corteSuperior.size.height - ((image.size.height - image.size.width)/2)); 
imageRef = CGImageCreateWithImageInRect([corteSuperior CGImage], cropRect); 



self.imagenCamara.image = [UIImage imageWithCGImage:imageRef]; 



smallImage = nil; 
imageRef = nil; 
corteSuperior = nil; 

UIGraphicsEndImageContext(); 
CGImageRelease(imageRef); 

} 
+0

对于其他提示,看看我的答案http://stackoverflow.com/questions/19042819/creating-image-with-in-the-frame-of-a-rectangle-placed-on-a-uiimageview – iPatel

+0

顺便说一句,你有一个调用'UIGraphicsBeginImageContext'和两个调用'UIGraphicsEndImageContext'。您可能想要删除第二个电话。 – Rob

回答

2

你必须调用CGImageRelease呼吁各CGImageCreateWithImageInRect打电话找问题。你永远不会释放第一次出现。在第二次分配imageRef之前,请释放第一个imageRef

另外,在最后,请确保您在执行CGImageRelease之前nilimageRef指针。通过将imageRef设置为nil,您将失去对imageRef的引用,因此CGImageRelease将无法​​发布任何内容。

+2

提示:如果您使用XCode静态分析仪,它应该向您显示这些类型的错误! – Vinzzz

+0

谢谢你,罗伯,我纠正它,工作正常,非常感谢! – user3110923