我的应用程序由于接收内存警告而崩溃。当我使用仪器运行应用程序时,我会遇到以下内存泄漏问题。
这是我这个方法的完整代码。
- (UIImage *)resizeImage:(UIImage *)orignalImage newWidth:(int)width newHeight:(int)height bgColor:(UIColor *)bgColor
{
CGRect newRect;
if((orignalImage.size.width>width) || (orignalImage.size.height>height))
{
float newWidth,newHeight;
if(orignalImage.size.width>orignalImage.size.height)
{
newWidth=width;
newHeight=(height*orignalImage.size.height)/orignalImage.size.width;
}
else if(orignalImage.size.height>orignalImage.size.width)
{
newWidth=(width*orignalImage.size.width)/orignalImage.size.height;
newHeight=height;
}
else
{
newWidth=width;
newHeight=height;
}
newRect.origin.x=(width-newWidth)/2;
newRect.origin.y=(height-newHeight)/2;
newRect.size.width=newWidth;
newRect.size.height=newHeight;
}
else
{
newRect.origin.x=(width-(orignalImage.size.width))/2;
newRect.origin.y=(height-(orignalImage.size.height))/2;
newRect.size.width=orignalImage.size.width;
newRect.size.height=orignalImage.size.height;
}
CGImageRef imageRef = [orignalImage CGImage];
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
//if (alphaInfo == kCGImageAlphaNone)
alphaInfo = kCGImageAlphaNoneSkipLast;
CGContextRef bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), 4 * width, CGImageGetColorSpace(imageRef), alphaInfo);
CGContextSetFillColorWithColor(bitmap, bgColor.CGColor);
CGContextFillRect(bitmap, CGRectMake(0, 0, width, height));
CGContextDrawImage(bitmap, newRect, imageRef);
CGImageRef newImgRef = CGBitmapContextCreateImage(bitmap);
UIImage *newImg = [UIImage imageWithCGImage:newImgRef];
CGContextRelease(bitmap);
CGImageRelease(newImgRef);
return newImg;
}
有人可以建议我代码中有什么错误。感谢任何帮助。