Objective c 加快保存图像-iOS

Objective c 加快保存图像-iOS,objective-c,ios,performance,Objective C,Ios,Performance,我正在做更多的迷你项目,这将被纳入一个新的项目。它基本上是一个测试单元 我正在做的是创建一个AVCaptureSession,然后为OutputSampleBufferDelegate创建一个方法。在该方法中,我将sampleBuffer转换为UIImage并保存UIImage。当我在iPhone4上运行应用程序时,它每秒只能保存2-3张图像。必须有一种更有效的方法来保存图像 有人能帮我加快速度吗 谢谢 如果您在第一种方法中注释掉以下行,您可以看到您可以生成多少图像: [imageData w

我正在做更多的迷你项目,这将被纳入一个新的项目。它基本上是一个测试单元

我正在做的是创建一个
AVCaptureSession
,然后为
OutputSampleBufferDelegate
创建一个方法。在该方法中,我将
sampleBuffer
转换为
UIImage
并保存
UIImage
。当我在iPhone4上运行应用程序时,它每秒只能保存2-3张图像。必须有一种更有效的方法来保存图像

有人能帮我加快速度吗

谢谢


如果您在第一种方法中注释掉以下行,您可以看到您可以生成多少图像:

[imageData writeToFile:finalPath atomically:YES];
我之所以这么说,是因为您将花费大量时间将该映像写入磁盘。在不将图像写入磁盘的情况下,看看这是如何执行的,这会很有趣。至少通过这种方式,您将知道是否所有时间都花在实际创建图像上,而不是存储图像上。或者你也可以像另一张海报上提到的那样,用仪器测量你在每种方法中的时间

如果将图像写入磁盘花费的时间太长,那么我建议尝试实现一种缓存机制,将图像缓存在内存中,稍后再将其写入磁盘


尝试在后台线程上调用writeToFile:atomically:可能也会有所帮助。

使用此代码,我可以将保存图像的时间缩短到0.1秒

- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
       fromConnection:(AVCaptureConnection *)connection 
{  

    double frameTime = CFAbsoluteTimeGetCurrent();
    UIImage *resultUIImage = [self imageFromSampleBuffer:sampleBuffer];

    // takes freaking forever to do.
    double pre = CFAbsoluteTimeGetCurrent();
    NSData *imageData = UIImageJPEGRepresentation(resultUIImage, 0.9);
    NSLog(@"It took to write the file:%f",CFAbsoluteTimeGetCurrent()-pre);

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory
                                                         ,NSUserDomainMask
                                                         , YES);
    NSString *path = [paths objectAtIndex:0];
    NSString *filename =  [NSString stringWithFormat:@"%f.png", frameTime];
    NSString *finalPath = [path stringByAppendingString:filename];
    [imageData writeToFile:finalPath atomically:YES];
}

在Instruments中查看代码时,哪些任务/调用占用的时间最多?感谢您的评论。我看了一眼,测量了最长的时间。它正在将uiimage转换为NSData对象。我在网上找到了一个更有效的方法(使用jpeg格式而不是png格式)。iPhone似乎必须在内部将图像存储为JPG,而转换为png才是真正的瓶颈,不能写入磁盘?我得到了CGBitmapContextCreateImage:无效上下文0x0。如果要查看回溯,请设置CG_CONTEXT_SHOW_backtrace环境变量。
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
       fromConnection:(AVCaptureConnection *)connection 
{  

    double frameTime = CFAbsoluteTimeGetCurrent();
    UIImage *resultUIImage = [self imageFromSampleBuffer:sampleBuffer];

    // takes freaking forever to do.
    double pre = CFAbsoluteTimeGetCurrent();
    NSData *imageData = UIImageJPEGRepresentation(resultUIImage, 0.9);
    NSLog(@"It took to write the file:%f",CFAbsoluteTimeGetCurrent()-pre);

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory
                                                         ,NSUserDomainMask
                                                         , YES);
    NSString *path = [paths objectAtIndex:0];
    NSString *filename =  [NSString stringWithFormat:@"%f.png", frameTime];
    NSString *finalPath = [path stringByAppendingString:filename];
    [imageData writeToFile:finalPath atomically:YES];
}