gpt4 book ai didi

ios - 当我从图像创建视频时收到内存警告

转载 作者:行者123 更新时间:2023-11-29 12:07:43 24 4
gpt4 key购买 nike

我正在使用从图像创建视频,它工作得很好,但不幸的是,应用程序在 iPhone 4s 中崩溃了。请查看我的代码并提供您的建议。

- (void)createMovieFromImages:(NSArray *)images withCompletion:(CEMovieMakerCompletion)completion;
{
self.completionBlock = completion;

[self.assetWriter startWriting];
[self.assetWriter startSessionAtSourceTime:kCMTimeZero];

dispatch_queue_t mediaInputQueue = dispatch_queue_create("mediaInputQueue", NULL);

__block NSInteger i = 0;

NSInteger frameNumber = [images count];

[self.writerInput requestMediaDataWhenReadyOnQueue:mediaInputQueue usingBlock:^{
while (YES){
if (i >= frameNumber) {
break;
}
if ([self.writerInput isReadyForMoreMediaData]) {

CVPixelBufferRef sampleBuffer = [self newPixelBufferFromCGImage:[[images objectAtIndex:i] CGImage]];

if (sampleBuffer) {
if (i == 0) {
[self.bufferAdapter appendPixelBuffer:sampleBuffer withPresentationTime:kCMTimeZero];
}else{
CMTime lastTime = CMTimeMake(i-1, self.frameTime.timescale);
CMTime presentTime = CMTimeAdd(lastTime, self.frameTime);
[self.bufferAdapter appendPixelBuffer:sampleBuffer withPresentationTime:presentTime];
}
CFRelease(sampleBuffer);
i++;
}
}
}

[self.writerInput markAsFinished];
[self.assetWriter finishWritingWithCompletionHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
self.completionBlock(self.fileURL);
});
}];

CVPixelBufferPoolRelease(self.bufferAdapter.pixelBufferPool);
}];
}


- (CVPixelBufferRef)newPixelBufferFromCGImage:(CGImageRef)image
{
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
[NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
nil];

CVPixelBufferRef pxbuffer = NULL;

CGFloat frameWidth = [[self.videoSettings objectForKey:AVVideoWidthKey] floatValue];
CGFloat frameHeight = [[self.videoSettings objectForKey:AVVideoHeightKey] floatValue];


CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault,
frameWidth,
frameHeight,
kCVPixelFormatType_32ARGB,
(__bridge CFDictionaryRef) options,
&pxbuffer);

NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL);

CVPixelBufferLockBaseAddress(pxbuffer, 0);
void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
NSParameterAssert(pxdata != NULL);

CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();

CGContextRef context = CGBitmapContextCreate(pxdata,
frameWidth,
frameHeight,
8,
4 * frameWidth,
rgbColorSpace,
(CGBitmapInfo)kCGImageAlphaNoneSkipFirst);
NSParameterAssert(context);
CGContextConcatCTM(context, CGAffineTransformIdentity);
CGContextDrawImage(context, CGRectMake(0,
0,
CGImageGetWidth(image),
CGImageGetHeight(image)),
image);
CGColorSpaceRelease(rgbColorSpace);
CGContextRelease(context);

CVPixelBufferUnlockBaseAddress(pxbuffer, 0);

return pxbuffer;
}

我已经传递了图像数组并获取了视频,但它在 iPhone 4s 中崩溃了,请帮助我。

最佳答案

由于以下原因,此问题变得越来越普遍:

  1. “iPhone 4S 有 512 MB 的 DDR2 RAM”(wiki),随着操作系统的众多进程和硬件自身的需求,以及设备的老化(磨损),一个iPhone 4s 不太可能满足如此苛刻的要求。
  2. 这个问题 ( ios app maximum memory budget ) 假设应用程序在任何给定时间消耗的内存不超过 200MB(为安全起见,保持在 120MB 左右)。

要尝试并使这项工作正常进行,请在后台线程中放置尽可能多的与用户界面相关的函数。您的整个 - (CVPixelBufferRef)newPixelBufferFromCGImage:(CGImageRef)image 都在主线程上处理,因为您的方法 - (void)createMovieFromImages:(NSArray *)images...

不能保证将这些方法放在后台线程上会起作用,但值得一试。以下问题/答案/链接包含一些与线程有关的相关兴趣点,如果您不了解它们,即使您是开发人员,也可以阅读其中的一些兴趣点:

  1. > GCD - main vs background thread for updating a UIImageView
  2. > NSOperation and NSOperationQueue working thread vs main thread
  3. > http://pinkstone.co.uk/how-to-execute-a-method-on-a-background-thread-in-ios/

关于ios - 当我从图像创建视频时收到内存警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34452886/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com