应用程序因内存警告而崩溃

时间:2012-08-25 09:41:49

标签: ios uiimageview

我正在制作一个摄影应用程序,因为我正在为一个图像添加10个效果。我正在使用一些库。一段时间后应用程序崩溃了。它显示内存警告。我在压缩后使用图像,但仍然应用程序崩溃。谁能告诉我如何解决这个记忆警告。我正在使用以下代码进行图像压缩

-(UIImage *)resizeImage:(UIImage *)image {
int w = image.size.width;
int h = image.size.height; 

CGImageRef imageRef = [image CGImage];

int width, height;

int destWidth = 640;
int destHeight = 480;
if(w > h){
    width = destWidth;
    height = h*destWidth/w;
} else {
    height = destHeight;
    width = w*destHeight/h;
}

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

CGContextRef bitmap;
bitmap = CGBitmapContextCreate(NULL, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedFirst);

if (image.imageOrientation == UIImageOrientationLeft) {
    CGContextRotateCTM (bitmap, M_PI/2);
    CGContextTranslateCTM (bitmap, 0, -height);

} else if (image.imageOrientation == UIImageOrientationRight) {
    CGContextRotateCTM (bitmap, -M_PI/2);
    CGContextTranslateCTM (bitmap, -width, 0);

} else if (image.imageOrientation == UIImageOrientationUp) {

} else if (image.imageOrientation == UIImageOrientationDown) {
    CGContextTranslateCTM (bitmap, width,height);
    CGContextRotateCTM (bitmap, -M_PI);

}

CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage *result = [UIImage imageWithCGImage:ref];

CGContextRelease(bitmap);
CGImageRelease(ref);

return result;  
}

我正在使用那个GPUImage库,我添加了GPUImageView来显示图像。 如果有人有任何想法,请帮助我。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

您需要发布colorspace

CGColorSpaceRelease(colorspace);

这可能是泄漏的原因,但如果你多次拨打resizeImage,那么它可能是崩溃的原因