writeImageToSavedPhotosAlbum 太慢?

发布于 2024-12-03 06:50:45 字数 1185 浏览 5 评论 0原文

我正在开发一个 iPhone 应用程序,其中包含一些标准的“相机”功能。保存到相机胶卷真的太慢了​​,在iPhone 4上大约需要四秒钟。有什么办法可以提高速度吗?

如果您查看默认的 iPhone 相机应用程序,它可以在没有很大延迟的情况下拍摄后续照片,并且照片会立即保存到磁盘(如果您单击屏幕下角的最后一张照片的小方块,照片库始终会在保存的照片上打开,即使您拍摄了大量照片)。

这是我用来从缓冲区获取图像然后将照片保存在相机胶卷中的重要代码的两个片段;我尝试在第二个片段之前和之后放置 NSLog,它确认了 4 秒的延迟才能完成保存过程。

[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
 {
     NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
     UIImage *image = [[UIImage alloc] initWithData:imageData];

...

     ALAssetsLibrary *library = [[[ALAssetsLibrary alloc] init] autorelease];
     NSLog(@"SCATTO: Inizio salvataggio in library...");
     [library writeImageToSavedPhotosAlbum:[image CGImage] metadata:exifAttachments_dictionary completionBlock:^(NSURL *newURL, NSError *error) {
         if (error){
             NSLog(@"SCATTO: Salvataggio in library: ERRORE");
         } else {
             NSLog(@"SCATTO: Salvataggio in library: OK");
             [self loadNewestPhoto];
         }
     }];

I'm developing an iPhone app that includes some standard "camera" functions. Saving to camera roll is really TOO slow, it requires about four seconds on iPhone 4. Is there some way to improve speed?

If you look at the default iPhone Camera app, it can take subsequent photos without big delays, and photos are saved quite instantly to disk (if you click on the small square with the last photo taken, in the lower corner of the screen, the photo library always opens on the saved picture, even if you take a big sequence of them).

Here's two snipplets of the significant code I use to get the image from the buffer and then to save the photo in the camera roll; I tried to put a NSLog before and after the 2nd snipplet, and it confirmed a 4 seconds delay to complete the the save process.

[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
 {
     NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
     UIImage *image = [[UIImage alloc] initWithData:imageData];

...

     ALAssetsLibrary *library = [[[ALAssetsLibrary alloc] init] autorelease];
     NSLog(@"SCATTO: Inizio salvataggio in library...");
     [library writeImageToSavedPhotosAlbum:[image CGImage] metadata:exifAttachments_dictionary completionBlock:^(NSURL *newURL, NSError *error) {
         if (error){
             NSLog(@"SCATTO: Salvataggio in library: ERRORE");
         } else {
             NSLog(@"SCATTO: Salvataggio in library: OK");
             [self loadNewestPhoto];
         }
     }];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

惟欲睡 2024-12-10 06:50:45

我不是这方面的专家,但对此主题感到好奇。

我认为保存图像总是需要大量的时间。如果您希望支持未来的 iPhone,因为它们很可能会创建比现在更大的图像,则尤其如此。

我认为可以提高性能的一件事是如果您使用 -(void)writeImageDataToSavedPhotosAlbum: ...
而不是 - (void)writeImageToSavedPhotosAlbum: ...。 JPEG 提取和压缩过程可以忽略。它的成本非常高,因为它将大约 3MB 的数据转换为大约 15MB。

这纯粹是猜测,但如果 JPEG 提取/压缩是硬件加速的,那么如果同时显示相机输出,则可能会占用硬件资源。所以这个过程必须由CPU来完成。

I am not an expert in this, but am curious about the topic.

I think there always will be a significant amount of time needed to save an image. This is especially true if you wish to support future iPhones which will most likely create larger images than today.

One thing that I think may improve the performance is if you use -(void)writeImageDataToSavedPhotosAlbum: ...
instead of - (void)writeImageToSavedPhotosAlbum: .... The JPEG extraction and compression process can be ignored then. It is very costly as it transforms about 3MB of data to about 15MB.

This is pure speculation, but if the JPEG extraction/compression is hardware accelerated, the hardware resource may be occupied if you display the camera output at the same time. So the process has to be done by the CPU.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文