我正在尝试使用AVCaptureSession获取图像。我按照本教程http://www.benjaminloulier.com/posts/2-ios4-and-direct-access-to-the-camera进行了操作。我正在从图像引用创建uiimage,然后从该uiimage获取像素。 但该应用程序在一段时间后(少于30秒)崩溃。我尝试使用Leaks进行分析并且崩溃了。使用日志我发现应用程序在行CGContextDrawImage(context,rect,image1.CGImage)之前崩溃; 你们对我可能做错了什么有什么建议吗?在应用程序崩溃前几秒钟,我也会看到内存分配错误。请帮忙。
代码发布在下面..
// Create a UIImage from sample buffer data
- (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer
{
lock = @"YES";
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
void *baseAddress = 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);
// Create a device-dependent RGB color space
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
size_t bufferSize = CVPixelBufferGetDataSize(imageBuffer);
// Create a Quartz direct-access data provider that uses data we supply.
NSData *data = [NSData dataWithBytes:baseAddress length:bufferSize];
CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
CGImageRef quartzImage = CGImageCreate(width, height, 8, 32, bytesPerRow,
colorSpace, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little,
dataProvider, NULL, true, kCGRenderingIntentDefault);
CGDataProviderRelease(dataProvider);
// Unlock the pixel buffer
CGColorSpaceRelease(colorSpace);
// Create an image object from the Quartz image
UIImage *image = [UIImage imageWithCGImage:quartzImage];
// Release the Quartz image
CGImageRelease(quartzImage);
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
baseAddress = nil;
[data release];
lock = @"NO";
return(image);
}
-(void)calculate
{
@try {
UIImage *image1 = [self stillImage]; //Capture an image from the camera.
//Extract the pixels from the camera image.
CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
size_t bytesPerRow = image1.size.width*4;
unsigned char* bitmapData = (unsigned char*)malloc(bytesPerRow*image1.size.height);
CGContextRef context = CGBitmapContextCreate(bitmapData, image1.size.width, image1.size.height, 8, bytesPerRow,colourSpace,kCGImageAlphaPremultipliedFirst|kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colourSpace);
CGContextDrawImage(context, rect, image1.CGImage);
unsigned char* pixels = (unsigned char*)CGBitmapContextGetData(context);
totalLuminance = 0.0;
for(int p=0; p<image1.size.width*image1.size.height*4; p+=4)
{
totalLuminance += pixels[p]*0.3 + pixels[p+1]*0.59 + pixels[p+2]*0.11;
}
totalLuminance /= (image1.size.height * image1.size.width);
pixels = nil;
bitmapData = nil;
[image1 release];
CGContextRelease(context);
//image1 = nil;
//totalLuminance = [n floatValue]; //Calculate the total luminance.
float f = [del.camcont.slider value];
float total = totalLuminance * f;
NSString *ns = [NSString stringWithFormat:@"Lux : %0.2f", total];
NSLog(@"slider = %f",f);
NSLog(@"totlaluminance = %f",totalLuminance);
NSLog(@"%@",ns);
//NSString *ns = [NSString initWithFormat:@"Lux : %0.2f", total];
[del.camcont.lux setText:ns];//Display the total luminance.
self.stillImage = nil;
//[self.stillImage release];
ns = nil;
//n = nil;
//del = nil;
}
@catch (NSException *exception) {
NSLog(@"main: Caught %@: %@", [exception name], [exception reason]);
}
}
答案 0 :(得分:1)
我不清楚你为什么要使用CMSampleBufferRef
,然后创建CGImageRef
然后创建UIImage
,然后取出UIImage
的{{1}} 1}}并取出数据,然后将其推送到CGImageRef
指针(基本上指向首先在unsigned char
中的相同字节)。
如果您执行以下操作,您将简化生活(并且您应该更容易调试):
CMSampleBufferRef
(嵌套CVPixelBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer,0);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
uint8_t *pixels = malloc(bytesPerRow*height);
memcpy(pixels, baseAddress, bytesPerRow*height);
baseAddress = nil;
imageBuffer = nil;
sampleBuffer = nil;
float totalLuminance = 0.0;
for(int r=0; r<height; r++)
{
for(int p=0, p<width, p+=4)
{
totalLuminance += pixels[p+(r*bytesPerRow)]*0.3
+ pixels[p+1+(r*bytesPerRow)]*0.59
+ pixels[p+2+(r*bytesPerRow)]*0.11;
{
}
free(pixels);
totalLuminance /= (width * height);
循环用于补偿由于填充而无法假定for
与bytesPerRow
相同的事实。)
答案 1 :(得分:0)
乍一看,内存管理看起来还不错。作为一种解决方法,您可以考虑使用UIImage -imageWithData: 以防有关自定义CGImageCreate代码的事情。这是因为你正在使用CGImage创建一个UIImage。