我使用captureOutput:didOutputSampleBuffer:fromConnection:.中的CIContext、CIDetector和CIImage来检测从样本导出的vImage_Buffer中的矩形看起来,无论是检测器还是CIImage都保留着内存,并且无法释放。下面是有问题的代码--跳过这段代码显示内存保持不变,否则会增加,直到应用程序崩溃:
// ...rotatedBuf and format managed outside scope
// Use a CIDetector to find any potential subslices to process
CVPixelBufferRef cvBuffer;
vImageCVImageFormatRef cvFormat = vImageCVImageFormat_CreateWithCVPixelBuffer(pixelBuffer);
CVPixelBufferCreate(kCFAllocatorSystemDefault, rotatedBuf.width, rotatedBuf.height, kCVPixelFormatType_32BGRA, NULL, &cvBuffer);
CVPixelBufferLockBaseAddress(cvBuffer, kCVPixelBufferLock_ReadOnly);
err = vImageBuffer_CopyToCVPixelBuffer(&rotatedBuf, &format, cvBuffer, cvFormat, NULL, kvImageNoFlags);
CVPixelBufferUnlockBaseAddress(cvBuffer, kCVPixelBufferLock_ReadOnly);
if (![self vImageDidError:err]) {
CIImage *ciImage = [CIImage imageWithCVPixelBuffer:cvBuffer];
NSArray *feats = [self.ciDetector featuresInImage:ciImage options:nil];
if (feats && [feats count]) {
for (CIFeature *feat in feats) {
// The frame is currently in image space, so we must convert it to a unitless space like the other rects.
CGRect frame = feat.bounds;
CGRect clip = CGRectMake(frame.origin.x / rotatedBuf.width, frame.origin.y / rotatedBuf.height,
frame.size.width / rotatedBuf.width, frame.size.height / rotatedBuf.height);
rects = [rects arrayByAddingObject:[NSValue valueWithCGRect:clip]];
}
}
}
CVPixelBufferRelease(cvBuffer);
vImageCVImageFormat_Release(cvFormat);其他答案似乎建议在一个自动释放池中包装,或者每个帧创建一个新的CIDector,但这两种方法都不会影响内存的使用。
CIDetector isn't releasing memory
CIDetector won't release memory - swift
编辑:将调度队列切换到dispatch_main_queue以外的其他队列似乎清除了内存问题,并保持了UI响应。
发布于 2016-10-20 04:57:19
我想出了一个不同的解决方案--在我的例子中,我在主调度队列上运行我所有的处理程序。解决这种情况的方法是创建一个新队列来运行处理。我意识到,当我的大部分CPU时间都花在了对featuresInImage:options的调用上时,情况可能是这样的。它没有解释是什么导致了内存问题,但是现在我在一个单独的队列中运行,内存是很好的和恒定的。
https://stackoverflow.com/questions/40142462
复制相似问题