2012-08-25 152 views
1

我正在为一个照片应用程序工作,因为我正在为一个图像添加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用于显示图像。 如果有人有任何想法,请帮助我。 在此先感谢。

+0

上面的代码只是为了调整一个图像。你从哪里得到这些图像?你一次有多少内存? GPUImage与此有什么关系?您需要为我们提供更多信息,甚至可以在这里帮助您。 –

+0

另外:**仪器**(从这里开始:http://developer.apple.com/library/ios/#technotes/tn2151/_index.html) – nielsbot

回答

1

你需要释放colorspace

CGColorSpaceRelease(colorspace); 

这可能是泄漏的原因,但如果你调用resizeImage很多次话,就可以崩溃的原因

+0

它可能是一个泄漏,但我怀疑颜色空间物体需要多少记忆 - 事实上,我是单身物体,用于幕后的常见色彩空间。 – nielsbot

相关问题