iphone - 读取视频帧 iPhone 时出现内存问题
<p><p>我在从 iPhone 库中选择的现有视频中读取视频帧时出现内存问题。首先,我将 UIImage-frames 本身添加到一个数组中,但一段时间后我认为该数组对于内存来说太大了,所以我将 UIImages 保存在文档文件夹中并将图像路径添加到数组中。但是,即使使用 Instruments 检查分配,我仍然会收到相同的内存警告。分配的总内存永远不会超过 2.5mb。也没有发现泄漏...有人能想到什么吗?</p>
<pre><code>-(void)addFrame:(UIImage *)image
{
NSString *imgPath = ;
;
;
frameCount++;
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
;
;
frameCount = 0;
// incoming video
NSURL *videoURL = ;
//NSLog(@"Video : %@", videoURL);
// AVURLAsset to read input movie (i.e. mov recorded to local storage)
NSDictionary *inputOptions = forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
AVURLAsset *inputAsset = [ initWithURL:videoURL options:inputOptions];
// Load the input asset tracks information
completionHandler: ^{
NSError *error = nil;
nrFrames = CMTimeGetSeconds() * 30;
NSLog(@"Total frames = %d", nrFrames);
// Check status of "tracks", make sure they were loaded
AVKeyValueStatus tracksStatus = ;
if (!tracksStatus == AVKeyValueStatusLoaded)
// failed to load
return;
/* Read video samples from input asset video track */
AVAssetReader *reader = ;
NSMutableDictionary *outputSettings = ;
forKey: (NSString*)kCVPixelBufferPixelFormatTypeKey];
AVAssetReaderTrackOutput *readerVideoTrackOutput = objectAtIndex:0] outputSettings:outputSettings];
// Assign the tracks to the reader and start to read
;
if ( == NO) {
// Handle error
NSLog(@"Error reading");
}
NSAutoreleasePool *pool = ;
while (reader.status == AVAssetReaderStatusReading)
{
if(!memoryProblem)
{
CMSampleBufferRef sampleBufferRef = ;
if (sampleBufferRef)
{
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBufferRef);
/*Lock the image buffer*/
CVPixelBufferLockBaseAddress(imageBuffer,0);
/*Get information about the image*/
uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
/*We unlock theimage buffer*/
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
/*Create a CGImageRef from the CVImageBufferRef*/
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGImageRef newImage = CGBitmapContextCreateImage(newContext);
/*We release some components*/
CGContextRelease(newContext);
CGColorSpaceRelease(colorSpace);
UIImage *image= .scale orientation:UIImageOrientationRight];
//;
;
/*We release the CGImageRef*/
CGImageRelease(newImage);
CMSampleBufferInvalidate(sampleBufferRef);
CFRelease(sampleBufferRef);
sampleBufferRef = NULL;
}
}
else
{
break;
}
}
;
NSLog(@"Finished");
}];
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>你做一件事然后尝试。</p>
<p>将 <code>NSAutoreleasePool</code> 移动到 <code>while</code> 循环内并在循环内排出。</p>
<p>所以它会是这样的:</p>
<pre><code>while (reader.status == AVAssetReaderStatusReading)
{
NSAutoreleasePool *pool = ;
.....
;
}
</code></pre></p>
<p style="font-size: 20px;">关于iphone - 读取视频帧 iPhone 时出现内存问题,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/9887527/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/9887527/
</a>
</p>
页:
[1]