8

次のように 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];リリースされていないようです

コードにメモリ リークがありますか?

ここに画像の説明を入力

ここに画像の説明を入力

4

2 に答える 2

4

私は同じ問題に遭遇しましたが、CIDetector を再利用するバグ (または、キャッシュの目的で設計されている可能性があります) のようです。

CIDetector を再利用せず、代わりに必要に応じてインスタンスを作成し、検出が完了したときにそれを解放する (または、ARC 用語では、参照を保持しない) ことで回避できました。これを行うにはいくらかのコストがかかりますが、あなたが言ったようにバックグラウンド スレッドで検出を行っている場合、無制限のメモリの増加と比較すると、そのコストはおそらく価値があります。

おそらくより良い解決策は、複数の画像を連続して検出する場合、1 つの検出器を作成してすべてに使用することです (または、成長が大きすぎる場合は、N 個の画像ごとに新しいものを解放して作成します。 N がどうあるべきかを確認するために実験する必要があります)。

この問題について Apple に Radar バグを報告しました: http://openradar.appspot.com/radar?id=6645353252126720

于 2014-02-13T20:01:55.873 に答える