Objective C,iOS - 收到内存警告

发布于 2024-12-08 21:37:11 字数 1167 浏览 1 评论 0原文

我目前正在开发一个 iOS 项目,该项目取得了良好的进展,但是整个内存问题 在 iOS 上无法正常工作。

iPhone 摄像头记录一个流。我有一个通过队列执行的捕获方法。 摄像机图像转换为灰度图像。它运作良好,但一段时间后会给出一些 内存警告并关闭应用程序,因为内存不足。

我发现这里的错误源

CGColorSpaceRef colorSpaceGray = CGColorSpaceCreateDeviceGray();
CGContextRef newContextGray = CGBitmapContextCreate(baseAddressGray, width, height, 8, width, colorSpaceGray, kCGImageAlphaNone);

CGImageRef GrayImage = CGBitmapContextCreateImage(newContextGray);


UIImage *img= [UIImage imageWithCGImage:GrayImage scale:1.0 orientation:UIImageOrientationRight];
[self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:img waitUntilDone:NO];



free(baseAddressGray);
CGColorSpaceRelease(colorSpaceGray);
CGContextRelease(newContextGray);


CVPixelBufferUnlockBaseAddress(imageBuffer,0);

全部位于自动释放池中,该池随后被耗尽。 根据我的理解,崩溃源所在的行是

CGContextRef newContextGray = CGBitmapContextCreate(baseAddressGray, width, height, 8, width, colorSpaceGray, kCGImageAlphaNone);

,这里应该没有内存问题,因为

CGColorSpaceRelease(colorSpaceGray);
CGContextRelease(newContextGray);

已被释放。

我在这里做错了什么,或者缺少什么?

i am currently working on an iOS project which makes good progress, but the whole memory thing
on iOS is not working properly.

The iphone camera records a a stream. I have a capture method which is executed via a queue.
The camer image is converted to greyscale. It works well but after a while gives some
memory warning and closes the app, because its out of memory.

I have spotted the error source to here

CGColorSpaceRef colorSpaceGray = CGColorSpaceCreateDeviceGray();
CGContextRef newContextGray = CGBitmapContextCreate(baseAddressGray, width, height, 8, width, colorSpaceGray, kCGImageAlphaNone);

CGImageRef GrayImage = CGBitmapContextCreateImage(newContextGray);


UIImage *img= [UIImage imageWithCGImage:GrayImage scale:1.0 orientation:UIImageOrientationRight];
[self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:img waitUntilDone:NO];



free(baseAddressGray);
CGColorSpaceRelease(colorSpaceGray);
CGContextRelease(newContextGray);


CVPixelBufferUnlockBaseAddress(imageBuffer,0);

All lies inside an autorelease pool which is drained afterwards.
The line which is th source for the crash is

CGContextRef newContextGray = CGBitmapContextCreate(baseAddressGray, width, height, 8, width, colorSpaceGray, kCGImageAlphaNone);

From my understanding there should be no memory problem here, because

CGColorSpaceRelease(colorSpaceGray);
CGContextRelease(newContextGray);

are released.

What am i doing wrong here, or what is missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

記憶穿過時間隧道 2024-12-15 21:37:11

您没有在代码示例的第 4 行中释放 GrayImage

You are not releasing GrayImage in line 4 of your code sample.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文