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 (er开发者_高级运维ror){
NSLog(@"SCATTO: Salvataggio in library: ERRORE");
} else {
NSLog(@"SCATTO: Salvataggio in library: OK");
[self loadNewestPhoto];
}
}];
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.
精彩评论