Using prepareForImageCapture causes a crash when saving images with ALAssetsLibrary
ALAssetsLibrary seems to not like the images created when using [filter prepareForImageCapture], as it causes a crash. Commenting out [filter prepareForImageCapture] prevents the crash, but then you're obviously not getting the faster rendering and lower memory usage.
Here's the code I'm using in the SimplePhotoFilter example to save the image (inside the takePhoto method):
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
CGImageRef imageRef = [processedImage CGImage];
[library writeImageToSavedPhotosAlbum:imageRef metadata:nil completionBlock:^(NSURL *assetURL, NSError *error2)
{
if (error2) {
NSLog(@"ERROR: the image failed to be written");
}
else {
NSLog(@"PHOTO SAVED - assetURL: %@", assetURL);
}
// CGImageRelease(imageRef);
}
];
And the relevant parts of the crash log:
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x0d578000
Crashed Thread: 5
Thread 5 name: Dispatch queue: com.apple.root.default-priority
Thread 5 Crashed:
0 libsystem_c.dylib 0x31e68a60 memcpy$VARIANT$CortexA9 + 160
1 CoreGraphics 0x3576dca0 CGAccessSessionGetBytes + 124
2 ImageIO 0x370d7502 alphaProviderGetBytes + 794
3 CoreGraphics 0x3576dd0c CGAccessSessionGetBytes + 232
4 ImageIO 0x370e355c _CGImagePluginWriteJPEG + 2280
5 ImageIO 0x370d46f6 CGImageDestinationFinalize + 126
6 PhotoLibraryServices 0x379562ae imageDataFromImageWithFormatAndProperties + 74
7 PhotoLibraryServices 0x37945068 __65-[PLAssetsSaver _saveImage:imageData:properties:completionBlock:]_block_invoke_0 + 352
8 libdispatch.dylib 0x34acac52 _dispatch_call_block_and_release + 6
9 libdispatch.dylib 0x34acd810 _dispatch_worker_thread2 + 252
10 libsystem_c.dylib 0x31dfcdf4 _pthread_wqthread + 288
11 libsystem_c.dylib 0x31dfccc8 start_wqthread + 0
After pulling latest bits I'm also getting a crash that appears to be related to prepareForImageCapture
I'm using -[GPUImageFilter imageByFilteringImage:] and started getting a crash during the filter dealloc, which happens in destroyFilterFBO:
CFRelease(renderTexture);
If I comment out this line in imageByFilteringImage:
//[self prepareForImageCapture];
then the crash goes away.
This was a tricky one to track down. Because of the asynchronous saving of the photo to the asset library, the pixel buffer for the input photos was being deallocated before the photo was finished being written to disk.
To fix this, I had to put in place an extra CFRetain around the pixel buffer when an image was being captured, followed by a CFRelease on the deallocation of the CGImage using it. This makes it safe to use the fast image download path for memory and speed improvements while saving to the asset library without crashes.
Also, to be on the safe side, you'll want to retain the CGImageRef returned by the UIImage, because that autoreleased UIImage will probably be deallocated at some point before the asset library writing is done. The CGImageRef returned from the -CGImage method is not owned by you until you retain it, so it will go away when the UIImage does. This led to all sorts of fun crashes when people started using ARC and had been relying on the UIImage to stick around until the end of the run loop, but we're dealing with an asynchronous method here, so even manual reference counting would have an issue with this.
In any case, check out the code now, and the SimplePhotoFilter example which now saves to the user's photo library.
That fixed it, and thanks for the note on retaining the CGImageRef... I have a much better understanding of what's going on there now.