UIImageWriteToSavedPhotosAlbum导致EXC_BAD_ACCESS

时间:2010-12-11 23:18:36

标签: iphone uikit uiimage core-graphics exc-bad-access

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

[output_view.layer performSelectorOnMainThread:@selector(setContents:) withObject: (id) image waitUntilDone:YES];

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

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

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

#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

以下是解决问题的方法:

-(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];
 }

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

10 个答案:

答案 0 :(得分:2)

请试试这个:

UIImageWriteToSavedPhotosAlbum(image,self,@ selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo :),context);

它将解决您的问题。如果您仍然遇到同样的问题,请告诉我。我想解决你的问题。

答案 1 :(得分:1)

我认为你需要保存保存UIImage,直到它成功保存在照片库中。 UIImageWriteToSavedPhotoAlbum有一个委托方法来通知保存图像的完整性。

答案 2 :(得分: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];

所以

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

答案 3 :(得分:0)

您的图像数据(或更可能是其衍生自的UIImage对象)经常被释放或自动释放,或者无法保留一次或多次。没有其他解释。

答案 4 :(得分:0)

您能告诉我们您是如何获得原始CGImageRef的吗?由于它在memmove()中崩溃,听起来你正在指定一个未完全分配或过早释放的图像数据块。这意味着它可能会成功保存大约一半的图像,但随后会在另一半上崩溃,这可能是超出范围的。

答案 5 :(得分:0)

我认为这是因为你从另一个线程调用UIImageWriteToSavedPhotosAlbum而不是主线程(我认为你这样做是因为“output_view.layer performSelectorOnMainThread”有些线路。

请注意,这些UI图形功能并非完全线程安全。

也许尝试将“保存到相册”调用移动到主线程的“setContent”函数中进行测试。如果它在那里崩溃我真的不知道为什么会发生。

答案 6 :(得分:0)

到目前为止,所有答案都假设崩溃在UIImageWriteToSavedPhotosAlbum(),但是堆栈跟踪imageDataFromImageRef中的行告诉我它在[UIImage imageWithCGImage: image]中崩溃了。也许您可以将该调用移动到中间变量以确定。

答案 7 :(得分:0)

您如何调用以下方法

-(void)captureOutput: (AVCaptureOutput *) captureOutput didOutputSampleBuffer: (CMSampleBufferRef) sampleBuffer fromConnection: (AVCaptureConnection *) conenction{

如果你使用其他线程(主线程除外)调用此方法,那么只需要使用NSAutoReleasePool否则它将释放一些变量,因为它可能会崩溃。

还有一件事,

你正在使用它,

[output_view.layer performSelectorOnMainThread:@selector(setContents:) withObject: (id) image waitUntilDone:YES];

为什么使用performSelector,您可以直接调用

之类的方法
[output_view.layer setContents:image];

尝试以上那些。 你可能会解决问题。

此致

萨蒂亚

答案 8 :(得分:0)

对我来说EXC_BAD_ACCESS意味着你使用了一个不再存在的对象(我知道它不正确,但这是我得到它的最主要原因)。调试所有对象/变量,看看它们中是否还有值。

尝试UIImageWriteToSavedPhotosAlbum([[UIImage imageWithCGImage: image]retain],nil,nil,nil);

启用NSZombieEnabled并再次运行(可执行文件 - &gt;参数 - &gt; NSZombieEnabled = YES)

答案 9 :(得分:0)

如果其他人遇到这方面的问题,我发现在我的特定情况下,这是由其中一个功能造成的,不完全确定但是一旦我在我的应用程序中全部评论它就开始工作

CGImageRelease(imageRef);
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpaceRef);
相关问题