加速iOS图片保存

6
我正在开发一个小型项目,该项目将被包含在一个新项目中。它基本上是一个测试单元。
我的做法是创建一个AVCaptureSession,然后创建一个OutputSampleBufferDelegate方法。在这个方法中,我将sampleBuffer转换为UIImage并保存UIImage。当我在我的iPhone 4上运行应用程序时,它每秒只能保存2-3张图像。肯定有更有效的方法来保存图像。
有人可以帮我加速吗?
谢谢! 代码大部分来自此处
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{   
    UIImage *resultUIImage = [self imageFromSampleBuffer:sampleBuffer];

    NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(resultUIImage)];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [paths objectAtIndex:0];

    CMTime frameTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);

    NSString *filename =  [NSString stringWithFormat:@"%f.png", CMTimeGetSeconds(frameTime)];

    NSString *finalPath = [path stringByAppendingString:filename];

    [imageData writeToFile:finalPath atomically:YES];
}

// Create a UIImage from sample buffer data
- (UIImage *)imageFromSampleBuffer:(CMSampleBufferRef)sampleBuffer 
{
    // Get a CMSampleBuffer's Core Video image buffer for the media data
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 
    // Lock the base address of the pixel buffer
    CVPixelBufferLockBaseAddress(imageBuffer, 0); 

    // Get the number of bytes per row for the pixel buffer
    void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer); 

    // Get the number of bytes per row for the pixel buffer
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); 
    // Get the pixel buffer width and height
    size_t width = CVPixelBufferGetWidth(imageBuffer); 
    size_t height = CVPixelBufferGetHeight(imageBuffer); 

    // Create a device-dependent RGB color space
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 

    // Create a bitmap graphics context with the sample buffer data
    CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst); 
    // Create a Quartz image from the pixel data in the bitmap graphics context
    CGImageRef quartzImage = CGBitmapContextCreateImage(context); 
    // Unlock the pixel buffer
    CVPixelBufferUnlockBaseAddress(imageBuffer,0);

    // Free up the context and color space
    CGContextRelease(context); 
    CGColorSpaceRelease(colorSpace);

    // Create an image object from the Quartz image
    UIImage *image = [UIImage imageWithCGImage:quartzImage];

    // Release the Quartz image
    CGImageRelease(quartzImage);

    return image;
}

当您在Instruments中查看代码时,哪些任务/调用占用了最多的时间? - Mats
2个回答

7
使用这段代码,我可以将图像保存时间缩短到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];
}

不错。看起来iPhone必须将图像内部存储为JPG格式,而转换为PNG格式才是真正的瓶颈,而不是写入磁盘? - Meekohi
我得到了CGBitmapContextCreateImage: invalid context 0x0的错误。如果您想查看回溯信息,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量。 - loretoparisi

5

请您尝试将第一个方法中的以下代码注释掉,看看可以生成多少张图片:

[imageData writeToFile:finalPath atomically:YES];

我说这样做的原因是你将花费很多时间将该图像写入磁盘。如果不将图像写入磁盘,看看性能如何会很有趣。至少这样你就会知道所有时间是花在创建图像还是存储图像上。或者你可以像另一个帖子中提到的那样,使用Instruments来计算每个方法所需的时间。
如果发现将图像写入磁盘太耗时,那么建议尝试实现缓存机制,将图像缓存在内存中,稍后再将其写入磁盘。
在后台线程上调用writeToFile:atomically:也可能有所帮助。

谢谢您对此进行评论。我看了一下并测量了最耗时的部分。它是将UIImage转换为NSData对象。我在网上找到了一种更有效的方法(使用jpeg表示而不是png)。 - SamB

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接