Iphone UIImageWriteToSavedPhotosAlbum导致EXC\u访问错误
我开发了一个CGImage,当程序在屏幕上使用以下命令显示它时,它工作正常:Iphone UIImageWriteToSavedPhotosAlbum导致EXC\u访问错误,iphone,uikit,uiimage,core-graphics,exc-bad-access,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);
[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];
}
感谢您对此问题提供的帮助。您的图像数据(或更可能的是,它所派生的UIImage对象)已被释放或自动释放过一次,或未能被保留一次或多次。没有其他解释。我认为您需要保留保存的UIImage,直到它成功保存到照片库中。UIImageWriteToSavedPhotoAlbum有一个委托方法来通知保存图像的完成。您能告诉我们如何获得原始的
CGImageRef
?由于它在memmove()
中崩溃,因此听起来您指定的图像数据块没有完全分配,或者是过早释放的。这意味着它可能成功地保存了大约一半的图像,但另一半图像可能会崩溃,这可能超出范围。请尝试以下操作:
UIImageWriteToSavedPhotosAlbum(图像,自我,@selector(ImageSavedPhotosAlbum:didFinishSavingWithError:contextInfo:),上下文)
它会解决你的问题。如果你仍然遇到同样的问题,请告诉我。我想解决您的问题。我认为这是因为您从主线程以外的另一个线程调用UIImageWriteToSavedPhotosAlbum造成的(我认为您这样做是因为“output\u view.layer performSelectorOnMainThread”下面的一些行 请注意,这些UI图形功能不是完全线程安全的
也许可以尝试将“save to album”调用移动到主线程的“setContent”函数中进行测试。如果它也在那里崩溃,我真的不知道为什么会发生这种情况。到目前为止,所有答案都假设崩溃发生在
UIImageWriteToSavedPhotosAlbum()中
,但是堆栈跟踪imageDataFromImageRef
中的行向我暗示它在[UIImage imageWithCGImage:image]
中崩溃。也许您可以将该调用移动到一个中间变量来确定一下。您是如何调用以下方法的
-(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];
试试上面的那些。
你也许能解决这个问题
问候,
Satya对于我来说,EXC_BAD_ACCESS意味着你使用的是一个不再存在的对象(我知道它不正确,但这是我得到它的最大原因)。调试你的所有对象/变量,看看它们是否还有值 尝试
UIImageWriteToSavedPhotosAlbum([[UIImage-imageWithCGImage:image]retain],nil,nil,nil);
启用
NSZombieEnabled
并再次运行(可执行文件->参数->NSZombieEnabled=YES)您可以尝试此代码而不是您的代码,看看它是否正常工作,可能是您的输入流出错,而不是保存操作
#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];
#导入
NSAutoreleasePool*池=[[NSAutoreleasePool alloc]init];
浮动宽度=64;
浮动高度=64;
GLubyte*buffer=(GLubyte*)malloc(宽*高*4);
CGDataProviderRef provider=CGDataProviderCreateWithData(NULL,缓冲区,(宽度*高度*4),NULL);
//为CGImage创建设置
int bitsPerComponent=8;
int bitsPerPixel=32;
int bytesPerRow=4*宽度;
CGColorSpaceRef colorSpaceRef=CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo=kCGBitmapByteOrderDefault;
CGImageRef imageRef=CGImageCreate(宽度、高度、bitsPerComponent、bitsPerPixel、bytesPerRow、colorSpaceRef、bitmapInfo、提供程序、NULL、NO、0);
UIImageWriteToSavedPhotosAlbum([UIImageImageWithCGImage:imageRef],nil,nil,nil);
CGCOLORSPACTERELEASE(COLORSPACTEREF);
CGImageRelease(imageRef);
CGDataProviderRelease(提供程序);
[泳池排水沟];
所以
如果这是可行的,请一点一点地添加原始代码的一部分,以查看它何时崩溃。如果其他人对此有问题,我发现在我的特殊情况下,它是由其中一个函数引起的,不完全确定,但一旦我在我的应用程序中对它们全部进行了注释,它就开始工作了
CGImageRelease(imageRef);
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpaceRef);
谢谢你的回答,但我已经尝试在进入该函数之前保留UIImage,我也在之后删除了CGImageRelease,以防它做了什么。所有这些都没有解决问题。好的,谢谢。我将编辑问题,包括整个相关方法。我将删除一些部分。在你提供的代码中,你似乎假设缓冲区包含alpha通道,但(据我所知)实际情况可能并非如此。期望数据比实际长度长肯定会导致您遇到的问题。您可以分割
CVPixelBufferGetBytesPerRow()而不是每像素硬编码32位和4字节
byCVPixelBufferGetHeight()
并对结果进行四舍五入,这应该是每个像素的字节数。谢谢,但肯定有一个alpha通道。您是否尝试保留[UIImage imageWithCGImage:image]在你的UIImageWriteToSavedPhotosAlbum
调用中?这只是一个猜测,因为你的代码在我看来还行。我一直在查看堆栈。我不太理解它,因为我不懂arm汇编语言,但有点奇怪。在堆栈上的某个点上,代码在mov操作时停止。现在,我们不是这样吗可能是在堆栈顶部出现问题?不是。也许我不知道堆栈信息是如何显示的。啊,由于某种原因,箭头总是指向您需要查看的实际行。您好。谢谢您的回答。我尝试在它上使用performSelectorOnMainThread…但失败了。W