我正在使用 aVFoundation 框架从视频资产中读取 CMSampleBufferRef,在一段时间循环中,我得到如下图像:
-(void)splitImagesFromVideo
{
if (images != nil) {
[images release];
images = nil;
}
images =[[NSMutableArray alloc] init];
NSURL *fileURL = [[NSBundle mainBundle]
URLForResource:@"3idiots" withExtension:@"mov"];
NSLog(@"video: %@",videoURL);
AVAsset *theAVAsset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
NSError *error = nil;
float width = theAVAsset.naturalSize.width;
float height = theAVAsset.naturalSize.height;
AVAssetReader *mAssetReader = [[AVAssetReader alloc] initWithAsset:theAVAsset error:&error];
NSArray *videoTracks = [theAVAsset tracksWithMediaType:AVMediaTypeVideo];
AVAssetTrack *videoTrack = [videoTracks objectAtIndex:0];
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey];
AVAssetReaderTrackOutput* mAssetReaderOutput = [[AVAssetReaderTrackOutput alloc] initWithTrack:videoTrack outputSettings:options];
[mAssetReader addOutput:mAssetReaderOutput];
BOOL success = [mAssetReader startReading];
NSInteger val = 1;
while ( [mAssetReader status]==AVAssetReaderStatusReading ){
CMSampleBufferRef buffer = [mAssetReaderOutput copyNextSampleBuffer];//read next image.
if (buffer) {
UIImage *img = [self imageFromSampleBuffer:buffer];
if (img != nil) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
img = [self imageOfSize:CGSizeMake(320, 480) fromImage:img];
[images addObject:img];
[pool release];
}
CMSampleBufferInvalidate(buffer);
CFRelease(buffer);
buffer = nil;
}
NSLog(@"value: %d", val++);
}
[images removeLastObject];
NSLog(@"img count: %d", [images count]);
NSLog(@"imgs: %@",images);
[theAVAsset release];
[mAssetReader release];
[mAssetReaderOutput release];
NSLog(@"count: %d", [images count]);
}
当 while 循环正在执行时,它会打印我输入的日志,并增加 integer val
。在这之间有时会在 GDB 中显示一条消息“收到内存警告”。
非常感谢...