iOS - AVAsset的内存问题

时间:2014-03-07 09:31:29

标签: ios objective-c memory avasset

我的应用程序就像Vine或Instagram,它记录小剪辑并将它们连接在一起。创建avasset时我的内存管理有问题。 如果我从头开始创建100个AVAsset对象,它工作正常。但是,如果我继续删除其中的一些,甚至所有这些,并再次创建新的,我会收到内存警告,并且还会对内存压力产生影响。我用仪器检查我正在运行的应用程序,并且没有泄漏的内存。 下面是我在数组资产中创建和删除AVAsset对象的方法。有谁知道可能是什么问题?

- (void) copyFileToDocuments:(NSURL *)fileURL
{
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd_HH-mm-ss"];
NSString *destinationPath = [documentsDirectory stringByAppendingFormat:@"/output_%@.mov", [dateFormatter stringFromDate:[NSDate date]]];

NSError *error;
if (![[NSFileManager defaultManager] copyItemAtURL:fileURL toURL:[NSURL fileURLWithPath:destinationPath] error:&error]) {
    if ([[self delegate] respondsToSelector:@selector(captureManager:didFailWithError:)]) {
        [[self delegate] captureManager:self didFailWithError:error];
    }
}

    //add asset into the array or pieces
    AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:destinationPath]];
    [self.assets addObject:asset];
}

-(void) deleteLastAsset
{
    AVAsset *asset = [self.assets lastObject];

    [self.delegate removeTimeFromDuration:CMTimeGetSeconds(asset.duration)];

    NSURL *fileURL = nil;
    if ([asset isKindOfClass:AVURLAsset.class])
    {
        AVURLAsset *urlAsset = (AVURLAsset*)asset;
        fileURL = urlAsset.URL;
    }

    if (fileURL)
        [self removeFile:fileURL];

    [self.assets removeLastObject];
}

- (void) startRecording
{
    if ([[UIDevice currentDevice] isMultitaskingSupported]) {
        [self setBackgroundRecordingID:[[UIApplication sharedApplication]         beginBackgroundTaskWithExpirationHandler:^{}]];
    }
    [self removeFile:[[self recorder] outputFileURL]];
    [[self recorder] startRecordingWithOrientation:self.orientation];
}


- (void) removeFile:(NSURL *)fileURL
{
    NSString *filePath = [fileURL path];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath]) {
        NSError *error;
        if ([fileManager removeItemAtPath:filePath error:&error] == NO) {
            if ([[self delegate] respondsToSelector:@selector(captureManager:didFailWithError:)]) {
                [[self delegate] captureManager:self didFailWithError:error];
            }            
        }
    }
}


-(void)startRecordingWithOrientation:(AVCaptureVideoOrientation)videoOrientation;
{
    AVCaptureConnection *videoConnection = [AVCamUtilities connectionWithMediaType:AVMediaTypeVideo         fromConnections:[[self movieFileOutput] connections]];
    if ([videoConnection isVideoOrientationSupported])
        [videoConnection setVideoOrientation:AVCaptureVideoOrientationPortrait];

    [[self movieFileOutput] startRecordingToOutputFileURL:[self outputFileURL] recordingDelegate:self];
}

-(void)stopRecording
{
    [[self movieFileOutput] stopRecording];
}

0 个答案:

没有答案