UIImageWriteToSavedPhotosAlbum导致EXC_BAD_ACCESS

Mat*_*ell 6 iphone exc-bad-access core-graphics uikit uiimage

我开发了一个CGImage,当程序在屏幕上显示它时它工作正常:

[output_view.layer performSelectorOnMainThread:@selector(setContents:) withObject: (id) image waitUntilDone:YES];
Run Code Online (Sandbox Code Playgroud)

现在,这样做会导致应用程序崩溃:

UIImageWriteToSavedPhotosAlbum([UIImage imageWithCGImage: image)],nil,nil,nil);
Run Code Online (Sandbox Code Playgroud)

我根本不明白.这是崩溃期间的堆栈:

#0  0x33b5db6e in memmove (Line 65)
#1  0x341ddee2 in CGAccessSessionGetBytes
#2  0x31ab4488 in alphaProviderGetBytes
#3  0x341ddf52 in CGAccessSessionGetBytes
#4  0x31abbc80 in writeOne
#5  0x31abbdae in _CGImagePluginWriteJPEG
#6  0x31ab2ddc in CGImageDestinationFinalize
#7  0x3037eda2 in imageDataFromImageWithFormatAndProperties
#8  0x3037effc in imageDataFromImageRef
#9  0x3038ea3c in __-[PLAssetsSaver _saveImage:imageData:properties:completionBlock:]_block_invoke_1
#10 0x33c32680 in _dispatch_call_block_and_release
#11 0x33c32ba0 in _dispatch_worker_thread2
#12 0x33bd7250 in _pthread_wqthread
Run Code Online (Sandbox Code Playgroud)

这是问题的方法:

-(void)captureOutput: (AVCaptureOutput *) captureOutput didOutputSampleBuffer: (CMSampleBufferRef) sampleBuffer fromConnection: (AVCaptureConnection *) conenction{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    // 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
    UInt8 * image_data = 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);
    //Removed part here which modifies the CVPixelBuffer pixels with a particular algorithm.
    [bottom_view setNeedsDisplay];
    [bottom_view performSelectorOnMainThread:@selector(setBackgroundColor:) withObject: [UIColor colorWithRed:0 green:av/255.0 blue:0 alpha:1] waitUntilDone: YES];
    // Create a device-dependent RGB color space
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    // Create a Quartz image from the pixel data
    CGDataProviderDirectCallbacks providerCallbacks = { 0, GetBytePointer, ReleaseBytePointer, GetBytesAtPosition, 0 };
    CGDataProviderRef d_provider = CGDataProviderCreateDirect(image_data,pixels*4,&providerCallbacks);
    CGImageRef image = CGImageCreate (width,height,8,32,bytesPerRow,colorSpace,kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst,d_provider,NULL,true,kCGRenderingIntentDefault);
    //Draw image
    if (needs_photo) {
        UIImageWriteToSavedPhotosAlbum([UIImage imageWithCGImage: image],nil,nil,nil);
        needs_photo = NO;
    }
    if (recording) {
        [writer_input appendSampleBuffer:sampleBuffer];
    }
    [output_view.layer performSelectorOnMainThread:@selector(setContents:) withObject: (id) image waitUntilDone:YES];
    // Unlock the pixel buffer
    CVPixelBufferUnlockBaseAddress(imageBuffer,0);
    // Free up the context and color space
    CGColorSpaceRelease(colorSpace);
    CGImageRelease(image);
    CGDataProviderRelease(d_provider);
    [pool drain];
 }
Run Code Online (Sandbox Code Playgroud)

感谢您对此问题的任何帮助.

小智 1

您可以尝试此代码而不是您的代码,看看它是否有效,也许您的输入流有问题,而不是保存操作有问题。

#import <OpenGLES/ES1/gl.h>

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

float width = 64;
float height = 64;

GLubyte *buffer = (GLubyte *) malloc(width * height * 4);
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, (width * height * 4), NULL);

// set up for CGImage creation
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, 0);

UIImageWriteToSavedPhotosAlbum([UIImage imageWithCGImage:imageRef],nil,nil,nil);

CGColorSpaceRelease(colorSpaceRef);
CGImageRelease(imageRef);
CGDataProviderRelease(provider);

[pool drain];
Run Code Online (Sandbox Code Playgroud)

所以

如果这有效,请一点一点地添加原始代码的一部分,看看它何时崩溃。