Reputation: 2778
I am reading CMSampleBufferRef from video asset using aVFoundation framework, In a while loop I am getting those image like below:
-(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 the while loop is executing it prints the log I put with incrementing the integer val
.
and in between this some times a message "Received Memory Warning" is displayed in GDB.
Thanks a lot...
Upvotes: 2
Views: 1886
Reputation: 121
Move your
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
line to just inside the while() loop, and put a
[pool drain];
just before the end of the while loop.
Also, depending on how many images you have, you'll run out of memory because you're keeping the images you're scaling down in memory...
Upvotes: 0
Reputation: 442
Sounds like you need to look into wrapping each iteration of your while loop in an autorelease pool https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html
Upvotes: 2