gpt4 book ai didi

objective-c - CIDetector 没有释放内存

转载 作者:太空狗 更新时间:2023-10-30 03:37:47 24 4
gpt4 key购买 nike

我多次使用 CIDetector 如下:

    -(NSArray *)detect:(UIImage *)inimage
{
UIImage *inputimage = inimage;
UIImageOrientation exifOrientation = inimage.imageOrientation;
NSNumber *orientation = [NSNumber numberWithInt:exifOrientation];

NSDictionary *imageOptions = [NSDictionary dictionaryWithObject:orientation forKey:CIDetectorImageOrientation];
CIImage* ciimage = [CIImage imageWithCGImage:inputimage.CGImage options:imageOptions];


NSDictionary *detectorOptions = [NSDictionary dictionaryWithObject:orientation forKey:CIDetectorImageOrientation];

NSArray* features = [self.detector featuresInImage:ciimage options:detectorOptions];

if (features.count == 0)
{
PXLog(@"no face found");
}

ciimage = nil;
NSMutableArray *returnArray = [NSMutableArray new];


for(CIFaceFeature *feature in features)
{
CGRect rect = feature.bounds;
CGRect r = CGRectMake(rect.origin.x,inputimage.size.height - rect.origin.y - rect.size.height,rect.size.width,rect.size.height);

FaceFeatures * ff = [[FaceFeatures new] initWithLeftEye:CGPointMake(feature.leftEyePosition.x, inputimage.size.height - feature.leftEyePosition.y )
rightEye:CGPointMake(feature.rightEyePosition.x, inputimage.size.height - feature.rightEyePosition.y )
mouth:CGPointMake(feature.mouthPosition.x, inputimage.size.height - feature.mouthPosition.y )];

Face *ob = [[Face new] initFaceInRect:r withFaceFeatures:ff] ;


[returnArray addObject:ob];
}

features = nil;
return returnArray;
}

-(CIContext*) context{
if(!_context){
_context = [CIContext contextWithOptions:nil];
}
return _context;
}
-(CIDetector *)detector
{
if (!_detector)
{
// 1 for high 0 for low
#warning not checking for fast/slow detection operation
NSString *str = @"fast";//[SettingsFunctions retrieveFromUserDefaults:@"face_detection_accuracy"];


if ([str isEqualToString:@"slow"])
{
//DDLogInfo(@"faceDetection: -I- Setting accuracy to high");
_detector = [CIDetector detectorOfType:CIDetectorTypeFace context:nil
options:[NSDictionary dictionaryWithObject:CIDetectorAccuracyHigh forKey:CIDetectorAccuracy]];
} else {
//DDLogInfo(@"faceDetection: -I- Setting accuracy to low");
_detector = [CIDetector detectorOfType:CIDetectorTypeFace context:nil
options:[NSDictionary dictionaryWithObject:CIDetectorAccuracyLow forKey:CIDetectorAccuracy]];
}

}
return _detector;
}

但是在遇到各种内存问题之后,根据 Instruments 看来 NSArray* features = [self.detector featuresInImage:ciimage options:detectorOptions]; 没有被释放

我的代码中是否存在内存泄漏?

enter image description here

enter image description here

最佳答案

我遇到了同样的问题,它似乎是重用 CIDetector 的错误(或者可能是出于缓存目的而设计的)。

我能够通过不重用 CIDetector 来绕过它,而是根据需要实例化一个,然后在检测完成时释放它(或者,用 ARC 术语来说,只是不保留引用)。这样做会产生一些成本,但是如果您像您所说的那样在后台线程上进行检测,那么与无限制的内存增长相比,这个成本可能是值得的。

也许更好的解决方案是,如果您连续检测多张图像,创建一个检测器,将其用于所有检测器(或者,如果增长太大,每 N 张图像发布并创建一个新检测器。您必须通过实验来确定 N 应该是多少)。

我已经向 Apple 提交了关于此问题的 Radar 错误:http://openradar.appspot.com/radar?id=6645353252126720

关于objective-c - CIDetector 没有释放内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19156330/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com